PHP plus nginx implementation of dynamic cropping picture Scheme _php Tutorial

Source: Internet
Author: User
Tags browser cache
Wrote a long time ago 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 generated image support, support for the picture 3 mode switch, in the portal automatically cut the size of the picture, reduce the server bandwidth, in theory should also meet the needs of the business, picture clipping using the Imagick components.

first, the idea of reproduction:
1, first write the request server generated 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 dynamic clipping PHP script
Copy CodeThe code is as follows:
/**
* 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);
}
}

$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);
}

Three, nginx.conf configuration
Copy CodeThe code is as follows:
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;
Include Fastcgi_params;
}

}

PS: At the end of the article I would like to highlight a bit about the browser cache article, whether you are generated by the PHP image, or the use of the Nginx cache generated images, in the PHP code to add a line
Copy CodeThe code is as follows: Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT ');
It's very helpful for you to use a CDN. The result is that the first time the client accesses this file, the HTTP status code is 200, and the status code is always 304 after the refresh.

http://www.bkjia.com/PHPjc/739780.html www.bkjia.com true http://www.bkjia.com/PHPjc/739780.html techarticle wrote a long time ago is also about the high-performance PHP picture dynamic cutting scheme article, that article uses the Nginx cache and rewrite implementation, of course, plus CDN, the solution exists a ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.