视递美食网
您的当前位置:首页php结合imgareaselect实现图片裁剪_jquery

php结合imgareaselect实现图片裁剪_jquery

来源:视递美食网
 引用CSS

/js/jquery.imgareaselect-0.9.10/css/imgareaselect-default.css 

引用js



html


 


 

 

jQuery代码



确定裁剪



服务器端php代码

public function actionCrop($src_path,$x1,$x2,$y1,$y2){
 $pic =$src_path;

 $width = $x2-$x1;
 $height = $y2-$y1;

 $type=exif_imagetype($pic); //判断文件类型
 $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
 if(!in_array($type, $support_type,true)) {
 echo "this type of image does not support! only support jpg , gif or png";
 exit();
 }
 switch($type) {
 case IMAGETYPE_JPEG :
 $image = imagecreatefromjpeg($pic);
 break;
 case IMAGETYPE_PNG :
 $image = imagecreatefrompng($pic);
 break;
 case IMAGETYPE_GIF :
 $image = imagecreatefromgif($pic);
 break;
 default:
 echo "Load image error!";
 exit();
 }

 $copy = $this->PIPHP_ImageCrop($image, $x1, $y1, $width, $height);//裁剪

 imagejpeg($copy, $src_path); //替换新图
 return ['result'=>'Success','path'=>$src_path]; //返回新图地址
 }
 function PIPHP_ImageCrop($image, $x, $y, $w, $h){
 $tw = imagesx($image); 
 $th = imagesy($image); 

 if ($x > $tw || $y > $th || $w > $tw || $h > $th) return FALSE; 

 $temp = imagecreatetruecolor($w, $h); 
 imagecopyresampled($temp, $image, 0, 0, $x, $y, $w, $h, $w, $h); 
 return $temp; 
 }

以上所述就是本文的全部内容了,希望大家能够喜欢。

显示全文