PHP: help with PHP Thumb class

I am using a php thumb class to display my images (class here http://wiki.github.com/masterexploder/PHPThumb/) following the instructions as strictly as possible but the images won't display at all. I have tried all sorts of things, still no luck. When checking the paths in the sourcecode, they look correct.. but still, can't make it work

The include file that is actually responsible for the images looks like this (below)

and the image tag then looks like this:  (example version)
<img src="show_image.php?file=<?php echo urlencode('/path/to/image.jpg'); ?>" />

(my version)
<img src="<?php echo INCLUDES_DIR ?>show_image.php?file=<?php echo urlencode(IMAGE_DIR.$image->filename)?>" class="thumbnail" />

Please suggest what could be wrong, I thought I had tried everything but I am sure you will have loads of ideas. More heads know more :-)

<?php //Original

require_once 'path/to/ThumbLib.inc.php';

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

if ($fileName h3. 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)
{
     // handle error here however you'd like
}

$thumb->adaptiveResize(80, 80);

$thumb->show();

?>

<?php  //my customized version
require_once ('../library/ThumbLib.inc.php');

echo 'script running';

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

if (!$fileName || !file_exists($fileName))  //h3. null
{
     // handle missing images however you want... perhaps show a default image??  Up to you...
     echo "MISSING IMAGE";
}

try
{
     $thumb = PhpThumbFactory::create($fileName);
}
catch (Exception $e)
{
     // handle error here however you'd like
     echo 'ERROR OCCURED';
}

$thumb->adaptiveResize(150, 100);

$thumb->show();

?>

Open in new window

czechmate1976Asked:
Who is Participating?
 
Slick812Connect With a Mentor Commented:
Greetings czechmate1976, , I looked at your code above, and I can see you have not used the image format in -
<img src="show_image.php?file=<?php echo urlencode('/path/to/image.jpg'); ?> />

in the PHPThumb, the method $thumb->show();  is VERY DIFFERENT  than  $thumb->save('/path/to/new_image.jpg');
I would suggest that you look these up and get a better Idea of what they do.

as to your problem above, in "my customized version" you have -
echo 'script running';
   NO NO, you can NOT have any text output for the $thumb->show(); method, this will result in all sorts of errors, which you will not seen in the <img > tag where the errors will be sent.

if you are using the <img src="show_image.php?file=<?php echo urlencode('/path/to/image.jpg'); ?> /> method you ABSOLUTELY HAVE TO ONLY SEND IMAGE DATA, with no text data whatsoever !
this require that you set the mime type in the header to  image/jpeg or another image type, and then output a complete Image file bytes.

if you are using a php GD -
<?php
$imgs = imagecreatetruecolor(100, 100);
imagetruecolortopalette($imgs, true, 255);
header('Content-Type: image/gif');
@ImageGIF($imgs);
imagedestroy($imgs);
?>

there are other considerations for the Image output, which PHPThumb takes care of, but you must understand some about what's going on with the methods.

Ask questions if you need more Info.

0
 
czechmate1976Author Commented:
Thanks a lot for your feedback. It's really appreciated, as you see not many come back on this issue.

That echo statement was to see something coming through, I did not realize there was no chance to view any of that. I have now tried using the example code from the class and extending it step by step to imitate my requirements, and found out that I cannot use absolute reference for teh '..?file=' to display the image, so now I am trying to just incorporate some of the try-catch error handling, but at least the images started showing up.

Just a quick question.. I guess you are familiar with the class to some extent, would you have any idea how to determine which dimension - width or height - is larger, so I can actually change the aim dimensions I need by finding out if an image is portrait or landscape format.

Thanks a lot again for your comment!!!
0
 
Slick812Connect With a Mentor Commented:
OK, I looked at the PHP code for some of the PHPThum files , but, I do not have time to go through all of the methods it has, It seems a bit much for a simple thumbnail image requirement.

I did see a function -
public function getCurrentDimensions ();
return is an array as -
$this->currentDimensions = array('width' => imagesx($this->oldImage), 'height' => imagesy($this->oldImage));

I DO NOT know if this will work, but you might try -

$thumb = PhpThumbFactory::create($fileName);
// only AFTER you initiate the Object
$dimArray = $thumb->getCurrentDimensions ();
$width = $dimArray['width];

 - - - - - - - - - - - - - -  --
or you may want to try the php GD -

$imageinfo = getimagesize($fileName);
$width = $imageinfo[0];
$height = $imageinfo[1];

- - - - - - - - - - - -

below is some code you might look over, to get some Idea of using the GD to make a thumb nail Image, but it does NOT do the <img tag method, It does a "save to file" method, which is easier to trouble shoot as you try and learn about GD, I only offer this because I posted it in EE before, so you might see what it involves.
<?php
define("MAX_THUMB", 108);//set this to the thumbnail DIMENTION max

    // doThumb( ) is the IMPORTANT resize to thumb function here

function doThumb($imInfo, &$imgT){
if (!is_array($imInfo)) return false;
$height = MAX_THUMB;
$width = MAX_THUMB;
if ($imInfo[0] < $imInfo[1]) {
        $width = (int) ((MAX_THUMB / ($imInfo[1] / $imInfo[0]))+ 1);
        }
        elseif ($imInfo[0] > $imInfo[1]) {
        $height = (int) ((MAX_THUMB / ($imInfo[0] / $imInfo[1]))+ 1);
        }
if ($height < 2) $height = 2;
if ($width < 2) $width = 2;
$outImg = ImageCreateTrueColor($width, $height);
if (!ImageCopyResampled($outImg, $imgT, 0, 0, 0, 0, $width, $height, $imInfo[0], $imInfo[1])) 
        {ImageDestroy($outImg); return false;}
return $outImg;
}

$fileName = 'bigger.jpg';
//set $fileName to the proper file path and name

$imageinfo = getimagesize($fileName);
$aDo = 1;
if ($imageinfo['mime'] == 'image/gif') {$temImg = imagecreatefromgif($fileName);}
        elseif ($imageinfo['mime'] == 'image/jpeg') {$temImg = imagecreatefromjpeg($fileName);}
        elseif ($imageinfo['mime'] == 'image/png') {$temImg = imagecreatefrompng($fileName);}
        else $aDo = 2;

if ($aDo == 1) {
        // this starts the thumb Image code
        if ($imageinfo[0] > MAX_THUMB || $imageinfo[1] > MAX_THUMB) 
                $smImg = doThumb($imageinfo, $temImg); else
                {$smImg = $temImg;}
        if($smImg != false){
                // DO NOT save the thumb as the same file type of upload
                imagejpeg($smImg,'images/temp.jpg',76);
//WARNING, you must place the correct file name in imagejpeg()
                ImageDestroy($smImg);
                }
        @ImageDestroy($temImg);
        // this is the end of the thumb image code
        } echo 'ERROR, not a Web Image';
?>

Open in new window

0
 
czechmate1976Author Commented:
Thank you both of you guys for your help!!! YOu both helped a great deal. See you around :-)
0
 
czechmate1976Author Commented:
Oh, sorry, I totally did not notice that it was just you and no other expert:-) From all the coding, I lose my sight :-) Well, great job - points are yours
0
All Courses

From novice to tech pro — start learning today.