- 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"));
Copy Code
In the header ("Content-type:image/jpeg"), add this 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 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
- }
Copy Code
Scenario Two: The browser caches the current page, although some image information is updated, but the source picture itself does not change, and you want to use the previous cache, then also use the cache.
- $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
- }
Copy Code
Of course, there are some special situations that we have to consider, and remember to put them all on top of the header ("Content-type:image/jpeg").
Example:
- 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
- **/bbs.it-home.org
- $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);
- ?>
Copy Code |