tags:

views:

212

answers:

4

I want to cache the images of my gallery. Generating images every pages load using GD uses a lot of memory, So I am planning to create a cache image for the images generated by php script done with GD. What will be the best thing to create cache?

A: 

See this please.

Sarfraz
Yes I read it a while ago before posting my question.. But I don't have any idea on how caching is made with PHP, yes i was able to generate an image but how generated images can transform into cache image.
christian
A: 

Save it to disk. Webserver will take care of caching.

Col. Shrapnel
I usually found cache into hash forms. Is webserver also take care of hashing? Can you explain or give an example how webserver take care of caching.
christian
By it's purpose. It were designed to cache, as it HTTP server and HTTP protocol support caching. It manages to calculate a hash.
Col. Shrapnel
+1  A: 

Have you considered using phpThumb? It has tons of options for image generation and caching.

Hippo
+2  A: 

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);
}
Pez Cuckow