Previously wrote an article is also about the high-performance PHP picture dynamic cutting scheme, the article uses the Nginx cache and rewrite implementation, of course, coupled with the CDN, the solution has a problem is that the picture is not actually generated, but in the form of a binary cache. If the cache fails then you also need to request PHP to generate it again. If that's what I think of the difference. The use of spare time, the new static generation of image support, support for the picture 3 mode switch, the portal site automatically cut the size of the picture, reduce the server bandwidth, image cropping using the Imagick component. First, the idea of reconstruction: 1, write the request server to generate the picture dynamic script, the main thing is to compare the image to zoom calculation + cropping. 2. Determine the URL rules you want to generate, such as http://www.domain.com/www/300x200-1/test.jpg. 3, the browser to do cache processing. 4, the end. Second, the PHP script to cut the picture dynamically
/**
- * Author Pony_chiang
- * High-performance image cutting scheme
- * Requires Php-imagick extension
- */
- Ini_set ("Memory_limit", "80M");
Request address such as http://yourdomain.com/resize.php?site=www&width=300&height=200&mode=2&path=uploadfile/ Helloworld.png
- Nginx rewrite rule 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-bad Request ');
- Exit ();
- }
if ($mode > 3 | | $mode < 0) {
- Header (' http/1.1-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") {
- Equal to scaled image
- $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") {
- Enlarge and crop an image
- $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);
- } bbs.it-home.org
- }
$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;
Looping through the build directory
- 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);
- }
Copy CodeThree, nginx.conf configuration
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 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;
- Includefastcgi_params;
- }
}
Copy CodeNote, highlighting the article about browser caching, whether it is generated by PHP or not, or using the Nginx cache generated images, add a line in the PHP code
- Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT ');
Copy CodeIt is a great help to use CDN. The first time the client accesses this file's HTTP status code is 200, after the refresh status code has been 304, understand the benefits of the local client cache, save bandwidth Oh. |