Jump to content

[SOLVED] I need help creating a thumbnail with GD


devdavad

Recommended Posts

I'm creating a photo uploader for my site and when I run the code below it only creates a black thumbnail. By the way I got this method for making the thumbnail from a sample on php.net.

# $uploadedPictExt is a string of the period and the file extension
# create the thumbnail
list($width, $height) = getimagesize("../photos/" . $_POST["photo_name"] . $uploadedPictExt);
$thumbnailGD = imagecreatetruecolor(200, 200);
# chooses the right imagecreatefrom function
if($uploadedPictExt == ".png") {
$uploadedPictGD = imagecreatefrompng("../photos/" . $_POST["photo_name"] . $uploadedPictExt);
imagecopyresampled($thumbnailGD, $uploadedPictGD, 0, 0, 0, 0, $width, $height);
if(!imagepng($thumbnailGD, "../photos/thumbs/" . $_POST["photo_name"] . $uploadedPictExt, 9)) {
	echo "Thumbnail for png failed";
}
}
else if($uploadedPictExt == ".gif") {
$uploadedPictGD = imagecreatefromgif("../photos/" . $_POST["photo_name"] . $uploadedPictExt);
imagecopyresampled($thumbnailGD, $uploadedPictGD, 0, 0, 0, 0, $width, $height);
if(!imagegif($thumbnailGD, "../photos/thumbs/" . $_POST["photo_name"] . $uploadedPictExt)) {
	echo "Thumbnail for gif failed";
}
}
else {
# must be a .jpg of some sorts
$uploadedPictGD = imagecreatefromjpeg("../photos/" . $_POST["photo_name"] . $uploadedPictExt);
imagecopyresampled($thumbnailGD, $uploadedPictGD, 0, 0, 0, 0, $width, $height);
if(!imagejpeg($thumbnailGD, "../photos/thumbs/" . $_POST["photo_name"] . $uploadedPictExt, 100)) {
	echo "Thumbnail for jpeg failed";
}
}

As a guess.. (as it looks okay try this)

 

<?php
//add at the start.
$uploadedPictExt = strtolower($uploadedPictExt);
if(!file_exists("../photos/" . $_POST["photo_name"] . $uploadedPictExt))
{
die("Error: file not found");
}

?>

As a guess.. (as it looks okay try this)

 

<?php
//add at the start.
$uploadedPictExt = strtolower($uploadedPictExt);
if(!file_exists("../photos/" . $_POST["photo_name"] . $uploadedPictExt))
{
die("Error: file not found");
}

?>

 

Actually when I execute it on my windows server it does not return any errors. I mean the actual thumbnail image is created and saved on the server, it's just all black.

that suggests that

imagecreatefromXXX isn't working.. thus checking to see if the file exists would be my first step..

do you have error_reporting on ?

I'm on godaddy shared hosting and unfortunately their php setup does not display any errors even with error_reporitng(0). Because of this I also can't correct this with the server's php.ini.

This is your line of code:

imagecopyresampled($thumbnailGD, $uploadedPictGD, 0, 0, 0, 0, $width, $height);

 

This is my line of code in my script:

imagecopyresampled($tmp,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight);

 

See the difference?

 

You're missing the source width and height - might be worth adding this and see if it make a difference.

This is your line of code:

imagecopyresampled($thumbnailGD, $uploadedPictGD, 0, 0, 0, 0, $width, $height);

 

This is my line of code in my script:

imagecopyresampled($tmp,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight);

 

See the difference?

 

You're missing the source width and height - might be worth adding this and see if it make a difference.

 

Thanks, solved.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.