-
- // 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 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.
-
- // 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;
- }
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.
-
- $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 we must consider, remember to put them all above header("Content-type: image/jpeg").
Example:
-
- //Resize image
- /**
- *Principle of proportional resizing of images:
- *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
- *2. If the size of the original image exceeds the target size, compare the width of the original image Height size
- *3. For example: width > height, then width = target width, height = ratio of target width * original height
- *4. For example: height > width, then height = target height, width = target height Scale* 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 copy part of the image and resize
- header('Content-Type: image /jpeg');
- imagejpeg($dst,null,100);
- imagedestroy($src);
- imagedestroy($dst);
- ?>
Copy code
|