php图片自动裁剪工具,php图片自动裁剪工具,解决图片变形问题,缩略图问题

class ImageCut {

public $path = '';

public $key = '';

public function __construct() {

}

public function set_image($path) {

$this->path = $path;

}

public function header_image(){

header('Content-type:image/jpeg');

}

public function header_304(){

//304缓存

$this->key = $etag = md5_file($this->path);

$last_mod = filemtime($this->path);

header('ETag:'.$etag);

header('Last-Modified:'.gmdate('D, d M Y H:i:s', $last_mod) . ' GMT');

if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $last_mod)

|| (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $last_mod)

|| (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)) {

header("HTTP/1.1 304 Not Modified");

return true;

} else {

return false;

}

}

public function get_data($width,$height){

if($this->path) {

$path = $this->path;

} else {

trigger_error('unset path');

}

if(!file_exists($path)) {

trigger_error('file not exist');

}

$img_type = exif_imagetype($path);

$old_size = getimagesize($path);

$old_width = $old_size[0];

$old_height = $old_size[1];

if($width==0 && $height==0) {

$width = $old_width;

$height = $old_height;

} else if($width==0) {

if($height>$old_height) {

$height = $old_height;

}

$width = $height/$old_height * $old_width;

} else if($height==0) {

if($width>$old_width) {

$width = $old_width;

}

$height = $width/$old_width * $old_height;

}

$dst_wh = $width/$height;

if($old_height>($old_width/$dst_wh)) {

$src_width = $old_width;

$src_height = $old_width/$dst_wh;

$src_x = 0;

$src_y = ($old_height-$src_height)/2;

} else {

$src_height = $old_height;

$src_width = $old_height*$dst_wh;

$src_y = 0;

$src_x = ($old_width-$src_width)/2;

}

switch($img_type) {

case IMAGETYPE_JPEG:

$img = imagecreatefromjpeg($path);

break;

case IMAGETYPE_PNG:

$img = imagecreatefrompng($path);

break;

case IMAGETYPE_GIF:

$img = imagecreatefromgif($path);

break;

}

if($src_width

$width = $src_width;

}

if($src_height

$height = $src_height;

}

//重新画

$newimage = imagecreatetruecolor($width,$height);

imagecopyresampled($newimage,$img,0,0,$src_x,$src_y,$width,$height,$src_width,$src_height);

//开始缓存图片

ob_start();

imagejpeg($newimage,false,90);

$data = ob_get_contents();

ob_end_clean();

imagedestroy($img);

imagedestroy($newimage);

return $data;

}

public function show($width,$height){

$this->header_image();

$if_304 = $this->header_304();

if($if_304) {

return;

}

echo $this->get_data($width, $height);

}

}

$cut = new ImageCut();

$cut->set_image('1.jpg');//path可以通过参数传过来

$cut->show(200,200);//随意设置宽高,可以通过参数传过来,

?>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值