PHP uses header () to output an image cache instance. _ PHP Tutorial

Source: Internet
Author: User
PHP uses header () to output the image cache instance ,. 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 PHP to use header () to output the 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)

The 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.

The 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.

The 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.

The 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.

An example of outputs () to output the Image Cache. this example describes how PHP outputs the image cache using header. Share it with you for your reference. The specific analysis is as follows...

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.