Home  >  Article  >  Operation and Maintenance  >  How does Nginx cooperate with PHP to realize the function of generating real-time thumbnails?

How does Nginx cooperate with PHP to realize the function of generating real-time thumbnails?

WBOY
WBOYforward
2023-05-15 18:37:06931browse

nginx configuration:

Copy code The code is as follows:

# Assume that there is a file on the server: abc.jpg, through http://filefs.domain .com/file/abc.jpg can access the original image. In fact, generally, we only save the content of "/file/abc.jpg" in the database.
#Now, we need to achieve the automatic generation of #abc.jpg.w320.jpg (w320, 320px width) by the server through http://filefs.domain.com/file/abc.jpg.w320.jpg Sketch map. and return image data.
# The following two conditions must be met:
# 1. If abc.jpg.w320.jpg exists, the image will not be regenerated
# 2. If it does not exist, in the same request, Return the image data and save the image file to the server.
server {
listen 80;
server_name filefs.domain.com;

root /var/www/http/filefs.domain.com;
location / {
index index.html index.htm;
}
location ~ \.(png|jpg|jpeg|gif)$ {
} #If the file does not exist, rewrite to the script file that generates the image autoimg.php
if (!-f $request_filename) {
rewrite ^/.*$ /autoimg.php;
expires max;
}
If the file exists, set the expiration date Time, turn off the access log
if (-f $request_filename) {
expires max;
access_log off;
}
}
error_page 500 502 503 504 /50x . html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ autoimg.php$ {#Security considerations, file server, Only the range of this script file is submitted to php for processing
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_filename /var/www/http/filefs.domain.com$fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}
}


##php generates image file code:

Copy code code show as below:

$file = $_server ['request_uri'];//Request string/file/abc.jpg.w320.jpg
$desfile = $_server ['document_root'] . $file; //Target destination path/var/www/http/file/abc.jpg.w320.jpg
$dirname = dirname ($desfile) . "/";
$filename = basename ( $desfile );
if (preg_match ( "/([^\.] \.(png|jpg|jpeg|gif))\.w([\d] )\.(jpg)/i", $filename, $m )) {
$srcfile = $dirname . $m [1];
$width = $m [3]; //Match the output file width
if (in_array ( $width, array (        //Only generate files of 202 and 320 widths
                                                                                                                                                                                                                                    $width );
}
}

/**
* Generate thumbnail
*
* @param source $src
* @param scaled bandwidth $width
*
*/
function thumbnail($src, $des, $width) {
ob_start ();/ /Start to intercept the output stream
                                                                                                                                                                                                                                        ##            $im = imagecreatefromgif ($src);
                                                                                                                                                                                                                                              == 3) {
                                                                                                                                            / $imageinfos [0];
                                                                                                                            / $imageinfos [0]; , 0, 0 , 0, 127 );
                                                                             $imageinfos [0], $imageinfos [1] );
                                                                                                                                                 , 90--compression quality, 100 represents the highest quality.
                                                               @imagedestroy (                                                                                                                                   through         $content = ob_get_contents ();//Get the output stream
       ob_end_flush ();//Output the stream to the web page, ensuring that the image data is put back in the first request
@file_put_contents ( $des, $content );//Save the file
}
?>

Rendering:

How does Nginx cooperate with PHP to realize the function of generating real-time thumbnails?

How does Nginx cooperate with PHP to realize the function of generating real-time thumbnails?

The above is the detailed content of How does Nginx cooperate with PHP to realize the function of generating real-time thumbnails?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete