I once wrote an article about a high-performance PHP image dynamic cropping solution. That article was implemented using nginx Cache and rewrite. Of course, coupled with CDN, a problem with that solution is that the image is not actually generated, but Stored in cache in binary form. If the cache fails, you need to request php to generate it again. As for the difference, this is what I think for the time being.
Use your spare time to add support for statically generated images, support switching between 3 image modes, automatically crop the image size on the portal website, reduce server bandwidth, and use the Imagick component for image cropping.
1. Reproduction of ideas:
1. First write a dynamic script that requests the server to generate a picture, which mainly performs proportional scaling calculation + cropping of the picture.
2. Determine the URL rule you want to generate, such as http://www.domain.com/www/300×200-1/test.jpg.
3. Caching the browser.
4. End.
2. PHP script for dynamically cropping images
-
-
/**
- * Author pony_chiang
- * High-performance image cropping solution
- * 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 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 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 );< ;/p>
if (! file_exists ( $save_dir ))
- wpx_mkdir ( $save_dir );
$target_width = $width;
- $target_height = $height;< /p>
$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") {
- //Constantly 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") {
- // Zoom in 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 );
- } 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;
- < ;p>// Loop to generate directories
- 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 code
3. nginx.conf configuration
-
-
server {
- listen 80;
- server_name test.yourdomain.com;
- root /mnt/webroot/test;
- index index.php;
- expires 30d;
-
location /s {
- #Dynamic cropping is only called 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;
- }
- }< /p>
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;
- includefastcgi_params;
- }
}
-
Description , emphasize the article about browser caching, whether it is an image generated through php or an image generated using nginx cache, add a line in the php code
-
- header('Last-Modified: ' .gmdate('D, d M Y H:i:s') . ' GMT' );
Copying the code
is very helpful for using CDN.
The http status code when the client accesses this file for the first time is 200. After refreshing, the status code is always 304. You understand the benefits. The local client caches it, saving bandwidth.
|