functionUploadlogo () {$category= ' Apply '; $dir=c (' Applydir '); $upload=New\think\upload ();//instantiating an upload class $upload->maxsize = 0;//set attachment upload size $upload->exts =c (' allexts ')); $uploadpath=$_server[' Document_root ']. __root__.$dir. ‘/‘; if(!file_exists($uploadpath) ) {Utilcontroller:: Mkdirs ($uploadpath); } $upload->rootpath =$uploadpath;//Set attachments upload root directory $upload->savepath = ";//Set attachments Upload (sub) directory $filename=Date("Ymdhis").Mt_rand(10000, 99999 ); $upload->savename =$filename;//set the uploaded filename//upload file $info=$upload-upload (); if(!$info) {//Upload error message $this->error ($upload-GetError ()); } Else{//Upload Successful $json[' Source ']=c (' Web_server '). __root__.$dir.‘ /‘.$info[' Filedata '] [' Savepath '].$info[' Filedata '] [' Savename '] ; //generate a small image $targetpath 2=$uploadpath.$category.‘ smallimg/'; Utilcontroller:: Mkdirs ($targetpath 2); Utilcontroller:: Imagecropper ($uploadpath.$info[' Filedata '] [' Savepath '].$info[' Filedata '] [' Savename '], 90, 90,$targetpath 2.$info[' Filedata '] [' Savename ']); $json[' URL ']=c (' Web_server '). __root__.$dir.‘ /‘.$category.‘ smallimg/'.$info[' Filedata '] [' Savename ']; $this->ajaxreturn ($json); } }
/** * Cycle Create folder * @param unknown $path * @param number $mode*/ Static functionMkdirs ($path,$mode= 0755) { $adir=Explode( ‘/‘,$path ); $dirlist= ' '; $rootdir=Array_shift($adir ); if(($rootdir! = '. ' | |$rootdir! = ') &&!file_exists($rootdir )) { @mkdir($rootdir,$mode ); } foreach($adir as $key=$val ) { $dirlist.= "/" .$val; $dirpath=$rootdir.$dirlist; if(!file_exists($dirpath )) { @mkdir($dirpath,$mode ); } } }
/** * @param unknown $source _path picture path * @param unknown $target _width required width * @param unknown $target _heigh T required height * @param unknown $target _path Target Path * @return Boolean * @author Panqingqiang */static function Imagecrop Per ($source _path, $target _width, $target _height, $target _path) {$source _info = getimagesize ($source _path); $source _width = $source _info [0]; $source _height = $source _info [1]; $source _mime = $source _info [' MIME ']; $source _ratio = $source _height/$source _width; $target _ratio = $target _height/$target _width; SOURCE Graph High if ($source _ratio > $target _ratio) {$cropped _width = $source _width; $cropped _height = $source _width * $target _ratio; $source _x = 0; $source _y = ($source _height-$cropped _height)/2; }//source map too wide ElseIf ($source _ratio < $target _ratio) {$cropped _width = $source _height/$target _ratio; $cropped _height = $source _height; $source _x = ($source _width-$cropped _width)/2; $source_y = 0; }//Source graph moderate Else {$cropped _width = $source _width; $cropped _height = $source _height; $source _x = 0; $source _y = 0; } switch ($source _mime) {case ' image/gif ': $source _image = imagecreatefromgif ($source _path); Break Case ' image/jpeg ': $source _image = imagecreatefromjpeg ($source _path); Break Case ' image/png ': $source _image = imagecreatefrompng ($source _path); Break Default:return false; Break } $target _image = Imagecreatetruecolor ($target _width, $target _height); $cropped _image = Imagecreatetruecolor ($cropped _width, $cropped _height); Crop Imagecopy ($cropped _image, $source _image, 0, 0, $source _x, $source _y, $cropped _width, $cropped _height); Zoom imagecopyresampled ($target _image, $cropped _image, 0, 0, 0, 0, $target _width, $target _height, $cropped _width, $c Ropped_height); Header (' Content-type:image/jpeg '); Imagejpeg ($target _image, $target _path); Imagedestroy ($source _image); Imagedestroy ($target _image); Imagedestroy ($cropped _image); }
PHP image Processing