before we generateVerification Code will need to enter the image directly, usually use to the header ("Content-type:image/jpeg"), to achieve, here is a brief introduction.
In many developments, we tried to use the header ("Content-type:image/jpeg") to output images and try to use some PHP image processing techniques to make the output image more intelligent and dynamic. But we often encounter new problems, unless you specify a different URL structure and use server technology to cache the picture, it is likely that these output pictures will consume a lot of traffic. How do you cache them and call the cache the next time the user accesses them? (only if you want to keep the picture intact.)
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"));
In the header ("Content-type:image/jpeg") above, add the above code, which will specify the current page cache time (two days) and use this cache time node in the next access.
Next determine if there is already a cache, and if so, use the cache.
Case one: If the browser already has a cache for the current page, then use it directly.
The browser would send a $_server[' http_if_modified_since '] if it has a cached copy if (Isset ($_server[' http_if_modified_ SINCE ']) { //If the browser have a cached version of this image, send 304 header (' last-modified: '. $_server[' Http_ If_modified_since '],true,304); Exit;}
Scenario Two: The browser caches the current page, although we have updated some image information, but the source image itself does not change, and we want to use the previous cache, then 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: '. Gmdat E (' d, D M Y h:i:s ', Filemtime ($img)). ' GMT ', true, 304); Exit;}
Of course, there are some special situations we have to consider, but the above code basically leads us to the idea. Yes, remember to put them all on top of the header ("Content-type:image/jpeg").
Then let's look at an example.
<?php//Resize Picture/** * Picture Proportional sizing Principle: * 1, compare the original size is less than or equal to the target size, if it is directly using the original image width, if the original size is larger than the target size, then compare the original width high size * 3, such as: width > height, then width = target width, high = The proportion of the target width * Original high, such as: High > Wide, then high = target high, width = target high proportion * The original width **/$image = "test.jpg"; $max _width = n; $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 the copy portion of the image and resize the header (' Content-type:image/jpeg '); imagejpeg ($DST, null,100); Imagedestroy ($SRC); Imagedestroy ($ DST);? >