upendra470 Posted July 10, 2011 Share Posted July 10, 2011 I have been trying for so long but not got any success. When i try to make thumbnails for an array of images, a blank image of size same as that of thumbnail is created. No image is shown. Here is my code: <?php include "session.php"; include "connect.php"; if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>8097152){ echo 'Upload FAILED, file is too large !'; exit(); } else{ $n_width = 100; $n_height = 100; if(count($_FILES["image_id"]["name"]) > 0){ for($j=0; $j < count($_FILES["image_id"]["name"]); $j++){ if ((($_FILES["image_id"]["type"][$j] == "image/gif") || ($_FILES["image_id"]["type"][$j] == "image/jpeg") || ($_FILES["image_id"]["type"][$j] == "image/pjpeg")) && ($_FILES["image_id"]["size"][$j] < 40000000)) { if ($_FILES["image_id"]["error"][$j] > 0) { echo "Return Code: " . $_FILES["image_id"]["error"][$j] . "<br />"; } else { //echo "Upload: " . $_FILES["image_id"]["name"] . "<br />"; //echo "Type: " . $_FILES["image_id"]["type"] . "<br />"; //echo "Size: " . ($_FILES["image_id"]["size"] / 1024) . " Kb<br />"; // echo "Temp file: " . $_FILES["image_id"]["tmp_name"] . "<br />"; if (file_exists("upload/gallery/" . $_FILES["image_id"]["name"][$j])) { echo $_FILES["image_id"]["name"][$j] . " already exists. "; } else { move_uploaded_file($_FILES["image_id"]["tmp_name"][$j], "upload/gallery/" . $_FILES["image_id"]["name"][$j]); echo "Stored in: " . "upload/gallery/" . $_FILES["image_id"]["name"][$j]; $tsrc = "upload/thumbs/".$_FILES["image_id"]["name"][$j]; $im = imagecreatefromjpeg($_FILES["image_id"]["name"][$j]); $width = imagesx($im); $height = imagesy($im); $newimage = imagecreatetruecolor($n_width,$n_height); imagecopyresampled($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height); imagejpeg($newimage, $tsrc); //chmod("$tsrc", 0777); } } } else { echo "Invalid file"; } } } } $category = $_POST['category']; $desc = $_POST['desc']; for($j=0; $j < count($_FILES["image_id"]["name"]); $j++){ $pic_name = $_FILES["image_id"]["name"][$j]; $sql= "insert into gallery (category, pic_desc, pic_name) values('$category', '$desc', '$pic_name')"; $result = mysql_query($sql) or die(mysql_error()); } if($result){ //echo "<h4 style='color:green;'>Picture uploaded sucessfully</h4>"; //echo "<meta http-equiv=\"refresh\" content=\"1;URL=gallery.php\" />"; header("location: newsadded.php"); } else{ echo " News cannot be added"; } ?> Please tell me where i am wrong. Quote Link to comment https://forums.phpfreaks.com/topic/241594-a-black-image-is-created-instead-of-thumbnails/ Share on other sites More sharing options...
QuickOldCar Posted July 10, 2011 Share Posted July 10, 2011 This most likely works if all your images are .jpg, but not others. GD manual http://php.net/manual/en/book.image.php look at the imagecreatefrom functions imagecreatefromgif — Create a new image from gif imagecreatefromjpeg — Create a new image from jpg,jpeg imagecreatefrompng — Create a new image from png bmp would need to be converted first if need something that for sure resizes a jpg image <?php $file = 'original.jpg'; $save = 'new.jpg'; echo "Created new image $save"; $size = 0.50;//resize % list($width, $height) = getimagesize($file) ; $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; ?> If want an already made and good script for image resizing all types http://phpthumb.sourceforge.net/ Quote Link to comment https://forums.phpfreaks.com/topic/241594-a-black-image-is-created-instead-of-thumbnails/#findComment-1240933 Share on other sites More sharing options...
upendra470 Posted July 10, 2011 Author Share Posted July 10, 2011 Thanks for replying but i have uploaded a jpg image and still the same thumbnail was produced i.e a small black image. Quote Link to comment https://forums.phpfreaks.com/topic/241594-a-black-image-is-created-instead-of-thumbnails/#findComment-1240944 Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2011 Share Posted July 10, 2011 You are using the uploaded file "name" in the imagecreatefrom... statement - $im = imagecreatefromjpeg($_FILES["image_id"]["name"][$j]); That's not where the image is at. You moved it to - "upload/gallery/" . $_FILES["image_id"]["name"][$j] and that is what you would need to put into the imagecreatefrom... statement. Quote Link to comment https://forums.phpfreaks.com/topic/241594-a-black-image-is-created-instead-of-thumbnails/#findComment-1240945 Share on other sites More sharing options...
upendra470 Posted July 11, 2011 Author Share Posted July 11, 2011 I changed the path also still not getting the desired result. Same black image of size thumbnail is produced Quote Link to comment https://forums.phpfreaks.com/topic/241594-a-black-image-is-created-instead-of-thumbnails/#findComment-1241241 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.