PHP header () output picture cache
In many developments, we tried to use the header ("Content-type:image/jpeg") to output images and try to use some PHP image processing techniques to make the output image more intelligent and dynamic. But we often encounter new problems, unless you specify a different URL structure and use server technology to cache the picture, it is likely that these output pictures will consume a lot of traffic. How do you cache them and call the cache the next time the user accesses them? (only if you want to keep the picture intact.)
| The code is as follows |
|
//put this above any PHP im Age 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")); |
In the header ("Content-type:image/jpeg") above, add the above code, which will specify the current page cache time (two days) and use this cache time node in the next access.
Next determine if there is already a cache, and if so, use the cache.
Case one: If the browser already has a cache for the current page, then use it directly.
| The code is as follows |
|
//the browser would send a $ _server[' http_if_modified_since ' IF it has a cached copy if (Isset ($_server[' http_if_modified_since ')) { //If the browser have a cached version of this image, send 304 header (' last-modified: '. $_server[' http_if_modified_since '],true,304); exit; } |
Scenario Two: The browser caches the current page, although we have updated some image information, but the source image itself does not change, 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 situations we have to consider, but the above code basically leads us to the idea. Yes, remember to put them all on top of the header ("Content-type:image/jpeg").
Then let's look at an example.
| The code is as follows |
|
| Resizing a picture /** * The principle of proportional resizing of pictures: * 1, compare the original size is less than or equal to the target size, if it is directly using the original width of the height * 2, if the original size exceeds the target size, then compare the original width and height * 3, such as: width > height, then width = target width, height = target width ratio * Original height * 4, such as: High > Wide, then high = target high, width = target high proportion * The original width **/ $image = "Test.jpg"; $max _width = 200; $max _height = 200; $size = getimagesize ($image); Get the size of the image $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 _height = $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 and resize a portion of an image Header (' Content-type:image/jpeg '); Imagejpeg ($DST, null,100); Imagedestroy ($SRC); Imagedestroy ($DST); ?> |
http://www.bkjia.com/PHPjc/922889.html www.bkjia.com true http://www.bkjia.com/PHPjc/922889.html techarticle PHP header () output picture cache a lot of development, we try to use header (content-type:image/jpeg), to output images, try to use some PHP image processing technology, so that the output image more ...