Jump to content

A black image is created instead of thumbnails


upendra470

Recommended Posts

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.

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/

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.

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.