许久以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章,那文章使用的是nginx Cache和rewrite实现的,当然再加上CDN,那个方案存在一个问题就是图片并没有实际生成,而是以二进制的形式存在缓存中。如果缓存失效了那么还需要请求php再次生成。如果说到区别这是我暂且认为的吧。
利用空余时间,新增了静态生成图片支持,支持对图片3种模式切换,在门户网站自动对图片尺寸进行裁剪,减少服务器带宽,理论上应该也满足了业务的需求吧,图片裁剪使用了Imagick组件。
一、思路再现:
1、先写好请求服务器生成图片动态脚本,主要就是对图片进行等比缩放计算+裁剪。
2、确定你想要生成的url规则,比如http://www.domain.com/www/300×200-1/test.jpg。
3、对浏览器做缓存处理。
4、结%本文来源gaodai#ma#com搞*代#码9网#搞gaodaima代码束。
二、动态裁剪PHP脚本
/**<BR> * Author pony_chiang<BR> * 高性能图像裁剪方案<BR> * 需要php-imagick扩展<BR> */<BR>ini_set ( "memory_limit", "80M" );<br><br>// 请求地址比如 http://yourdomain.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png<BR>// nginx重写规则 rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;<br><br>$path = trim ( $_GET ['path'] );<BR>$mode = intval ( $_GET ['mode'] );<BR>$site = trim ( $_GET ['site'] );<BR>$width = intval ( $_GET ['width'] );<BR>$height = intval ( $_GET ['height'] );<br><br>$site_list = array ('www' => '/mnt/webroot/test/' );<br><br>$orig_dir = dirname ( __FILE__ );<BR>if (! array_key_exists ( $site, $site_list )) {<BR> header ( 'HTTP/1.1 400 Bad Request' );<BR> exit ();<BR>}<br><br>if ($mode > 3 || $mode < 0) {<BR> header ( 'HTTP/1.1 400 Bad Request' );<BR> exit ();<BR>}<br><br>$orig_file = $site_list [$site] . $path;<BR>if (! file_exists ( $orig_file )) {<BR> header ( 'HTTP/1.1 404 Not Found' );<BR> exit ();<BR>}<br><br>$file_ext = '.' . pathinfo ( $path, PATHINFO_EXTENSION );<br><br>$file_name = basename ( $path, $file_ext );<BR>$save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}/{$path}";<BR>$save_dir = dirname ( $save_path );<br><br>if (! file_exists ( $save_dir ))<BR> wpx_mkdir ( $save_dir );<br><br>$target_width = $width;<BR>$target_height = $height;<br><br>$new_width = $target_width;<BR>$new_height = $target_height;<BR>$image = new Imagick ( $orig_file );<BR>list ( $orig_width, $orig_height, $type, $attr ) = getimagesize ( $orig_file );<br><br>if ($mode == "0") {<BR> //等比缩放图像<BR> $new_height = $orig_height * $new_width / $orig_width;<BR> if ($new_height > $target_height) {<BR> $new_width = $orig_width * $target_height / $orig_height;<BR> $new_height = $target_height;<BR> }<BR>} else if ($mode == "2") {<BR> // 放大并裁剪图像<BR> $desired_aspect = $target_width / $target_height;<BR> $orig_aspect = $orig_width / $orig_height;<br><br> if ($desired_aspect > $orig_aspect) {<BR> $trim = $orig_height - ($orig_width / $desired_aspect);<BR> $image->cropImage ( $orig_width, $orig_height - $trim, 0, $trim / 2 );<BR> error_log ( "HEIGHT TRIM $trim" );<BR> } else {<BR> $trim = $orig_width - ($orig_height * $desired_aspect);<BR> $image->cropImage ( $orig_width - $trim, $orig_height, $trim / 2, 0 );<BR> }<BR>}<br><br>$image->resizeImage ( $new_width, $new_height, imagick::FILTER_LANCZOS, 1 );<BR>$image->writeImage ( $save_path );<BR>header ( 'Content-Type: image/jpeg' );<BR>header ( 'Last-Modified: ' . gmdate ( 'D, d M Y H:i:s' ) . ' GMT' );<BR>echo file_get_contents ( $save_path );<BR>return true;<br><br>// 循环生成目录<BR>function wpx_mkdir($dir, $mode = 0777) {<BR> if (is_dir ( $dir ) || @mkdir ( $dir, $mode ))<BR> return true;<BR> if (! wpx_mkdir ( dirname ( $dir ), $mode ))<BR> return false;<BR> return @mkdir ( $dir, $mode );<BR>}<BR>
三、nginx.conf配置
server {<BR> listen 80;<BR> server_name test.yourdomain.com;<BR> root /mnt/webroot/test;<BR> index index.php;<BR> expires 30d;<br><br> location /s {<BR> #只有当没有生成这张图片时才调用动态裁剪<BR> if (!-e $request_filename) {<BR> rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;<BR> break;<BR> }<BR> }<br><br> error_page 404 403 402 500 502 503 504 /404.html;<BR> location = /404.html {<BR> }<br><br> location ~ \.php$ {<BR> fastcgi_pass 127.0.0.1:9000;<BR> fastcgi_index index.php;<BR> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<BR> include fastcgi_params;<BR> }<br><br>}<BR>
PS:在文章的末尾我要特别强调一点是关于浏览器缓存的文章,不管你是否是通过php生成的图片也好,还是使用nginx缓存生成的图片也罢,在php代码中添加一行
header('Last-Modified: ' .gmdate('D, d M Y H:i:s') . ' GMT' );
对你使用CDN有十分莫大的帮助。具体产生的效果就是客户端第一次访问此文件的http状态码是200,刷新后状态码一直都是304了。