hakimserwa Posted July 19, 2012 Share Posted July 19, 2012 i am tryin to resize my image upoad for a thumbnail but i keep geting this error massege. Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in. icant seem to figure this out help mates this i the code $sql2 = mysql_query("INSERT INTO files (id, userid, fname, lname, email) VALUES ('', '$lastInsertedId', '$first_name', '$last_name', '$email_address')") or die('Error: ' . mysql_error()); $lastInsertedId = mysql_insert_id(); //lets make a thumb for all clients. $thumbDir = "uploads/thumbs/"; $thumbWidth = 200; $thumbHeight = 150; $fileExt = strtolower($fileExt); $originalImage = "uploads/$fileName"; $newThumb = $thumbDir . $lastInsertedId ."_". $fileName; $scale_ratio = $width / $height; if (($thumbWidth / $thumbHeight) > $scale_ratio) { $thumbWidth = $thumbHeight * $scale_ratio; } else { $thumbHeight = $thumbWidth / $scale_ratio; } if($type==2){ $oldImage = imagecreatefromjpeg($originalImage); }elseif($type==1){ $oldImage = imagecreatefromgif($originalImage); }elseif($type==3){ $oldImage = imagecreatefrompng($originalImage); } echo $width. " "; echo $height ." "; echo $thumbWidth. " "; echo$thumbHeight; $thumb = imagecreatetruecolor($thumbWidth, $thumbheight); imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height); imagejpeg($thumb, $newThumb); Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 I think you need to get the width and height of the old image. At the moment you are not defining the $width and $height variables. Above this line. imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height); add this. list($width,$height) = getimagesize($originalImage); Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted July 19, 2012 Author Share Posted July 19, 2012 ihave it it is seated at the to wright after the moveuploadfile move_uploaded_file($fileTmpLoc, "uploads/$fileName"); list($width, $height, $type, $attr) = getimagesize("uploads/$fileName"); Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 Try removing the following lines of code: $scale_ratio = $width / $height; if (($thumbWidth / $thumbHeight) > $scale_ratio) { $thumbWidth = $thumbHeight * $scale_ratio; } else { $thumbHeight = $thumbWidth / $scale_ratio; } and set the variables $thumbHeight and $thumbWidth with set values just for testing purposes. Does that work? Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted July 19, 2012 Author Share Posted July 19, 2012 do meant i must just write int values instead of valuables? Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 Remove . $scale_ratio = $width / $height; if (($thumbWidth / $thumbHeight) > $scale_ratio) { $thumbWidth = $thumbHeight * $scale_ratio; } else { $thumbHeight = $thumbWidth / $scale_ratio; } and replace with $thumbWidth = 100; $thumbHeight = 50; Does that work? Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted July 19, 2012 Author Share Posted July 19, 2012 no its no it still gives me the same error massege. but how is my code is it ok by the way? Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted July 19, 2012 Author Share Posted July 19, 2012 ihave seen the problem. it was lying in the new image height valuable, spelling error before it was $thumb = imagecreatetruecolor($thumbWidth, $thumbheight); and changed the h in the valuable it a capital H $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 Well there a few things bits that stood out. You have the following line of code: $fileExt = strtolower($fileExt); But you are not exactly checking the extension anywhere from what I have seen. You are also using a variable called type to select which type of image gets created but I can't see that defined anywhere in your code either. Why not check the file extension and then use that instead of type? Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Share Posted July 19, 2012 Aghh ok, this is normally the case with most php issues. Glad you got it working. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 19, 2012 Share Posted July 19, 2012 Off topic, but you can convert the following from this: if($type==2){ $oldImage = imagecreatefromjpeg($originalImage); }elseif($type==1){ $oldImage = imagecreatefromgif($originalImage); }elseif($type==3){ $oldImage = imagecreatefrompng($originalImage); } to this: $originalImage = "uploads/$fileName"; $string = file_get_contents($originalImage); $oldImage = imagecreatefromstring($string); <?php $sql2 = mysql_query("INSERT INTO files (id, userid, fname, lname, email) VALUES ('', '$lastInsertedId', '$first_name', '$last_name', '$email_address')") or die('Error: ' . mysql_error()); $lastInsertedId = mysql_insert_id(); //lets make a thumb for all clients. $thumbDir = "uploads/thumbs/"; $thumbWidth = 200; $fileExt = strtolower($fileExt); $originalImage = "uploads/$fileName"; $details = getimagesize($originalImage) or die("invalid image format."); $newThumb = $thumbDir . $lastInsertedId ."_". $fileName; $scale_ratio = $width / $height; $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $string = file_get_contents($originalImage); $oldImage = imagecreatefromstring($string); echo $width. " "; echo $height ." "; echo $thumbWidth. " "; echo$thumbHeight; $thumb = imagecreatetruecolor($thumbWidth, $thumbheight); imagecopyresampled($thumb, $oldImage, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height); imagejpeg($thumb, $newThumb); Quote Link to comment Share on other sites More sharing options...
hakimserwa Posted July 19, 2012 Author Share Posted July 19, 2012 Well there a few things bits that stood out. You have the following line of code: $fileExt = strtolower($fileExt); But you are not exactly checking the extension anywhere from what I have seen. You are also using a variable called type to select which type of image gets created but I can't see that defined anywhere in your code either. Why not check the file extension and then use that instead of type? i had it taken care of from the top even though it wasnt in the script that i posted. Quote Link to comment 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.