_php example of dynamic cropping picture scheme with PHP plus nginx implementation

Source: Internet
Author: User
Tags mkdir

Wrote a long time ago is also about high-performance PHP picture dynamic cropping scheme article, the article is used Nginx cache and rewrite implementation, of course, plus CDN, the solution exists a problem is that the picture is not actually generated, but in the form of a binary cache. If the cache fails then you will need to request PHP to generate it again. If I say the difference, I think for the moment.
The use of free time, new static generated picture support, support to the picture 3 mode switch, in the portal automatically cut picture size, reduce server bandwidth, theoretically should also meet the needs of the business, the picture is cropped using the Imagick component.

One, the idea reappearance:
1, first write good request server to generate picture dynamic script, is mainly to the picture and so on than scaling calculation + cropping.
2, make sure you want to generate the URL rules, such as http://www.domain.com/www/300x200-1/test.jpg.
3, the browser do cache processing.
4, end.
Ii. dynamic cropping of PHP scripts

Copy Code code as follows:

/**
* Author Pony_chiang
* High-performance image clipping scheme
* Need Php-imagick Extension
*/
Ini_set ("Memory_limit", "80M");

Request address such as Yun_qi_img/helloworld.png
Nginx rewrite rules 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 Request ');
Exit ();
}

if ($mode > 3 | | $mode < 0) {
Header (' http/1.1 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") {
Scaling image with equal ratio
$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 the 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;

Loop Generation 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);
}

Third, nginx.conf configuration

Copy Code code 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;
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 emphasize a little bit about browser caching articles, whether you are generated through PHP or a picture, or using the Nginx cache generated pictures, add a line in the PHP code
Copy Code code as follows:
Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT ');

It's very helpful for you to use CDN. The result is that the first time the client accesses this file, the HTTP status code is 200, and the status code is 304 after the refresh.

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.