PHP加Nginx實現動態裁剪圖片方案_PHP教程

來源:互聯網
上載者:User
許久以前寫過一篇也是關於高效能PHP圖片動態裁剪方案的文章,那文章使用的是nginx Cache和rewrite實現的,當然再加上CDN,那個方案存在一個問題就是圖片並沒有實際產生,而是以二進位的形式存在緩衝中。如果緩衝失效了那麼還需要請求php再次產生。如果說到區別這是我暫且認為的吧。
利用空餘時間,新增了靜態產生圖片支援,支援對圖片3種模式切換,在門戶網站自動對圖片尺寸進行裁剪,減少伺服器頻寬,理論上應該也滿足了業務的需求吧,圖片裁剪使用了Imagick組件。

一、思路再現:
1、先寫好請求伺服器產生圖片動態指令碼,主要就是對圖片進行等比縮放計算+裁剪。
2、確定你想要產生的url規則,比如http://www.domain.com/www/300×200-1/test.jpg。
3、對瀏覽器做緩衝處理。
4、結束。
二、動態裁剪PHP指令碼
複製代碼 代碼如下:
/**
* Author pony_chiang
* 高效能映像裁剪方案
* 需要php-imagick擴充
*/
ini_set ( "memory_limit", "80M" );

// 請求地址比如 http://yourdomain.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/helloworld.png
// nginx重寫規則 rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;

$path = trim ( $_GET ['path'] );
$mode = intval ( $_GET ['mode'] );
$site = trim ( $_GET ['site'] );
$width = intval ( $_GET ['width'] );
$height = intval ( $_GET ['height'] );

$site_list = array ('www' => '/mnt/webroot/test/' );

$orig_dir = dirname ( __FILE__ );
if (! array_key_exists ( $site, $site_list )) {
header ( 'HTTP/1.1 400 Bad Request' );
exit ();
}

if ($mode > 3 || $mode < 0) {
header ( 'HTTP/1.1 400 Bad Request' );
exit ();
}

$orig_file = $site_list [$site] . $path;
if (! file_exists ( $orig_file )) {
header ( 'HTTP/1.1 404 Not Found' );
exit ();
}

$file_ext = '.' . pathinfo ( $path, PATHINFO_EXTENSION );

$file_name = basename ( $path, $file_ext );
$save_path = "{$orig_dir}/{$site}/{$width}x{$height}-{$mode}/{$path}";
$save_dir = dirname ( $save_path );

if (! file_exists ( $save_dir ))
wpx_mkdir ( $save_dir );

$target_width = $width;
$target_height = $height;

$new_width = $target_width;
$new_height = $target_height;
$image = new Imagick ( $orig_file );
list ( $orig_width, $orig_height, $type, $attr ) = getimagesize ( $orig_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 );
}

三、nginx.conf配置
複製代碼 代碼如下:
server {
listen 80;
server_name test.yourdomain.com;
root /mnt/webroot/test;
index index.php;
expires 30d;

location /s {
#只有當沒有產生這張圖片時才調用動態裁剪
if (!-e $request_filename) {
rewrite ^([^\.]*)/s/(.*)/(\d+)x(\d+)-(\d)/(.*) $1/s/resize.php?site=$2&width=$3&height=$4&mode=$5&path=$6 last;
break;
}
}

error_page 404 403 402 500 502 503 504 /404.html;
location = /404.html {
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

PS:在文章的末尾我要特彆強調一點是關於瀏覽器緩衝的文章,不管你是否是通過php產生的圖片也好,還是使用nginx緩衝產生的圖片也罷,在php代碼中添加一行
複製代碼 代碼如下:header('Last-Modified: ' .gmdate('D, d M Y H:i:s') . ' GMT' );
對你使用CDN有十分莫大的協助。具體產生的效果就是用戶端第一次訪問此檔案的http狀態代碼是200,重新整理後狀態代碼一直都是304了。

http://www.bkjia.com/PHPjc/739780.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/739780.htmlTechArticle許久以前寫過一篇也是關於高效能PHP圖片動態裁剪方案的文章,那文章使用的是nginx Cache和rewrite實現的,當然再加上CDN,那個方案存在一個...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.