PHP Thumb » Help & Bugs » Help

Displaying Thumbs

(3 posts)
  1. idragon
    Member

    Hi Guys,

    I need some help, and somewhat newbie in php but i get along usually.

    I have a special field for image classes, so i want to resize them with http://www.site.com/show_imagex.php?file=%field% this works in my code but i have lots of images, around 20+ per page, so i think it takes a hit on the server. So i was thinking to save them and display them, something like this:

    <?php

    require_once 'ThumbLib.inc.php';

    $fileName = (isset($_GET['file'])) ? urldecode($_GET['file']) : null;

    if ($fileName === null || !file_exists($fileName))
    {
    echo "<p>imagine lipsa</p>";
    }

    try
    {
    $thumb = PhpThumbFactory::create($fileName);
    }
    catch (Exception $e)
    {
    echo "<p>exception</p>";
    }

    if(!file_exists('static/'.$fileName))
    {

    $thumb->adaptiveResize(170, 80);

    $thumb->save('static/'.$fileName);
    }
    $new = PhpThumbFactory::create('static/'.$fileName);

    if(file_exists('static/'.$fileName))
    {

    $new->show();
    }

    ?>

    So basically get the field name, save in static, and show it. Now i dont know if this uses resources each time i call it, to create a new thumb, or it takes it from static.

    Any suggestions?

    Posted 2 years ago #
  2. d1uluv2h8
    Member

    Actually, what you did uses even more resources than just displaying the image :)

    Here's what i did for a rudimentary caching system:


    require 'ThumbLib.inc.php';
    $options = array('jpegQuality' => 90);

    $fileName = (isset($_GET['file'])) ? urldecode($_GET['file']) : null;

    $cache_dir = 'img_cache';
    $cache_file_name = md5($fileName).'_60x60_adaptive.jpg';

    if (file_exists($cache_dir.'/'.$cache_file_name)) {
    $im = @imagecreatefromjpeg($cache_dir.'/'.$cache_file_name);

    header('Content-type: image/jpeg');
    header('Content-Length: '. filesize($cache_dir.'/'.$cache_file_name));
    imagejpeg($im);
    imagedestroy($im);
    exit();
    }

    if (!is_dir($cache_dir)) {
    mkdir($cache_dir);
    }

    if ($fileName === null || !file_exists($fileName))
    {
    // handle missing images however you want... perhaps show a default image?? Up to you...
    }

    try
    {
    $thumb = PhpThumbFactory::create($fileName);
    }
    catch (Exception $e)
    {
    // Exception
    }

    if (isset($thumb))
    {
    $thumb->adaptiveResize(60, 60);

    $thumb->save($cache_dir.'/'.$cache_file_name, 'jpg');

    $thumb->show();
    }
    else
    {
    header("HTTP/1.0 404 Not Found");
    include(site_root."/404.php");
    exit();
    }

    Posted 2 years ago #
  3. idragon
    Member

    thank you very much, i will try this

    Posted 2 years ago #

RSS feed for this topic

Reply

You must log in to post.