PHP generated image and Pear Cache_lite

Discussion in 'PHP' started by WebWorth, Mar 25, 2010.

  1. #1
    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
     
    WebWorth, Mar 25, 2010 IP
  2. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #2
    
    <?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!
     
    WebWorth, Mar 25, 2010 IP
  3. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    Nyu, Mar 26, 2010 IP