PHP uses header () to output an image cache instance,
This example describes how PHP outputs the image cache using header. Share it with you for your reference. The specific analysis is as follows:
When we generate a verification code, we need to directly enter the image. Usually we use the header ("Content-type: image/jpeg"). Here we will give a brief introduction.
In many development scenarios, we try to use header ("Content-type: image/jpeg"); to output images and use some php image processing techniques, make the output image more intelligent and dynamic. However, we often encounter new problems. Unless you specify different URL structures and use server technology to cache images, these output images may consume a lot of traffic. How can we cache them and call the cache next time a user accesses them? (If you want to keep this image unchanged)
Copy codeThe code is as follows: // 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 the above Code above header ("Content-type: image/jpeg"), which specifies the Cache Time of the current page (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.
Copy codeThe Code is as follows: // 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;
}
Scenario 2: the browser caches the current page. Although we have updated some image information, the source image itself remains unchanged. We also want to use the previous cache.
Copy codeThe Code is as follows: $ 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, but the above Code can basically lead our thinking. By the way, remember to put them all on the header ("Content-type: image/jpeg.
Let's look at an example.
Copy codeThe Code is as follows: <? Php
// 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
**/
$ 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 );
?>
I hope this article will help you with PHP programming.