一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP加Nginx實現動態裁剪圖片方案

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

2020-06-16 12:03PHP教程網 PHP教程

這篇文章主要介紹了PHP加Nginx實現動態裁剪圖片的方案,使用Imagick組件實現,需要的朋友可以參考下

許久以前寫過一篇也是關于高性能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了。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲成年| 99热精品在线免费观看 | 日本午夜vr影院新入口 | 国产夜趣福利第一视频 | 色哟哟在线视频 | 亚洲精品www久久久久久久软件 | 国产尤物精品视频 | 金莲你下面好紧夹得我好爽 | 国产一卡2卡3卡4卡公司科普 | 国产91精品在线播放 | 4hu永久地域网名入口 | zozzozozozo大| 婷婷综合缴情亚洲五月伊 | 肥胖女性大bbbbbb视频女厕 | 亚洲精品视频网 | gaychinese男男2022 | 我强进了老师身体在线观看 | 国产激情影院 | 精品久久香蕉国产线看观看亚洲 | 精品国产福利在线 | 99精品久久精品一区二区 | 91精品手机国产在线观 | 男同精品视频免费观看网站 | 日本成熟bbxxxxxxxx | 精品无码一区二区三区中文字幕 | 国内精品久久久久影院网站 | 久久亚洲精品AV成人无码 | 国产成人精品男人的天堂538 | 国产自在线拍 | 好姑娘在线完整版视频 | 欧美精品一区二区三区免费播放 | 欧美色图亚洲 | a免费看 | 国产成人咱精品视频免费网站 | 500福利第一导航 | 毛片亚洲毛片亚洲毛片 | 久久婷婷五月综合色精品首页 | 欧洲vodafonewi喷潮 | 初尝黑人巨大h文 | 四虎影院久久 | 涩涩国产精品福利在线观看 |