This article describes how to use the Imagick component to dynamically crop images in PHP and Nginx. For more information, see
This article describes how to use the Imagick component to dynamically crop images in PHP and Nginx. For more information, see
I wrote an article about the high-performance PHP Image Dynamic cropping solution a long time ago. The article uses nginx Cache and rewrite for implementation. Of course, with CDN, one problem with that solution is that images are not actually generated, but cached in binary form. If the cache is invalid, You need to request php to generate it again. The difference is what I think for the moment.
With the free time, the support for generating static images is added. You can switch between the three image modes and crop the image size on the portal to reduce the server bandwidth, in theory, it should also meet the business needs. Image cropping uses the Imagick component.
I. Ideas reproduction:
1. Write a dynamic image script generated by the request server, which is mainly used to perform proportional scaling and cropping on the image.
2. Determine the url rule you want to generate, such as x 200-1/test.jpg.
3. cache the browser.
4. End.
Ii. dynamically crop PHP scripts
The Code is as follows:
/**
* Author pony_chiang
* High-Performance image cropping Solution
* Php-imagick extension required
*/
Ini_set ("memory_limit", "80 M ");
// Request address such? 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 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 "){
// Proportional scaling 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 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;
// Generate directory cyclically
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 );
}
3. nginx. conf configuration
The Code is as follows:
Server {
Listen 80;
Server_name test.yourdomain.com;
Root/mnt/webroot/test;
Index. php;
Expires 30d;
Location/s {
# Dynamic cropping is called only when this image is not generated
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 .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 emphasize the article about browser cache, whether it is an image generated using php or an image generated using nginx cache, add a line in php code
The Code is as follows:
Header ('Last-Modified: '. gmdate ('d, d m y h: I: s'). 'gmt ');
It is very helpful for you to use CDN. The specific effect is that the first time the client accesses the file, the http status code is 200. After refreshing, the status code is always 304.