PHP全功能无变形图片裁剪操作类与用法示例


Posted in PHP onJanuary 10, 2017

本文实例讲述了PHP全功能无变形图片裁剪操作类与用法。分享给大家供大家参考,具体如下:

如果需要图片裁剪,或生成缩略图,这个基本上没有问题,全部需要的功能都在里面,全部都是无变形的。

这里我们划分为四种模式:

1. 任意图片裁剪到指定大小,超过的,剧中裁剪,并且最大化缩放,不够的拉伸,
2. 超过的剧中裁剪,而不足的,不拉伸,即只缩小,不放大裁剪,会产生补白,可以用png透明度来消除
3. 保留全部图片信息。不裁剪,只缩放,不足的补白。
4. 保留全部图片信息。不裁剪,只缩放,不足的不补白,产生的图片为实际有效像素大小,例如有个图片是600X600 现在要生成 120X100 那么缩放后的实际有效像素为100X100,所以只生成100X100的图片,而第三种模式则生成120X100的大小,并且有补白

代码如下(此处代码通过本站在线工具http://tools.3water.com/code/3water_php_format做了格式化处理):

<?php
/**
* Author : smallchicken
* mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
* mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
* mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
* mode 4 : 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
* 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
*
* 调用方法:
*
* $ic=new ImageCrop('old.jpg','afterCrop.jpg');
* $ic->Crop(120,80,2);
* $ic->SaveImage();
*    //$ic->SaveAlpha();将补白变成透明像素保存
* $ic->destory();
*
*
*/
class ImageCrop {
  var $sImage;
  var $dImage;
  var $src_file;
  var $dst_file;
  var $src_width;
  var $src_height;
  var $src_ext;
  var $src_type;
  function ImageCrop($src_file,$dst_file='') {
    $this->src_file=$src_file;
    $this->dst_file=$dst_file;
    $this->LoadImage();
  }
  function SetSrcFile($src_file) {
    $this->src_file=$src_file;
  }
  function SetDstFile($dst_file) {
    $this->dst_file=$dst_file;
  }
  function LoadImage() {
    list($this->src_width, $this->src_height, $this->src_type) = getimagesize($this->src_file);
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      $this->sImage=imagecreatefromjpeg($this->src_file);
      $this->ext='jpg';
      break;
      case IMAGETYPE_PNG :
      $this->sImage=imagecreatefrompng($this->src_file);
      $this->ext='png';
      break;
      case IMAGETYPE_GIF :
      $this->sImage=imagecreatefromgif($this->src_file);
      $this->ext='gif';
      break;
      default:
      exit();
    }
  }
  function SaveImage($fileName='') {
    $this->dst_file=$fileName ? $fileName : $this->dst_file;
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      imagejpeg($this->dImage,$this->dst_file,100);
      break;
      case IMAGETYPE_PNG :
      imagepng($this->dImage,$this->dst_file);
      break;
      case IMAGETYPE_GIF :
      imagegif($this->dImage,$this->dst_file);
      break;
      default:
      break;
    }
  }
  function OutImage() {
    switch($this->src_type) {
      case IMAGETYPE_JPEG :
      header('Content-type: image/jpeg');
      imagejpeg($this->dImage);
      break;
      case IMAGETYPE_PNG :
      header('Content-type: image/png');
      imagepng($this->dImage);
      break;
      case IMAGETYPE_GIF :
      header('Content-type: image/gif');
      imagegif($this->dImage);
      break;
      default:
      break;
    }
  }
  function SaveAlpha($fileName='') {
    $this->dst_file=$fileName ? $fileName . '.png' : $this->dst_file .'.png';
    imagesavealpha($this->dImage, true);
    imagepng($this->dImage,$this->dst_file);
  }
  function OutAlpha() {
    imagesavealpha($this->dImage, true);
    header('Content-type: image/png');
    imagepng($this->dImage);
  }
  function destory() {
    imagedestroy($this->sImage);
    imagedestroy($this->dImage);
  }
  function Crop($dst_width,$dst_height,$mode=1,$dst_file='') {
    if($dst_file) $this->dst_file=$dst_file;
    $this->dImage = imagecreatetruecolor($dst_width,$dst_height);
    $bg = imagecolorallocatealpha($this->dImage,255,255,255,127);
    imagefill($this->dImage, 0, 0, $bg);
    imagecolortransparent($this->dImage,$bg);
    $ratio_w=1.0 * $dst_width / $this->src_width;
    $ratio_h=1.0 * $dst_height / $this->src_height;
    $ratio=1.0;
    switch($mode) {
      case 1:    // always crop
      if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
        $src_x = (int) (($this->src_width-$tmp_w)/2) ;
        $src_y = (int) (($this->src_height-$tmp_h)/2) ;
        imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
        imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      } else {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
        $src_x = (int)($tmp_w - $dst_width) / 2 ;
        $src_y = (int)($tmp_h - $dst_height) / 2 ;
        imagecopy($this->dImage, $tmp_img, 0,0,$src_x,$src_y,$dst_width,$dst_height);
        imagedestroy($tmp_img);
      }
      break;
      case 2:    // only small
      if($ratio_w < 1 && $ratio_h < 1) {
        $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
        $src_x = (int) ($this->src_width-$tmp_w)/2 ;
        $src_y = (int) ($this->src_height-$tmp_h)/2 ;
        imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
        imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      } elseif($ratio_w > 1 && $ratio_h > 1) {
        $dst_x = (int) abs($dst_width - $this->src_width) / 2 ;
        $dst_y = (int) abs($dst_height -$this->src_height) / 2;
        imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
      } else {
        $src_x=0;
        $dst_x=0;
        $src_y=0;
        $dst_y=0;
        if(($dst_width - $this->src_width) < 0) {
          $src_x = (int) ($this->src_width - $dst_width)/2;
          $dst_x =0;
        } else {
          $src_x =0;
          $dst_x = (int) ($dst_width - $this->src_width)/2;
        }
        if( ($dst_height -$this->src_height) < 0) {
          $src_y = (int) ($this->src_height - $dst_height)/2;
          $dst_y = 0;
        } else {
          $src_y = 0;
          $dst_y = (int) ($dst_height - $this->src_height)/2;
        }
        imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,$src_x,$src_y,$this->src_width,$this->src_height);
      }
      break;
      case 3:    // keep all image size and create need size
      if($ratio_w > 1 && $ratio_h > 1) {
        $dst_x = (int)(abs($dst_width - $this->src_width )/2) ;
        $dst_y = (int)(abs($dst_height- $this->src_height)/2) ;
        imagecopy($this->dImage, $this->sImage, $dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
      } else {
        $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
        $dst_x = (int)(abs($tmp_w -$dst_width )/2) ;
        $dst_y = (int)(abs($tmp_h -$dst_height)/2) ;
        imagecopy($this->dImage, $tmp_img, $dst_x,$dst_y,0,0,$tmp_w,$tmp_h);
        imagedestroy($tmp_img);
      }
      break;
      case 4:    // keep all image but create actually size
      if($ratio_w > 1 && $ratio_h > 1) {
        $this->dImage = imagecreatetruecolor($this->src_width,$this->src_height);
        imagecopy($this->dImage, $this->sImage,0,0,0,0,$this->src_width,$this->src_height);
      } else {
        $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
        $tmp_w = (int)($this->src_width * $ratio);
        $tmp_h = (int)($this->src_height * $ratio);
        $this->dImage = imagecreatetruecolor($tmp_w ,$tmp_h);
        imagecopyresampled($this->dImage,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
      }
      break;
    }
  }
  // end Crop
}
?>
PHP 相关文章推荐
php 正则表达式小结
Aug 31 PHP
PHP 实现explort() 功能的详解
Jun 20 PHP
百度站点地图(百度sitemap)生成方法分享
Jan 09 PHP
ThinkPHP3.1数据CURD操作快速入门
Jun 19 PHP
yiic命令时提示“php.exe”不是内部或外部命令的解决方法
Dec 18 PHP
微信开发之网页授权获取用户信息(二)
Jan 08 PHP
Symfony2开发之控制器用法实例分析
Feb 05 PHP
PHP下的浮点运算不准的解决方法
Oct 27 PHP
php指定长度分割字符串str_split函数用法示例
Jan 30 PHP
PHP中单例模式与工厂模式详解
Feb 17 PHP
PHP实现微信申请退款功能
Oct 01 PHP
Laravel 验证码认证学习记录小结
Dec 20 PHP
php实现36进制与10进制转换功能示例
Jan 10 #PHP
php获取当前url地址的方法小结
Jan 10 #PHP
PHP实现接收二进制流转换成图片的方法
Jan 10 #PHP
ThinkPHP 模板substr的截取字符串函数详解
Jan 09 #PHP
php 访问oracle 存储过程实例详解
Jan 08 #PHP
微信开发之php表单微信中自动提交两次问题解决办法
Jan 08 #PHP
yii2-GridView在开发中常用的功能及技巧总结
Jan 07 #PHP
You might like
PHPMyadmin 配置文件详解(配置)
2009/12/03 PHP
ThinkPHP在新浪SAE平台的部署实例
2014/10/31 PHP
PHP7 windows支持
2021/03/09 PHP
js单例模式详解实例
2013/11/21 Javascript
Javascript的严格模式strict mode详细介绍
2014/06/06 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
js控制输入框获得和失去焦点时状态显示的方法
2015/01/30 Javascript
JQuery显示隐藏DIV的方法及代码实例
2015/04/16 Javascript
ES6学习之变量的解构赋值
2017/02/12 Javascript
基于Vue2.0的分页组件
2017/03/16 Javascript
JavaScript实现图片无缝滚动效果
2017/07/07 Javascript
node跨域请求方法小结
2017/08/25 Javascript
vue与TypeScript集成配置最简教程(推荐)
2017/10/17 Javascript
微信小程序实现简单input正则表达式验证功能示例
2017/11/30 Javascript
vue生命周期实例小结
2018/08/15 Javascript
全面了解JavaScript的作用域链
2019/04/03 Javascript
layer提示框添加多个按钮选择的实例
2019/09/12 Javascript
JavaScript中继承原理与用法实例入门
2020/05/09 Javascript
谈谈JavaScript中的函数
2020/09/08 Javascript
mapboxgl实现带箭头轨迹线的代码
2021/01/04 Javascript
深入讲解Python中面向对象编程的相关知识
2015/05/25 Python
Python计算字符宽度的方法
2016/06/14 Python
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
2017/06/23 Python
Python paramiko模块使用解析(实现ssh)
2019/08/30 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
2019/09/11 Python
Flask框架 CSRF 保护实现方法详解
2019/10/30 Python
浅谈HTML5 defer和async的区别
2016/06/07 HTML / CSS
HTML5移动端开发遇见的东西
2019/10/11 HTML / CSS
HTML5获取当前地理位置并在百度地图上展示的实例
2020/07/10 HTML / CSS
2019年c语言经典面试题目
2016/08/17 面试题
自动化专业个人求职信范文
2013/11/29 职场文书
药剂专业学生求职信范文
2013/12/28 职场文书
绩效工资分配方案
2014/01/18 职场文书
大二自我鉴定
2014/01/31 职场文书
mysql如何配置白名单访问
2021/06/30 MySQL
详解在OpenCV中如何使用图像像素
2022/03/03 Python