Home > Backend Development > PHP Tutorial > PHP header function outputs image cache implementation code

PHP header function outputs image cache implementation code

WBOY
Release: 2016-07-25 08:52:17
Original
1054 people have browsed it
  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 the code

Add in header("Content-type: image/jpeg"); This code will specify the cache time of the current page (two days) and use this cache time node on the next visit. Next, determine whether there is already a cache. If so, use the cache.

Situation 1: If the browser already caches the current page, then 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. }
Copy code

Case 2: Browser cached Although some image information has been updated on the current page, the source image itself has not changed. If you want to use the previous cache, you should 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 we must consider, remember to put them all above header("Content-type: image/jpeg").

Example:

  1. //Resize image
  2. /**
  3. *Principle of proportional resizing of images:
  4. *1. Compare whether the size of the original image is less than or equal to the target size. If so, directly use the width and height of the original image
  5. *2. If the size of the original image exceeds the target size, compare the width of the original image Height size
  6. *3. For example: width > height, then width = target width, height = ratio of target width * original height
  7. *4. For example: height > width, then height = target height, width = target height Scale* 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 copy part of the image and resize
  35. header('Content-Type: image /jpeg');
  36. imagejpeg($dst,null,100);
  37. imagedestroy($src);
  38. imagedestroy($dst);
  39. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template