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";
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.