Click to See Complete Forum and Search --> : thumbnail BG color


cybercampbell
11-10-2004, 04:52 PM
I use this thumbnail script which works great:

<?
$path='';
$path=$_GET['path'];
$thumbsize = explode("x", $_GET['size']);
if(isset($_GET['path'])&&isset($_GET['size'])&&substr($path, 0, 7) != "http://" && substr($path, -4)==('.jpg'||'.gif'||'.png') && file_exists($path)){
$imagesource = $path;
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (!$image) die();
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
if ($imagewidth >= $imageheight) {
$thumbwidth = $thumbsize[0];
$factor = $thumbsize[0] / $imagewidth;
$thumbheight = $imageheight * $factor;
}
elseif ($imageheight >= $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
}
elseif ($imageheight==$imagewidth) {
$thumbheight = $thumbsize[1];
$thumbwidth = $thumbsize[0];
}
$thumb = @imagecreatetruecolor($thumbsize[0],$thumbsize[1]);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
header('Content-type: image/jpeg');
imagejpeg($thumb);
imagedestroy($image);
imagedestroy($thumb);
} else { }
?>

it resizes the image down so the longest lenth is 100px and the other lenth is relative then fill the rest of the ThumbNail black so the whole thumb is 100px x 100px (did that make sence?)

I need to change the backgroung fill color to 'white'....how do I do that?

also a quick extra question:

is it better to use 'imagecopyresampled' or 'imagecopyresized'?

Cheers
Chris

sciguyryan
11-11-2004, 01:59 PM
I don't think that you can alter the image in this way but, I'm not shure.

An as for the other question I have no idea, I have not worked much with images :)


RyanJ

jebster1ca
11-12-2004, 06:35 PM
Add this

$white = ImageColorAllocate($thumb, 255, 255, 255);
ImageFill($thumb, 0, 0, $white);

before this
$thumb = @imagecreatetruecolor($thumbsize[0],$thumbsize[1]);

:)

jebster1ca
11-12-2004, 06:50 PM
Also, if you want it to have even padding on either side(or top and bottom depending on the picture dimensions) replace this

if ($imagewidth >= $imageheight) {
$thumbwidth = $thumbsize[0];
$factor = $thumbsize[0] / $imagewidth;
$thumbheight = $imageheight * $factor;
}
elseif ($imageheight >= $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
}
elseif ($imageheight == $imagewidth) {
$thumbheight = $thumbsize[1];
$thumbwidth = $thumbsize[0];
}

with this

if($imagewidth > $imageheight) {
$thumbwidth = $thumbsize[0];
$factor = $thumbsize[0] / $imagewidth;
$thumbheight = $imageheight * $factor;
$x = 0;
$y = round(($thumbsize[1]-$thumbheight)/2);
}
elseif($imageheight > $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
$x = round(($thumbsize[0]-$imagewidth)/2);
$y = 0;
}
elseif($imageheight==$imagewidth) {
$thumbheight = $thumbsize[1];
$thumbwidth = $thumbsize[0];
$y = 0;
$x = 0;
}


And replace this
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
with this
imagecopyresampled($thumb, $image, $x, $y, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);


That should do the trick :)

cybercampbell
11-13-2004, 01:08 AM
Thanks...you read my mind...wy next question was how to do the padding!

this is what I have now:
<?
$path='';
$path=$_GET['path'];
$thumbsize = explode("x", $_GET['size']);
if(isset($_GET['path'])&&isset($_GET['size'])&&substr($path, 0, 7) != "http://" && substr($path, -4)==('.jpg'||'.gif'||'.png') && file_exists($path)){
$imagesource = $path;
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (!$image) die();
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
if($imagewidth > $imageheight) {
$thumbwidth = $thumbsize[0];
$factor = $thumbsize[0] / $imagewidth;
$thumbheight = $imageheight * $factor;
$x = 0;
$y = round(($thumbsize[1]-$thumbheight)/2);
}
elseif($imageheight > $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
$x = round(($thumbsize[0]-$imagewidth)/2);
$y = 0;
}
elseif($imageheight==$imagewidth) {
$thumbheight = $thumbsize[1];
$thumbwidth = $thumbsize[0];
$y = 0;
$x = 0;
}
$white = ImageColorAllocate($thumb, 255, 255, 255);
ImageFill($thumb, 0, 0, $white);
$thumb = @imagecreatetruecolor($thumbsize[0],$thumbsize[1]);
imagecopyresampled($thumb, $image, $x, $y, 0, 0, $thumbwidth, $thumbheight, $imagewidth, $imageheight);
header('Content-type: image/jpeg');
imagejpeg($thumb);
imagedestroy($image);
imagedestroy($thumb);
} else { }
?>
did I do it correct?

The BG is still black and some of the Thumbnails centered but most are missing leaving only an empty black box.

any ideas?

Cheers
Chris

jebster1ca
11-13-2004, 02:55 AM
Oh, oops, sorry, typo in my one post, it should have been

Add this

$white = ImageColorAllocate($thumb, 255, 255, 255);
ImageFill($thumb, 0, 0, $white);

AFTER this
$thumb = @imagecreatetruecolor($thumbsize[0],$thumbsize[1]);

Just fix that and it should be good to go :)

Also, in that last chunk of PHP code, the semi-colon shouldn't be on a newline, it will work with it like that, but it should be on the end of the line before it, just for some reason the forum software keeps putting it on the next line:confused:

cybercampbell
11-13-2004, 07:10 AM
Thanks mate...the white is working. :D

Just a problem with the padding....the images with a longer y don't show up...I just get a white box.

Also....is it better to use 'imagecopyresampled' or 'imagecopyresized'?
imagecopyresampled seems to give better quality.

and just one more question...can I have a transparent background?

Thanks for your help

Chris

cybercampbell
11-13-2004, 11:59 AM
I found the problem...

THIS:
elseif($imageheight > $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
$x = round(($thumbsize[0]-$imagewidth)/2);
$y = 0;
}

Should have been
THIS:

elseif($imageheight > $imagewidth) {
$thumbheight = $thumbsize[1];
$factor = $thumbsize[1] / $imageheight;
$thumbwidth = $imagewidth * $factor;
$x = round(($thumbsize[0]-$thumbwidth)/2);
$y = 0;
}

Cheers
Chris

if you get a chance I had a few other questions in my last post.

:D :D :D :D :D :D :D

jebster1ca
11-13-2004, 12:19 PM
Transparent:
$transparent = imagecolortransparent($thumb);

This will not work with JPEG's however, cause they do not support transparency. You would have to use PNG or GIF formats instead, I would recommend PNG.

As for imagecopyresized vs imagecopyresampled, don't think it matters, though, like you said, on PHP.net it says "better quality could be obtained using imagecopyresampled()" :)