Forum Moderators: coopster

Message Too Old, No Replies

img.php to print thumbnails

What to do when no image is loaded?

         

asantos

3:29 am on Jun 7, 2006 (gmt 0)

10+ Year Member



Hi. I found a thumbnail generator php script and kind-of-modify it to fit my needs. It now handels jpeg,gif and png resize.

The idea is to call:

<img src="http://foo.com/img.php?tipo=album&img=my_pic2.jpg" />

So it will print what i have in:
DIR_UPLOAD / album / my_pic2.jpg
DIR_UPLOAD = uploads directory

My problem is when i call:
[foo.com...]
It prints a blank page. Since im putting the URL in the src attribute of the <img> tag, should I send a special header when theres no image?

Here's the code of img.php
**************************
if(!$_GET['img'] ¦¦!$_GET['tipo']) die();
$file = DIR_UPLOAD.basename($_GET['tipo']).'/'.basename($_GET['img']);
if(file_exists($file)) {
$img = new img($file);
$img->resize($_GET['s'],$_GET['c']);
$img->show();
}

class img {
var $imagen;
var $temp;
var $ext;

function img($file) {
$info = pathinfo($file);
$this->ext = $info['extension'];
switch($this->ext) {
# GIF
case 'gif':
$this->imagen = ImageCreateFromGIF($file);
break;
# JPEG
case 'jpe': case 'jpeg': case 'jpg':
$this->imagen = ImageCreateFromJPEG($file);
break;
# PNG
case 'png':
$this->imagen = ImageCreateFromPNG($file);
break;
# ...
default: return;
}
}

function resize($size=false,$cuadrado=false) {
if(!$size) $size = 100;
$width_original = imagesx($this->imagen);
$height_original = imagesy($this->imagen);
if($cuadrado) {
$width = $height = $size;
} else {
$width = $size;
$height = round($height_original * $width / $width_original);
}
$this->temp = imageCreateTrueColor($width,$height);
imageCopyResampled($this->temp,$this->imagen,0,0,0,0,$width,$height,$width_original,$height_original);
$this->imagen =& $this->temp;
unset($this->temp);
return;
}

function show() {
header('Content-Type: image/jpeg');
switch($this->ext) {
# GIF
case 'gif':
ImageGIF($this->imagen);
break;
# JPEG
case 'jpe': case 'jpeg': case 'jpg':
ImageJPEG($this->imagen);
break;
# PNG
case 'png':
ImagePNG($this->imagen);
break;
# ...
default: return;
}
}
}
******************
Any ideas welcomed.

eelixduppy

10:39 am on Jun 7, 2006 (gmt 0)



You could do something like this. Change the following
if(!$_GET['img'] ¦¦!$_GET['tipo']) die();

To:

if(empty($_GET["img"] ¦¦ empty($_GET{"tipo"]))
{
die("Url query not correct.");
exit;
}

eelixduppy

1:35 pm on Jun 7, 2006 (gmt 0)



I'm sorry. There is an error in the above example. Here is the corrected code:

if(empty($_GET["img"] ¦¦ empty($_GET["tipo"]))
{
die("Url query not correct.");
exit;
}

Sorry about that ;)

asantos

5:42 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



eelixduppy,
i had something like that, but i was thinking on redrawing a black square with a "?" sign in the middle when the image doesnt exist.

How can i accomplish that?

eelixduppy

5:46 pm on Jun 7, 2006 (gmt 0)



Why don't you just create a black square with a question mark (with your favorite editor--mine is Photoshop ;)) in it and just display it when the image doesn't exist. Something like this:
if(empty($_GET["img"] ¦¦ empty($_GET["tipo"]))
{
echo 'Not an image!<br>';
echo '<img src="No_image.jpg />';
exit;
}

Is this what you are trying to accomplish?

asantos

5:59 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Actually that isnt a viable alternative. Because the thumbnail can have ANY dimensions. If i have a default image (default.gif) it will loose quality when it gets resized. Remember the default size must be the same size as the other thumbnails.

Thats why i have to generate a black square with the "?" in the middle. How can i accomplish that?

eelixduppy

6:09 pm on Jun 7, 2006 (gmt 0)



Umm...I see what you are trying to do now, sorry I didn't understand fully before. This task can be completed with a series of calls to functions within PHP's GD library. imagefilledrectangle [us2.php.net] will help you create a square, and then you have to use a function to write text to the image, one such function is imagefttext [us2.php.net]. A function list is available here [us2.php.net]. You can accomplish this with these functions, however, I'm not very good with image manipulation (mostly because I don't really do it). Good luck!

asantos

7:16 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Thanks for the tips.