- // 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 this code in header ("Content-type: image/jpeg"), which specifies the current page cache time (two days ), the cache time node is used in the next access. Next, determine whether a cache exists. If yes, use the cache. Scenario 1: If the browser already caches the current page, use it directly.
- // The browser will 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 2: The browser caches the current page. although some image information is updated, the source image itself remains unchanged and you want to use the previous 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;
- }
Of course, we have to consider some special cases. remember to put them on the header ("Content-type: image/jpeg. Example:
- // Adjust the image size
- /**
- * How images are adjusted proportionally:
- * 1. compare whether the source image size is smaller than or equal to the target size. If yes, directly use the source image width and height.
- * 2. if the source image size exceeds the target size, the source image width and height are compared.
- * 3. for example, if the width is greater than the height, the width = the target width and the height = the proportion of the target width * the original height
- * 4. for example: height> width, the ratio of height to target height and width to target height * original width
- **/Bbs.it-home.org
-
- $ Image = "test.jpg ";
- $ Max_width = 200;
- $ Max_height = 200;
-
- $ Size = getimagesize ($ image); // Obtain the 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_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 true color image
- Imagecopyresampled ($ dst, $ src, 0, 0, 0, $ tn_width, $ tn_height, $ width, $ height); // Copy part of the image and resize it.
- Header ('content-Type: image/jpeg ');
- Imagejpeg ($ dst, null, 100 );
- Imagedestroy ($ src );
- Imagedestroy ($ dst );
- ?>
|