Hi Guys Im trying to acomplish generating an image with an id and cache it but im not sure if im doing it right <?php require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE ); $cache = new Cache_Lite($options); if ($im= $cache->get('id_of_the_page')) { imagegif($im); imagedestroy($im); } else { //GET data from db // Create a new image instance $im = imagecreatetruecolor(100, 100); // Make the background white imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF); // Draw a text string on the image imagestring($im, 3, 40, 20, 'DB Data', 0xFFBA00); // Output the image to browser header('Content-type: image/gif'); imagegif($im); $cache->save($im); imagedestroy($im); } ?> PHP: I seam to be getting cache files generated but they are very small I would have expected them to be atleast the size of the image (4k) - im sure im saving the $im data to cache but i was hopeing to save the output of imagegif($im); and display when its in cache. Hope this makes sense? Rob
<?php require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE); $cache = new Cache_Lite($options); if ($contents= $cache->get('id_of_the_page')) { echo $contents; } else { $im = imagecreatetruecolor(300, 50); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); // start buffering ob_start(); // output jpeg (or any other chosen) format & quality imagejpeg($im, NULL, 85); // capture output to string $contents = ob_get_contents(); // end capture ob_end_clean(); imagejpeg($im, NULL, 85); $cache->save($contents); // be tidy; free up memory imagedestroy($im); } ?> PHP: Sorted it!
You can only save string data with Cache_lite. And obviously your image is no string data I do not have any experience with PEAR Cache_lite but why don't you just save the image to disk with the id and check if the image with a given id was already created?