In many development, we try to use header ("Content-type:image/jpeg") to output pictures, try to use some PHP image processing technology, make the output picture more intelligent and dynamic. But we often encounter new problems, unless you specify a different URL structure, and the use of server technology to cache pictures, it is likely that these output pictures will consume a lot of traffic. How do I cache them and call the cache the next time the user accesses it? (If you want this picture to stay the same)
The code is as follows |
|
Put this above any PHP image generation code: Session_Start (); Header ("Cache-control:private, max-age=10800, pre-check=10800"); Header ("Pragma:private"); Header ("Expires:"). Date (Date_rfc822,strtotime ("2 Day")); |
Add the above code above the header ("Content-type:image/jpeg"), which will specify the current page cache time (two days) and use the cache time node for the next visit.
Next, determine if there is already a cache, and if so, use caching.
Case one: If the browser already has a cache on the current page, use it directly.
The code is as follows |
|
The browser'll send a $_server[' http_if_modified_since ' IF it has a cached copy if (Isset ($_server[' http_if_modified_since ')) { If the browser has a cached version of this image, send 304 Header (' last-modified: '. $_server[' http_if_modified_since '],true,304); Exit } |
Case two: Browsers cache the current page, although we have updated some picture information, but the source picture itself has not changed, and we want to use the previous cache, then also use the cache.
The code is as follows |
|
$img = "Some_image.png"; if (Isset ($_server[' http_if_modified_since ')) && (Strtotime ($_server[' http_if_modified_since ') = = Filemtime ($img))) { Send the last mod time of the "file back" Header (' last-modified: ' Gmdate (' d, D M Y h:i:s ', Filemtime ($img)). ' GMT ', True, 304); Exit } |
Of course, there are some special circumstances we must consider, but the above code can basically lead our thinking. Yes, remember to put them all on top of the header ("Content-type:image/jpeg").
Then let's take a look at an example.
code as follows |
|
<?php //Adjust picture size /** * Picture proportional resizing principle: * 1, compare original big Small is less than or equal to the target size, if it is directly using the original image width High * 2, if the original size exceeds the target size, then the contrast image is wide and high size * 3, such as: Wide > High, then wide = target wide, high = target width of the ratio * Originally high *4, such as: High > wide, high = high target, width = target high ratio * Original width **/ $image = "test.jpg"; $max _width = + $max _height = $size = getimagesize ($image);//Get image size $width = $size [0]; $height = $size [1]; $x _ratio = $max _width/$width; $y _ratio = $max _height/$height; if ($width <= $max _width) && ($height <= $max _height)) { $tn _width = $width; $tn _heig HT = $height; } ElseIf (($x _ratio * $height) < $max _height) { $tn _height = ceil ($x _ratio * $height); $tn _width = $max _width; } Else { $tn _width = ceil ($y _ratio * $width); $tn _height = $max _height; } $src = Imagecreatefromjpeg ($image); $dst = Imagecreatetruecolor ($tn _width, $tn _height);//Create a new true color image Imagecopyresampled ($DST, $src, 0, 0, 0, 0, $tn _width, $tn _height, $width, $height);//Resample copy part of image and resize header (' Cont Ent-type:image/jpeg '); Imagejpeg ($DST, null,100); Imagedestroy ($SRC); Imagedestroy ($DST); ? |