<?php /** * 图像裁剪 * 需要系统安装 php-imagick 扩展 */ //ini_set ( "memory_limit", "80M" ); // 请求地址比如 http://xxx.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png // nginx重写规则 rewrite /s/(.*)/(\d+)x(\d+)-(\d)/(.*) /s/resize.php?site=$1&width=$2&height=$3&mode=$4&path=$5 last; // 测试 http://localhost/resize.php?site=imgs&width=300&height=300&mode=1&path=22.jpg // 图片放在resize.php同级的 ./imgs/ 目录下 访问后会创建 ./cache/ 文件夹 // 后期可以考虑 $path = trim ( $_GET ['path'] ); $mode = intval ( $_GET ['mode'] ); $site = trim ( $_GET ['site'] ); $width = intval ( $_GET ['width'] ); $height = intval ( $_GET ['height'] ); //访问文件是否存在 $now_dir = dirname ( __FILE__ ); //缩略图路径(***路径名cache可选修改***) $zip_dir = "{$now_dir}/cache/{$site}/{$width}x{$height}-{$mode}/"; //原始图片存放目录 $img_dir = "{$now_dir}/{$site}/"; //原始图片文件完整路径 $big_file = "{$img_dir}{$path}"; //缩略图文件完整路径 $save_path = "{$zip_dir}{$path}"; //缩略图存在直接返回,不存在去检测原图 if (file_exists ( $save_path )){ header ( 'Content-Type: image/jpeg' ); //header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' ); //读取文件的修改时间 header ('Modified:'.gmdate("D, d M Y H:i:s",filemtime($save_path)).' GMT'); //输出图片流 echo file_get_contents ( $save_path ); return true; }else if(! file_exists($big_file)){ header ( 'HTTP/1.1 404 Not Found' ); exit (); } //检测并创建保存目录 if (! file_exists ( $zip_dir )){ //wpx_mkdir ( $zip_dir ); createDir($zip_dir); } $target_width = $width; $target_height = $height; $new_width = $target_width; $new_height = $target_height; $image = new Imagick ( $big_file ); list ( $orig_width, $orig_height, $type, $attr ) = getimagesize ( $big_file ); if ($mode == "0") { //等比缩放图像 $new_height = $orig_height * $new_width / $orig_width; if ($new_height > $target_height) { $new_width = $orig_width * $target_height / $orig_height; $new_height = $target_height; } } else if ($mode == "2") { // 放大并裁剪图像 $desired_aspect = $target_width / $target_height; $orig_aspect = $orig_width / $orig_height; if ($desired_aspect > $orig_aspect) { $trim = $orig_height - ($orig_width / $desired_aspect); $image->cropImage ( $orig_width, $orig_height - $trim, 0, $trim / 2 ); error_log ( "HEIGHT TRIM $trim" ); } else { $trim = $orig_width - ($orig_height * $desired_aspect); $image->cropImage ( $orig_width - $trim, $orig_height, $trim / 2, 0 ); } } // 处理图片 $image->resizeImage ( $new_width, $new_height, imagick::FILTER_LANCZOS, 1 ); // 保存处理后的图片 $image->writeImage ( $save_path ); //标记返回头信息 header ( 'Content-Type: image/jpeg' ); header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' ); echo file_get_contents ( $save_path ); return true; // 循环生成目录(不使用) function wpx_mkdir($dir, $mode = 0777) { if (is_dir ( $dir ) || @mkdir ( $dir, $mode )) return true; if (! wpx_mkdir ( dirname ( $dir ), $mode )) return false; return @mkdir ( $dir, $mode ); } //生成目录 function createDir($path){ if (!file_exists($path)){ createDir(dirname($path)); mkdir($path, 0777) or die('create dir fail, NND'); } } ?>
大搬运并修改,原地址
https://www.markdream.com/technologies/programs/how-to-build-a-resizing-image-server-with-imagick.shtml