美文网首页
php图片压缩居中裁剪

php图片压缩居中裁剪

作者: 程序猿某人_ | 来源:发表于2019-12-20 18:33 被阅读0次

用于业务需求,需要进行图片处理,图片压缩生成缩略图,超过规定尺寸要居中裁剪,现封装一个图片处理方法,直接调用即可:

 /**
    * desription 压缩图片
    * @param sting $imgsrc 图片路径
    * @param string $imgdst 压缩后保存路径
    * @param string $imgWidth 新图片宽度
    * @param string $imgHeight 新图片高度
    * @param string $imgHeight 压缩质量0-100
    */
protected function compressed_image($imgsrc,$imgdst,$imgWidth,$imgHeight,$quality=100){
      list($width,$height,$type)=getimagesize($imgsrc);
      $new_width = $imgWidth; //$pic_config['width'];
      $new_height = $imgHeight; //$pic_config['height'];

      //按照比例最小比例压缩  
      if($width/$height > $new_width/$new_height) {
        //以高为标准
        $new_width = $new_height * ($width / $height);
      }else{
        //以宽度为准
        $new_height = $new_width * ($height / $width);
      }

      switch($type){
        case 1:
          //$giftype=check_gifcartoon($imgsrc);
          //if($giftype){
             header('Content-Type:image/gif');
            $image_wp=imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromgif($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            //75代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $imgdst, $quality);
            imagedestroy($image_wp);
          //}
           
          break;
        case 2:
          header('Content-Type:image/jpeg');
          $image_wp=imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefromjpeg($imgsrc);
          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          imagejpeg($image_wp, $imgdst,$quality);
          imagedestroy($image_wp);
          break;
        case 3:
         header('Content-Type:image/png');
          $image_wp=imagecreatetruecolor($new_width, $new_height);
          $image = imagecreatefrompng($imgsrc);
          imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
          imagejpeg($image_wp, $imgdst,$quality);
          imagedestroy($image_wp);
          break;
      }

        //创建目标图像
        $dst_im = imagecreatetruecolor($imgWidth, $imgHeight);
        $white = imagecolorallocate($dst_im, 255, 255, 255);
        imagefill($dst_im, 0, 0, $white);

        //源图像
        $src_im = @imagecreatefromjpeg($imgdst);

        $height = $width = 0;
        if($new_width > $imgWidth) {
            $width = ($new_width - $imgWidth) / 2;
        }
        if($new_height > $imgHeight) {
            $height = ($new_height-$imgHeight) / 2;
        }
        imagecopy( $dst_im, $src_im, 0, 0, $width, $height, $imgWidth, $imgHeight );
        imagejpeg($dst_im, $imgdst);
        imagedestroy($dst_im);
    }

如有疑问可以联系wechat: gratitude369

相关文章

  • php图片压缩居中裁剪

    用于业务需求,需要进行图片处理,图片压缩生成缩略图,超过规定尺寸要居中裁剪,现封装一个图片处理方法,直接调用即可:...

  • 裁剪微信分享缩略图片(长缩略图取中裁剪)

    有时分享出去的图片如果过长,就会导致缩略图的正方形图片呈现的效果是被压缩的,一般才用居中裁剪的方式,将裁剪后的图作...

  • java实现图片压缩

    采用ImageIO实现图片的裁剪与压缩,分为裁剪与压缩两个模块,先裁剪再压缩 效果还不错,11M左右的图片能够在2...

  • 图片裁剪压缩

    方法1 方法2 也可以使用系统自带的方法,对图片进行降噪: NSData *data=UIImageJPEGR...

  • 前端实现图片裁剪和压缩

    前端实现图片的裁剪和压缩1、压缩图片drawImage,压缩图片长宽或者质量参数来实现压缩 2、图片base64转...

  • 图片的上传压缩等学习

    1.App图片压缩裁剪原理 主要讲原理,流程解释 2.Android MediaStore裁剪大图片 Inten...

  • 移动端添加图片裁剪的坑

    本文关键:阻止冒泡、canvas压缩、canvas裁剪、生成黑色图片。 最近项目需要在添加图片时实现裁剪功能,虽然...

  • iOS 图片处理

    本文主要列出简单的图片处理代码,如:压缩图形大小,裁剪图片,添加文字水印,添加图片水印,压缩图片大小并保存。 本来...

  • Glide设置RoundedCorners & CenterCr

    问题 需要对网络图片进行圆角裁剪和居中裁剪 只设置圆角,如下: 只设置居中剪裁 两者同时设置,则最后设置的才会生效...

  • 图片处理

    为了适应各种分辨率的显示和速度快,需要将图片进行压缩或者裁剪 前端图片处理 cropperjs可以实现图片的裁剪和...

网友评论

      本文标题:php图片压缩居中裁剪

      本文链接:https://www.haomeiwen.com/subject/ktytictx.html