PHP header function output picture cache implementation code

Source: Internet
Author: User
    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"));
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.

    1. The browser would 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 have a cached version of this image, send 304
    4. Header (' last-modified: '. $_server[' http_if_modified_since '],true,304);
    5. Exit
    6. }
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.

    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. }
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:

  1. Resizing a picture
  2. /**
  3. * The principle of proportional resizing of pictures:
  4. * 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
  5. * 2, if the original size exceeds the target size, then compare the original width and height
  6. * 3, such as: width > height, then width = target width, height = target width ratio * Original height
  7. * 4, such as: High > Wide, then high = target high, width = target high proportion * The original width
  8. **/bbs.it-home.org
  9. $image = "Test.jpg";
  10. $max _width = 200;
  11. $max _height = 200;
  12. $size = getimagesize ($image); Get the size of the image
  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 new True color image
  34. Imagecopyresampled ($DST, $src, 0, 0, 0, 0, $tn _width, $tn _height, $width, $height); Resample and resize a portion of an image
  35. Header (' Content-type:image/jpeg ');
  36. Imagejpeg ($DST, null,100);
  37. Imagedestroy ($SRC);
  38. Imagedestroy ($DST);
  39. ?>
Copy Code
  • 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.