PHP header () output picture cache

Source: Internet
Author: User

In many development, we try to use header ("Content-type:image/jpeg") to output pictures, try to use some PHP image processing technology, make the output picture more intelligent and dynamic. But we often encounter new problems, unless you specify a different URL structure, and the use of server technology to cache pictures, it is likely that these output pictures will consume a lot of traffic. How do I cache them and call the cache the next time the user accesses it? (If you want this picture to stay the same)

The code is as follows Copy Code
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 the header ("Content-type:image/jpeg"), which will specify the current page cache time (two days) and use the cache time node for the next visit.

Next, determine if there is already a cache, and if so, use caching.

Case one: If the browser already has a cache on the current page, use it directly.

The code is as follows Copy Code
The browser'll 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
}

Case two: Browsers cache the current page, although we have updated some picture information, but the source picture itself has not changed, and we want to use the previous cache, then also use the cache.

The code is as follows Copy Code

$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, there are some special circumstances we must consider, but the above code can basically lead our thinking. Yes, remember to put them all on top of the header ("Content-type:image/jpeg").

Then let's take a look at an example.

The code is as follows Copy Code

<?php
Resize a picture
/**
* The principle of proportional resizing of pictures:
* 1, compare the original size is less than equal to the target size, if it is directly using the original image width and height
* 2, if the original size exceeds the target size, compare the original image width and height size
* 3, such as: Wide > High, then wide = target width, high = target width of the ratio * Original height
* 4, such as: High > Wide, then high = target high, wide = target high Proportion * Original width
**/

$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 copy part of image and resize
Header (' Content-type:image/jpeg ');
Imagejpeg ($DST, null,100);
Imagedestroy ($SRC);
Imagedestroy ($DST);
?>

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.