自定义裁剪区域的缩略图裁剪功能

目标:识别裁剪图片大小与原图大小最大限度裁剪原图指定部分的功能

我将常用的裁剪图片概括为此6种情况,正方形可近似看作长方形,程序逻辑:根据裁剪尺寸和原图尺寸判定横图裁剪或竖图裁剪->根据自定义参数计算保留区域->执行裁剪

代码如下:

<?php

/**
 * 生成缩略图,支持jpeg,png,gif,bmp
 * @author lgt 20120929
 */
class Thumb{
  private $input;
  private $width;
  private $height;
  private $output; // 文件目录
  private $cut;
  private $pathinfo;
  private $imageinfo;
  private $image;
  private $newimg; // 文件名称
  public $newfile;
  function __construct ($input, $width, $height, $cut = 0, $output = false){
    $this->input = $input;
    $this->width = $width;
    $this->height = $height;
    $this->output = $output;
    $this->cut = $cut;
    if (file_exists($input) == false) {
      die('thumb file "' . $input . '" not exist');
    }
    $this->pathinfo = pathinfo($input); 
    if ($this->output == false) {
      $this->output = str_replace('\\', '/', $this->pathinfo['dirname']) . '/';
    }
    if (is_writeable($this->output) == false) {
      die('thumb dir "' . $this->output . '" is not writeable');
    }
    $this->newfile = $this->pathinfo['filename'] . '_' . $this->width . '_' .$this->height . '.' . $this->pathinfo['extension'];
    if (file_exists($this->output . $this->newfile)) {
      return;
    }
    $this->imageinfo = getimagesize($input);
    $type = $this->getType($input); // 获取图片类型
    eval('$this->image=imagecreatefrom' . $type . '($input);'); // 拷贝源文件副本
    $this->newimg = imagecreatetruecolor($width, $height);
    $bgColor = imagecolorallocate($this->newimg, 255, 255, 255);
    imagefill($this->newimg, 0, 0, $bgColor);
    $this->setImage($width, $height); // 进行裁剪
    $this->saveImage($type); // 保存图片
  }

  /**
   * 获取图片类型
   * 
   * @param string $file          
   */
  private function getType ($file)
  {
    $file = fopen($file, "rb");
    $bin = fread($file, 2); // 只读2字节
    fclose($file);
    $strInfo = @unpack("C2chars", $bin);
    $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
    switch ($typeCode) {
      case 7173:
        return 'gif';
        break;
      case 6677:
        return 'bmp';
        break;
      case 13780:
        return 'png';
        break;
      case 255216:
      default:
        return 'jpeg';
    }
  }

  /**
   * 生成缩略图
   * @param $width
   * @param  $height
   */
  private function setImage ($width, $height)
  {
    $proportion = ($this->imageinfo[0] / $this->imageinfo[1] - $width / $height); // $proportion>=0则按高度,$proportion<0则按宽度
    if ($this->cut == 0) { // 不裁剪
      if (imagecopyresampled($this->newimg, $this->image, 0, 0, 0, 0, $width,$height, $this->imageinfo[0], $this->imageinfo[1]) == false) {
        die('resize failed');
      }
    } elseif ($this->cut == 1) { // 从左端裁剪
      if ($proportion < 0) {
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, 0, 0, $width, $height, $width * ($this->imageinfo[0] / $width), $height * ($this->imageinfo[0] / $width)) == false) {
          die('resize failed');
        }
      } else {
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, 0, 0, $width, $height, $width * ($this->imageinfo[1] / $height), $height * ($this->imageinfo[1] / $height)) == false) {
          die('resize failed');
        }
      }
    } elseif ($this->cut == 2) { // 从中间裁剪
      if ($proportion < 0) {
        $y = ($this->imageinfo[1] - $height * ($this->imageinfo[0] / $width)) / 2;
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, 0, $y, $width,  $height, $width * ($this->imageinfo[0] / $width),  $height * ($this->imageinfo[0] / $width)) == false) {
          die('resize failed');
        }
      } else {
        $x = ($this->imageinfo[0] - $width * ($this->imageinfo[1] / $height)) / 2;
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, $x, 0, $width, $height, $width * ($this->imageinfo[1] / $height), $height * ($this->imageinfo[1] / $height)) == false) {
          die('resize failed');
        }
      }
    } elseif ($this->cut == 3) { // 从右端裁剪
      if ($proportion < 0) {
        $y = $this->imageinfo[1] - $height * ($this->imageinfo[0] / $width);
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, 0, $y, $width,$height, $width * ($this->imageinfo[0] / $width), $height * ($this->imageinfo[0] / $width)) == false) {
          die('resize failed');
        }
      } else {
        $x = $this->imageinfo[0] - $width * ($this->imageinfo[1] / $height);
        if (imagecopyresampled($this->newimg, $this->image, 0, 0, $x, 0, $width, $height, $width * ($this->imageinfo[1] / $height),  $height * ($this->imageinfo[1] / $height)) == false) {
          die('resize failed');
        }
      }
    }
  }

  /**
   * 保存图片
   */
  private function saveImage ($type)
  {
    eval('image'.$type.'($this->newimg,"'.$this->output.$this->newfile.'");');
    imagedestroy($this->newimg);
    imagedestroy($this->image);
  }
  /**
   * 获取图片路径
   */
  public function getFile ()
  {
    return $this->output.$this->newfile;
  }
}
$gif = new Thumb('test.png', 2000, 3000, 2);
exit($gif->getFile());
?>
特点:

原图小于裁剪尺寸会将原图拉大后裁剪

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值