当前位置:Gxlcms > JavaScript > php结合imgareaselect实现图片裁剪_jquery

php结合imgareaselect实现图片裁剪_jquery

时间:2021-07-01 10:21:17 帮助过:16人阅读

引用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; 
  }

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

人气教程排行