devdavad Posted March 31, 2009 Share Posted March 31, 2009 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/ Share on other sites More sharing options...
MadTechie Posted March 31, 2009 Share Posted March 31, 2009 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-798205 Share on other sites More sharing options...
devdavad Posted March 31, 2009 Author Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-798225 Share on other sites More sharing options...
MadTechie Posted March 31, 2009 Share Posted March 31, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-798228 Share on other sites More sharing options...
devdavad Posted April 3, 2009 Author Share Posted April 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-800082 Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 that suggests that imagecreatefromXXX isn't working.. thus checking to see if the file exists would be my first step.. Did you try that script mod ? Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-800133 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-800137 Share on other sites More sharing options...
devdavad Posted April 8, 2009 Author Share Posted April 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151990-solved-i-need-help-creating-a-thumbnail-with-gd/#findComment-804095 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.