Phpheader function outputs image cache implementation code

Source: Internet
Author: User
Phpheader function outputs image cache implementation code

  1. // Put this above any php image generation code:
  2. Session_start ();
  3. Header ("Cache-Control: private, max-age = 10800, pre-check = 10800 ");
  4. Header ("Pragma: private ");
  5. 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.

  1. // The browser will send a $ _ SERVER ['http _ IF_MODIFIED_SINCE '] if it has a cached copy
  2. If (isset ($ _ SERVER ['http _ IF_MODIFIED_SINCE ']) {
  3. // If the browser has a cached version of this image, send 304
  4. Header ('last-Modified: '. $ _ SERVER ['http _ IF_MODIFIED_SINCE'], true, 304 );
  5. Exit;
  6. }

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.

  1. $ Img = "some_image.png ";
  2. If (isset ($ _ SERVER ['http _ IF_MODIFIED_SINCE ']) & (strtotime ($ _ SERVER ['http _ IF_MODIFIED_SINCE']) = filemtime ($ img ))) {
  3. // Send the last mod time of the file back
  4. Header ('last-Modified: '. gmdate ('d, d m y h: I: S', filemtime ($ img). 'gmt', true, 304 );
  5. Exit;
  6. }

Of course, we have to consider some special cases. remember to put them on the header ("Content-type: image/jpeg.

Example:

  1. // Adjust the image size
  2. /**
  3. * How images are adjusted proportionally:
  4. * 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.
  5. * 2. if the source image size exceeds the target size, the source image width and height are compared.
  6. * 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
  7. * 4. for example: height> width, the ratio of height to target height and width to target height * original width
  8. **/Bbs.it-home.org
  9. $ Image = "test.jpg ";
  10. $ Max_width = 200;
  11. $ Max_height = 200;
  12. $ Size = getimagesize ($ image); // Obtain the image size.
  13. $ Width = $ size [0];
  14. $ Height = $ size [1];
  15. $ X_ratio = $ max_width/$ width;
  16. $ Y_ratio = $ max_height/$ height;
  17. If ($ width <= $ max_width) & ($ height <= $ max_height ))
  18. {
  19. $ Tn_width = $ width;
  20. $ Tn_height = $ height;
  21. }
  22. Elseif ($ x_ratio * $ height) <$ max_height)
  23. {
  24. $ Tn_height = ceil ($ x_ratio * $ height );
  25. $ Tn_width = $ max_width;
  26. }
  27. Else
  28. {
  29. $ Tn_width = ceil ($ y_ratio * $ width );
  30. $ Tn_height = $ max_height;
  31. }
  32. $ Src = imagecreatefromjpeg ($ image );
  33. $ Dst = imagecreatetruecolor ($ tn_width, $ tn_height); // create a true color image
  34. Imagecopyresampled ($ dst, $ src, 0, 0, 0, $ tn_width, $ tn_height, $ width, $ height); // Copy part of the image and resize it.
  35. Header ('content-Type: image/jpeg ');
  36. Imagejpeg ($ dst, null, 100 );
  37. Imagedestroy ($ src );
  38. Imagedestroy ($ dst );
  39. ?>

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.