How caching is implemented using PHP GD

I want to cache images of my gallery. Generating images when loading each page using GD uses a lot of memory. Therefore, I plan to create a cache image for images created by php script executed with GD. What will be the best way to create a cache?

+3
source share
4 answers

Use something like

$mime_type = "image/png";
$extension = ".png";
$cache_folder = "cache";

$hash = md5($unique . $things . $for . $each . $image);
$cache_filename = $cache_folder . '/' . $hash . $extension;

//Is already it cached?
if($file = @fopen($cache_filename,'rb')) {  
    header('Content-type: ' . $mime_type);
    while(!feof($file)) { print(($buffer = fread($file,4096))); }
    fclose($file);
    exit;
} else {
//Generage a new image and save it
   imagepng($image,$cache_filename); //Saves it to the given folder

//Send image
   header('Content-type: ' . $mime_type);
   imagepng($image);
}
+5
source

Do you find using phpThumb ? It has many features for generating and caching images.

+1
source

, , readfile(). :

if (file_exists($image_path)) {
    // send the cached image to the output buffer (browser)
    readfile($image_path);
}else{
    // create a new image using GD functions
...

script :

http://www.alphadevx.com/a/57-Implementing-an-Image-Cache-for-PHP-GD

+1

. Web- .

0
source

Source: https://habr.com/ru/post/1737104/


All Articles