...

View Full Version : A Little GD problem with image re-sizing



Y-STU-K
02-06-2003, 12:42 AM
Ok i'm trying to upload an image, save it and save a thumbnail image too. Now heres the first bit i've done which will just save it as a thumbnail but its not working :S can anyone suggest what i've done wrong ?


<?php

if (isset($_POST["Submit"]))
{
$path = "/images";
$size_w = 100;
$size_h = 75;

header("Content-type: image/jpeg");
$src_img = ImageCreateFromJpeg($_FILES["upload"]);
$dst_img = ImageCreate($size_w, $size_h);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $size_w, $size_h, ImageSX($src_img),ImageSY($src_img));
Imagejpeg($dst_img, $path, 100);
}
else
{
?>

<form name="form1" enctype="multipart/form-data" method="post" action="<?PHP $PHP_SELF; ?>">
<input name="upload" type="file" id="upload">
<input type="submit" name="Submit" value="Submit">
</form>

<?php
}

?>

oh one more thing. I'm currently having to develop this on my hosts web server as i can't get GD working in PHP on my localhost any ideas on that too ?

Thanx

Stu :)

Jesh
02-06-2003, 04:02 AM
Hey

I think GD is installed on firepages package

http://www.firepages.com.au/

Jesh

Y-STU-K
02-06-2003, 10:39 AM
i don't actually want to install an entire package thing i just need to know how to setup GD on a windows box thats all.

But more importantly why the above script is producing an error when its run on a server with GD installed?

Íkii
02-06-2003, 11:43 AM
with your inline script you'd probably get issues with headers - attempting to send a http header after an image one would glitch - might be ok as no browser output is really sent - iffy though.

also, referencing the resource file as $_FILES['upload'] isn't enough. Until you move it from /tmp/ it would be refered to as $_FILES['upload']['tmp_name'] though I suggest you move it from /tmp/ prior to thumbnailing it

so....

{
move_uploaded_file($_FILES['upload']['tmp_name'],'folder/newname.jpg');
$path = "/images";
$size_w = 100;
$size_h = 75;

ob_start();
header("Content-type: image/jpeg");
$src_img = ImageCreateFromJpeg('folder/newname.jpg');
$dst_img = ImageCreate($size_w, $size_h);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $size_w, $size_h, ImageSX($src_img),ImageSY($src_img));
Imagejpeg($dst_img, $path, 100);
ob_end_clean();
}

Also - type test before moving, size test before moving, just do all you can to assure the uploaded file is exactly what you want.
Note: you'd probably want to create a name for the uploaded image based on other params.

Last note: ImageCreate (certainly within old builds of GD) is limited to a 256 colour palette. If you have GD2+ use imagecreatetruecolor and imagecopyresampled - else scamper over to sitepoint and search for a php tut on GD

Íkii
02-06-2003, 11:47 AM
One last note:

ImageJPEG($dst_img, $path, 100);

would effectively try to save the thumb as /images

perhaps

ImageJPEG($dst_img,$path.'/file.jpg',80);

would be better.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum