nginx+php real-time generation of different size pictures

Source: Internet
Author: User
Tags imagejpeg php script readfile

The original picture server uses the Windows. NET architecture, in view of the need to generate a variety of size pictures.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/95/wKiom1cjJSSQEzn6AABzdrDHnIQ604.png "title=" QQ picture 20160429171042.png "alt=" Wkiol1cjfoaju6pzaabcg35yrkg456.png "/>

Process Description:

The user from Nginx request the corresponding picture, determine whether there are _200x300 corresponding parameters, if not directly request to the corresponding directory of the original, otherwise continue to determine whether the corresponding cache image has been generated locally, if there is a return has been generated custom size picture, otherwise request PHP dynamic generation.

Nginx Partial configuration:

    server {        listen        80;        server_name  pics.abc.com ;        location / {             root   /var/www/html;             index  index.html index.htm index.php;         error_page   500 502 503 504  / 50x.html;        location = /50x.html {             root   html;         }        location ~ \_ (\d+) x (\ d+) \. (jpg|png|gif|jpeg|bmp) $&nbsP {  //Decide whether to customize the diagram                  try_files  $uri  /temp/$uri  /get.php;    //to determine if a custom diagram has been generated otherwise transferred to/ Get.php                expires       30d;        }         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;        }         location ~ .*\. (gif|jpg|jpeg|png|bmp) $        {                 expires      30d;         }        }     }

In/var/www/html we mount the Windows directory in read-only mode, modify/etc/fstab, add

\\192.168.2.3\f$\pics.abc.com\pics/var/www/html/pics/cifs Ro,username=user,password=pass 1 2

Then restart the Netfs service, and execute the following command to install the dependent packages

Yum-y Install samba-client cifs-utilsservice netfs restartchkconfig netfs on

The resulting scaling graph is placed in the temp directory of the site directory, such as the requested Http://pics.abc.com/pics/201604/29/abc_200x300.jpg

The resulting picture is placed in the Temp/pics/201604/29/abc_200x300.jpg directory


PHP Script:

Function thumb ($src,  $width,  $height,  $filename,  $mode  =  ' scale ',  $ quality =  ' + ')  {try {         $imageValue  =  getimagesize ($SRC);         $sourceWidth  =  $imageValue [ 0]; //Original width          $sourceHeight  =  $imageValue [1]; / /original          $thumbWidth  =  $width;  //thumbnail width           $thumbHeight  =  $height;  //thumbnail height          $_x = 0;        $_y = 0;          $w  =  $sourceWidth;          $h  =  $sourceHeight;        if  ($mode  ==  ' scale ')  {                if  ($ sourcewidth <=  $thumbWidth  &&  $sourceHeight  <=  $thumbHeight)   {                         $_x = floor (($thumbWidth  -  $sourceWidth)  / 2 );                         $_y = floor (($thumbHeight  -  $sourceHeight)  / &NBSP;2);                          $thumbWidth  =  $sourceWidth;                           $thumbHeight  =  $sourceHeight;                 } else {                         if  ($thumbHeight  * $ sourcewidth >  $thumbWidth  *  $sourceHeight)  {                                   $thumbHeight  = floor ($sourceHeight  * $ width /  $sourceWidth);                                  $_y = floor (($height  -  $thumbHeight)  / 2);                         } else {                                   $thumbWidth  = floor ($ sourcewidth *  $height  /  $sourceHeight);                                  $_x = floor (($width  -  $thumbWidth)  / 2);                          }                 }        } else if  ($mode   ==  ' crop ')  {                if  ($sourceHeight  <  $thumbHeight)  { //If the original size is less than the current size                    $thumbWidth  = floor ($thumbWidth  * $ sourceheight /  $thumbHeight);                  $thumbHeight  =  $sourceHeight;                 }                 if  ($sourceWidth  <  $thumbWidth)  {                           $thumbHeight  = floor ($thumbHeight  *  $sourceWidth  /   $thumbWidth);                           $thumbWidth  =  $sourceWidth;                 }                  $s 1 =  $sourceWidth  /  $sourceHeight;  //original ratio                   $s 2 =  $width  /  $height;  //New chart ratio                  if  ($s 1 ==  $s 2)  {                 } else if  ($s 1 >  $s 2)  {  Full height                            $y  = 0;                          $ax  = floor ($sourceWidth  *  ($thumbHeight  /  $sourceHeight));                          $x  =  ($ax  -  $thumbWidth)  / 2;                          $w  =  $thumbWidth   /  ($thumbHeight  /  $sourceHeight);                 } else { //Full Width                            $x  = 0;                          $ay  = floor ($sourceHeight  *  ($thumbWidth  /  $sourceWidth) );  //simulated original ratio height                           $y  =  ($ay  -  $thumbHeight)  /  2;                          $h  =  $thumbHeight  /  ($thumbWidth  / $ Sourcewidth);                 }         }        switch  ( $imageValue [2])  {               &NBSP;&NBSP;CASE&NBSP;2:  $source  = imagecreatefromjpeg ($SRC);                         break;                 case 1:  $source  = imagecreatefromgif ($SRC);                         break;                 case 3:  $source  =  Imagecreatefrompng ($SRC);                         break;                 case 6:  $source  =  Imagecreatefromwbmp ($SRC);                          break;                 default: defulat ();                 return;        }         header ("Content-type: image/jpeg");         $ Thumb = imagecreatetruecolor ($width,  $height);         Imagefill ($thumb,  0, 0, imagecolorallocate ($thumb,  255, 255, 255));         imagecopyresampled ($thumb,  $source, 0, 0,  $x,  $y,   $width,  $height,  $w,  $h)         imagejpeg ($thumb , null,  $quality)         imagejpeg ($thumb,  $filename,  $quality);         imagedestroy ($THUMB);         imagedestroy ($source);}  catch  (exception  $ex)  {        defulat ();  }} Function defulat ()  {/*         $default _img =  Realpath ('.. /pictures/nopic.gif ');         ob_start ();         header (' content-type:image/jpeg ');         ReadFile ($default _img);         ob_flush ();         flush ();*/echo  ' Error ';} Function mkdirs ($dir) {    if (!is_dir ($dir)) {         if (!mkdirs (dirname ($dir))) {            return false;         }        if (!mkdir ($dir, 0755)) {             return false;         }    }    return true;} $uri =$_server[' Request_uri '; $image =basename ($uri); $temp = './temp/'. DirName ($uri). ' /'; $imgpath = '. '. DirName ($uri). ' /';/*//checks if there is a local file, the original if (File_exists ($temp. $image)) {        ob_start ();         header (' content-type:image/jpeg ');         readfile ($temp. $image);         ob_flush ();         flush ();         exit () ;} *///checks whether the generated picture has ever been generated, is returned if it exists, otherwise regenerates the new figure if (!preg_match ('/_ (\d+)X (\d+)/',  $image,  $wh) {        ob_start ();         header (' content-type:image/jpeg ');         readfile ($imgpath. $image);         ob_flush ();         flush ();         exit ();} $width  =  $WH [1]; $height  =  $WH [2]; $source _img=preg_replace ('/_ (\d+) x (\d+)/',  ',  $image)//For images that are over the length and width, return the original if ($width >=2000 | |   $height >=2000) {Ob_start ();         header (' content-type:image/ JPEG ');         readfile ($imgpath. $source _img);         ob_flush ();         flush ();         exit ();} Picture processing $src= $imgpath. $source _img; $filename = $temp. $image; Mkdirs ($temp);//thumb (Realpath ($src),  $width,  $height,  $filename,  ' crop ',  ' 85 '); Thumb (Realpath ($src),  $width,  $height,  $filename,  ' crop ',  ' 100 ');


PHP Generate dimensions section refer to <php picture automatic cutting to cope with different sizes of display >





This article is from "Maple Night" blog, please be sure to keep this source http://fengwan.blog.51cto.com/508652/1769043

nginx+php real-time generation of different size pictures

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.