ThinkPHP adds seven cattle upload function

Time:2022-2-12

First, you need to download the PHP version SDK of qiniu:https://developer.qiniu.com/sdk#official-sdk

Rename the file qiniu, then move it to the extend directory, and then you can use it directly.

<?
/**
 *Seven cattle file upload processing
 */
require_ once '../ extend/Qiniu/autoload. php';  // Import seven cattle file
use think\Db;
use Qiniu\Auth;//  Introduction of seven cattle authentication category
use Qiniu\Storage\UploadManager;//  Introduction of seven cattle upload class
use Qiniu\Processing\PersistentFop;// Thumbnail persistence
class QiniuUpload extends Common{
	/**
	 *[upload_files]
	 *@ param [type] $files [uploaded file]
	 * @return [type]        [description]
	 */
	public function upload_files($files){
		//1. Validation file -- omitted
		//Get the seven cattle configuration parameters
        $file_ path = $files->getRealPath();//  Gets the local path of the file object
        if(empty($file_path)){
            Return ['code' = > - 200, 'MSG' = > 'please pass in the local path of the file'];
        }
		$system_ cofing = system_ cofing();            // Get the system configuration - the seven cow configuration of gas is cached in the system parameters
        //You need to fill in your access key and secret key
        $accessKey = $system_ cofing['qiniu_accessKey'];// "8G-G8oyFme8ltP5p_zc6XaOrzcMST9hc5YCINlsR";    Log in to qiniu to view
        $secretKey = $system_ cofing['qiniu_secretKey'];// "Iz5YDPIgNWVQxO6GMAF7jWUeUf5UXyUh6HlTb7oa";    Log in to qiniu to view
        $bucket = $system_ cofing['qiniu_bucket'];      // "img";    Log in to qiniu to view -- storage space naming
        $folder = $system_ cofing['qiniu_folder'];      // Qiniu directory ID
        $domain = $system_ cofing['qiniu_url'];         // Accessing domain names
        //Build authentication object
        $auth = new Auth($accessKey, $secretKey);
        //Generate upload token
        $token = $auth->uploadToken($bucket);
        //Get file suffix
        $ext = strtolower(pathinfo($files->getInfo('name'), PATHINFO_ EXTENSION));  // suffix
        //File name saved after uploading to qiniu
        $key = $folder.'/'.date('Ymd').'/'.md5(microtime(true)).'.'.$ext;
        //Initialize the uploadmanager object and upload files.
        $uploadMgr = new UploadManager(); 
        //Call the putfile method of uploadmanager to upload files.
        list($ret, $err) = $uploadMgr->putFile($token, $key, $file_path);
        if ($err !== null) {
            return ['code'=>-200,'msg'=>$err->message(),'statusCode'=>$err->code];
        } else {
            return [
                'code'=>200,
                'msg '= >' upload succeeded ',
                'value' = > $key, // return the path to save the image
                'url'=>$domain.'/'.$ Key, // returns the full path of the saved image
            ];
        }
	}
}

The above is the code for direct and simple use

Recommended Today

php displays Chinese graphically and specifies the ttf font library

1 <?php 2 header("Content-Type:image/png"); 3 $img=imagecreatetruecolor(400,300); 4 // imagejpeg($img); 5 // imagejpeg($img,"./img/copy_img01.jpg",10); 6 $color1=imagecolorallocate($img,100,100,100); 7 $color2=imagecolorallocate($img,255,0,0); 8 $str="北京欢迎你!"; 9 imagefill($img,0,0,$color1); 10 // imagestring($img,5,0,0,$str,$color2); 11 $fontfile="F:\phpStudy\WWW\myweb\FZXBSJW.ttf"; 12 imagettftext($img,24,0,100,100,$color2,$fontfile,$str); 13 imagepng($img); 14 imagedestroy($img); 15 // header("Content-Type:text/html"); 16 // echo "gave over!"; Line 13 imagepng($img) must have, and the Chinese font path must be an absolute path.