Demo Client API

DEMO UPLOAD IMAGE TO CDN


Select image to upload:

Process on server
<?php
if(isset($_POST["submit"])) {
    if(!empty(
$_FILES["fileToUpload"]) && !empty($_FILES["fileToUpload"]["tmp_name"])) {
        
$check getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if (
$check !== false) {
            require(
'ImageClient.php');
            
$result ImageClient::getInstance()->uploadFile($_FILES["fileToUpload"]['tmp_name'], $_FILES["fileToUpload"]['name'], ''$err);
            if (
$result) {
                
$src ImageClient::getInstance()->getImageUrl($_FILES["fileToUpload"]['name'], ''500);
                echo 
"File is uploaded.";
                echo 
'<p><img src="' $src '"></p>';
            } else {
                echo 
"Error! " $err;
            }
        } else {
            echo 
"File is not an image.";
        }
    }else{
        echo 
'Please choose some file';
    }
}
?>

Class ImageClient.php
<?php

class ImageClient {
    const 
CONNECT_TIMEOUT 30// Thoi gian toi da cho connect duoc toi API
    
const TIME_OUT 30// Thoi gian toi da cho API tra loi

    
protected static $instance;

    protected static 
$cdnUrl 'http://cdn.local/';
    protected static 
$cdnApiUrl 'http://cdn.local/api/v1';
    protected static 
$cdnUsername 'testapi';
    protected static 
$cdnPassword 'api@123';
    protected static 
$imageQuality 80;

    public function 
__construct() {

    }

    public static function 
getInstance() {
        if (empty(
self::$instance)) {
            
self::$instance = new ImageClient();
        }
        return 
self::$instance;
    }

    public function 
getImageUrl($file_name ''$dir ''$width 0$height 0){

        if (
$width == && $height == 0) {
            
$path '';
        } else {
            if (
$width == 0) {
                
$path 'thumb_h/' $height ',' self::$imageQuality;
            } elseif (
$height == 0) {
                
$path 'thumb_w/' $width ',' self::$imageQuality;
            } else {
                
$path 'thumb/' $width '_' $height ',' self::$imageQuality;
            }
        }
        if(!empty(
$dir)) {
            return 
self::$cdnUrl . (!empty($path) ? $path '/' '') . self::$cdnUsername '/' $dir '/' $file_name;
        }
        return 
self::$cdnUrl . (!empty($path) ? $path '/' '') . self::$cdnUsername '/' $file_name;
    }

    public function 
uploadFile($tmpLocalFile$filename$dir '', &$err$overwrite true$keepLocal false) {
        if(empty(
$dir)){
            
$filename self::$cdnUsername '/' $filename;
        }else{
            
$filename self::$cdnUsername '/' $dir '/' $filename;
        }
        
$cfile $this->makeCurlFile($tmpLocalFile);
        
$data = array(
            
'filename' => $filename,
            
'filedata' => $cfile,
            
'overwrite' => (int) $overwrite
        
);
        
$requestUrl self::$cdnApiUrl.'/upload';
        
$result $this->doRequest($requestUrl$data$err);

        if(!empty(
$result) && isset($result['code']) && $result['status'] == 0){
//            if (!$keepLocal) {
//                unlink($tmpLocalFile);
//            }
            
return true;
        }
        if(!empty(
$result)) {
            
$err array_pop($result);
        }
        return 
false;
    }

    protected function 
doRequest($url$data_post, &$err ''){
        
$curl curl_init();
        
curl_setopt($curlCURLOPT_HTTPHEADER, [
            
'username: '.self::$cdnUsername,
            
'password: '.self::$cdnPassword
        
]);
        
curl_setopt($curlCURLOPT_CONNECTTIMEOUTself::CONNECT_TIMEOUT);
        
curl_setopt($curlCURLOPT_TIMEOUTself::TIME_OUT);
        
curl_setopt($curlCURLOPT_URL$url);
        
curl_setopt($curlCURLOPT_VERBOSEfalse);
        
curl_setopt($curlCURLOPT_POSTtrue);
        
curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($curlCURLOPT_FOLLOWLOCATIONtrue);
        
curl_setopt($curlCURLOPT_POSTFIELDS$data_post);

        
$result curl_exec($curl);
        
$err curl_error($curl);
        
curl_close($curl);

        return 
json_decode($resulttrue);
    }

    protected function 
makeCurlFile($file){
        
$mime mime_content_type($file);
        
$info pathinfo($file);
        
$name $info['basename'];
        
$output = new \CURLFile($file$mime$name);
        return 
$output;
    }
}