PHP图片裁剪方案 发布日期:2017-08-01 17:46:09     博主推荐★
<?php 
    
class OPIMG{
private $_old_img_path = null;
private $_new_width = 0;//新图片的宽度
private $_new_height = 0;//新图片的高度
private $_old_width = 0;//原图片的宽度
private $_old_height = 0;//原图片的高度
private $_start_x = 0;
private $_start_y = 0;
  /**
*description 图片操作类
*/
public function __construct(){
}
/**
  * description 创建等比例的图片
  * @param sting $imgsrc 图片路径 
  * @param string $imgdst 压缩后保存路径
  * @param int $max_width 最大宽度
  * @param int $max_height 最大高度
  */
public function createScalingImg($imgsrc,$imgdst,$max_width,$max_height){
$this->isExists($imgsrc);
list($this->_old_width,$this->_old_height,$type) = getimagesize($imgsrc);
$width_rate = $max_width/$this->_old_width;
$height_rate = $max_height/$this->_old_height;
$min_rate = $width_rate>$height_rate?$height_rate:$width_rate;
$this->_new_width = $this->_old_width * $min_rate; 
$this->_new_height =$this->_old_height * $min_rate;
$this->createThumb($imgsrc,$imgdst,$type);
}
/**
  * description 创建固定的图片
  * @param sting $imgsrc 图片路径 
  * @param string $imgdst 压缩后保存路径
  * @param int $width 宽度
  * @param int $height 高度
  */
public function createFixedImg($imgsrc,$imgdst,$width,$height){
$this->isExists($imgsrc);
list($this->_old_width,$this->_old_height,$type) = getimagesize($imgsrc); 
$this->_new_width = $width; 
$this->_new_height =$height;
$this->createThumb($imgsrc,$imgdst,$type);
}
/**
  *网站背景裁剪
  * @param int $part 多少部分
  */
public function createMutilImg($imgsrc,$imgdst,$part){
$this->isExists($imgsrc);
list($this->_old_width,$this->_old_height,$type) = getimagesize($imgsrc); 
$this->_new_width  = $this->_old_width; 
$this->_new_height = $this->_old_height/$part;
for($i=1;$i<$part;$i++){
$this->_start_y = $this->_new_height*$i;
$index = strripos($imgdst,'.');
$part_imgdst = substr($imgdst,0,$index)."_$i".substr($imgdst,$index);
                $this->createCrop($imgsrc,$part_imgdst,$type);
}
}
/** 
* desription 压缩图片 
* @param sting $imgsrc 图片路径 
* @param string $imgdst 压缩后保存路径
* @param int 图片类型
*/
private function createThumb($imgsrc,$imgdst,$type){
  $this->_old_img_path = $imgsrc;
  switch($type){ 
case 1: 
//header('Content-Type:image/gif');
$image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
$image = imagecreatefromgif($imgsrc); 
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $this->_new_width, $this->_new_height, $this->_old_width, $this->_old_height); 
imagejpeg($image_wp, $imgdst,100); 
imagedestroy($image_wp);
                break; 
case 2: 
  //header('Content-Type:image/jpeg'); 
  $image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
  $image = imagecreatefromjpeg($imgsrc); 
  imagecopyresampled($image_wp, $image, 0, 0,0, 0, $this->_new_width, $this->_new_height, $this->_old_width, $this->_old_height); 
  imagejpeg($image_wp, $imgdst,100); 
  imagedestroy($image_wp); 
  break; 
case 3: 
  //header('Content-Type:image/png'); 
  $image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
  $image = imagecreatefrompng($imgsrc); 
  imagecopyresampled($image_wp, $image, 0, 0,0, 0, $this->_new_width, $this->_new_height, $this->_old_width, $this->_old_height); 
  imagejpeg($image_wp, $imgdst,100); 
  imagedestroy($image_wp); 
  break; 
  } 
} 
/** 
* desription 裁剪图片 
* @param sting $imgsrc 图片路径 
* @param string $imgdst 压缩后保存路径
* @param int 图片类型
*/
private function createCrop($imgsrc,$imgdst,$type){
  $this->_old_img_path = $imgsrc;
  switch($type){ 
case 1: 
//header('Content-Type:image/gif');
$image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
$image = imagecreatefromgif($imgsrc); 
imagecopy($image_wp, $image, 0, 0, $this->_start_x, $this->_start_y, $this->_new_width, $this->_new_height); 
imagejpeg($image_wp, $imgdst,100); 
imagedestroy($image_wp);
                break; 
case 2: 
  //header('Content-Type:image/jpeg'); 
  $image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
  $image = imagecreatefromjpeg($imgsrc); 
  imagecopy($image_wp, $image, 0, 0, $this->_start_x, $this->_start_y, $this->_new_width, $this->_new_height); 
  imagejpeg($image_wp, $imgdst,100); 
  imagedestroy($image_wp); 
  break; 
case 3: 
  //header('Content-Type:image/png'); 
  $image_wp=imagecreatetruecolor($this->_new_width, $this->_new_height); 
  $image = imagecreatefrompng($imgsrc); 
  imagecopy($image_wp, $image, 0, 0, $this->_start_x, $this->_start_y, $this->_new_width, $this->_new_height); 
  imagejpeg($image_wp, $imgdst,100); 
  imagedestroy($image_wp); 
  break; 
  } 
} 
public function removeOldImg(){
//删除原图片
unlink($this->_old_img_path);
}
private function isExists($imgsrc){
if(!file_exists($imgsrc)){
echo "imgsrc is not found\n";
exit;
}
}
}
//实例化对象
    $img_op = new OPIMG();
//创建固定大小的图片
$img_op->createFixedImg('./share.gif',"./share_fixed.gif",100,100);
//按照等比缩放图片
$img_op->createScalingImg('./share.gif',"./share_scaling.gif",100,100);
//用于纵向裁剪图片
$img_op->createMutilImg('./share.gif',"./share_mutli.gif",4);
echo "create ok!\n";
//删除原图片
$img_op->removeOldImg();
echo "remove ok!\n";
?>


博文地址:https://blog.ahamu.cn/blog/detail.html?id=230
   
推荐文章
  • 1
    sysbench
    2020/07/08
  • 2
    phper转java记录篇-spring boot
    2020/06/10
  • 3
    thinkphp5.0使用路由之后,post请求的
    2020/05/19
  • 4
    springboot单元测试aop失效
    2020/05/15
  • 5
    脑海中的JVM
    2020/05/12
  • 6
    IDEA搜索插件时显示search results
    2020/05/12
  • 7
    spring boot 配置加载源码查找
    2020/04/20
  • 8
    通过javap命令分析java汇编指令
    2020/04/16
  • 9
    IDEA小知识:查看JVM内存使用情况的步骤
    2020/04/16
  • 10
    springboot-加载自定义的properti
    2020/04/14
  • 11
    Jenkins执行shell脚本无法启动子进程解决
    2020/04/03
  • 12
    mac idea激活找专业的
    2020/04/02
  • 13
    Jenkins + DockerSwarm 实现弹
    2020/03/31
  • 14
    mac swarm学习过程
    2020/03/31
  • 15
    spring cloud
    2020/03/18
  • 16
    JAVA开发中遇到的问题记录002
    2020/03/12
  • 17
    JAVA开发中遇到的问题记录001
    2020/03/07
  • 18
    php -i查看信息
    2020/02/23
  • 19
    phpStorm中使用xdebug工具调试dock
    2019/12/09
  • 20
    讲的比较好的B+树执行原理的文章
    2019/12/09
最喜标签
NYOJ 面试 AJAX ping CentOS 灰度算法 YII2