在做一个项目,用到自动裁图,因为小编比较懒,懒得手动按标准尺寸裁图,在此之前用的都是php动态缓冲裁图,其对系统资源占用比较严重,每次请求图片裁图都会跑一边裁图,系统负担大、运行效率低。
我的博客前一篇是一个大概构想,经过多天的摸索和不断尝试,实现了我想要的效果。
前期思路是 图片http://yefengs.com/d/123/abc.jpg是真实存在的一张图,倘若我们想要一张400×300像素的缩略图,我们只需访问http://yefengs.com/d/123/abc.jpg_300x400.jpg就会在网站目录/d/123/下创建abc.jpg_300x400.jpg的图片,这个图片是真实存在的,这样的话我们只需在nginx或者Apache里配置,当这个缩略图不存在的时候,生成这张图,当第二次访问时,就直接读取这张图,不需要php参与了。
优点,动态裁切,在做模板时候实现各种尺寸的图片。
缺点,目前只支持.jpg和png,GIF裁切出来没有动画,不知道什么原因。另外一个缺点就是不支持主流的Webp格式。
使用方法,把代码二放到index.php并将index.php文件放到网站目录/d/cutpic/里,若没有该文件夹,可自己创建
若要修改cutpic目录,同是也要修改伪静态配置中的对应路径目录。
伪静态配置
for Nginx
if (!-e $request_filename){
rewrite ^/.*?(d/file/.*.+?)_([\d]+)x([\d]+)\.jpg$ /d/cutpic/?s=$1&w=$2&h=$3 last;
}
For Apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*?(d/file/.*.+?)_([\d]+)x([\d]+)\.jpg$ /d/cutpic/?s=$1&w=$2&h=$3 [L]
代码二
<?php
$imagesPath = $_GET['s'];
$width =(int)$_GET['w'];
$height = (int)$_GET['h'];
$appDepth = '../../';
$imagesPath = $appDepth.$imagesPath;
$outpath = $imagesPath.'_'.$width.'x'.$height.'.jpg';
if( empty($imagesPath) || $width<0 || $height<0 ){
header('HTTP/1.1 404 Not Found');
header('Status:404 Not Found');
exit;
}
if(!file_exists($imagesPath)){
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
exit;
}
if(file_exists($outpath)){
locationTo(str_replace($appDeept,'',$outpath));
}else{
imageCropper($imagesPath,$width,$height,$outpath);
locationTo(str_replace($appDeept,'',$outpath));
}
function imageCropper($source_path, $target_width, $target_height,$save_path){
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
if(!in_array($source_mime,array('image/jpeg','image/png','image/gif'))){
locationTo($source_path);
}
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
if ($source_ratio > $target_ratio){
// image-to-height
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}elseif ($source_ratio < $target_ratio){
//image-to-widht
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}else{
//image-size-ok
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime){
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
$source_image = imagecreatefromjpeg($source_path);
break;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
switch ($source_mime){
case 'image/gif':
imagegif($target_image,$save_path);
break;
case 'image/jpeg':
imagejpeg($target_image,$save_path);
break;
case 'image/png':
imagepng($target_image,$save_path);
break;
default:
imagejpeg($target_image,$save_path);
break;
}
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
function locationTo($src){
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$uri = 'https://';
} else {
$uri = 'http://';
}
$uri .= $_SERVER['HTTP_HOST'];
header('Location: '.$uri.'/'.$src);
exit;
}