1

I have a code to upload an image, then creates thumbnail on the fly for this uploaded image.

The following code that creates the thumbnail (thumb.php).

$fileName = filter_input(INPUT_GET, 'src');
$width = filter_input(INPUT_GET, 'width', FILTER_SANITIZE_NUMBER_INT);
$height = filter_input(INPUT_GET, 'height', FILTER_SANITIZE_NUMBER_INT);

$im = imagecreatefromjpeg($fileName); //<=== the error message indicates to this line
$tmp_im = imagecreatetruecolor($width, $height);
$imWidth = imagesx($im);
$imHeight = imagesy($im);
imagecopyresampled($tmp_im, $im, 0, 0, 0, 0, $width, $height, $imWidth, $imHeight);
header("Content-type: image/jpeg");
imagejpeg($tmp_im);
imagedestroy($im);
imagedestroy($tmp_im);

The following code that upload the image, then implement the creation of thumbnail for the uploaded image, then display this thumbnail.

require_once ('thumb.php');

$image_dir = "images/";
if($_FILES['photo']['error'] == 0){  
    $fileUPloaded = $_FILES['photo']['name'];
    move_uploaded_file($_FILES['photo']['tmp_name'], $image_dir.$fileUPloaded);
    echo "<a href='".$image_dir.$fileUPloaded."'><img src='thumb.php?src=".$image_dir.$fileUPloaded."&width=350&height=250' alt='' /></a>";
}

Now, why appears an error Warning: imagecreatefromjpeg(): Filename cannot be empty.

Note that the image is uploaded, and was created thumbnail for uploaded image, and was displayed thumbnail. so, why that error message appear ?

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • If you do `var_dump($filename)` does a file by that name actually exist? Do you have the path information you need as part of the name? Also what are you getting out of doing filter_input without specifying a filter? I would say you want to be very careful about any file name of a file you have uploaded. – Elin Jan 15 '15 at 04:50
  • @Elin: Thanks, no, gives me `null` value, but how `null` and it doing everything ?. regarding filter_input I have put just an example not the final code. – Lion King 1 min ago edit – Lion King Jan 15 '15 at 05:01
  • No one can tell you what is wrong with your code if you are not showing your code. If you are getting a null that's obviously the problem, just exactly as the error message said. Figure out why that is. What happens when you dump echo INPUT_GET? – Elin Jan 15 '15 at 05:41
  • @Elin: gives me `int 1`. – Lion King Jan 15 '15 at 05:49
  • Try `var_dump(isset($_GET['src']);` – Elin Jan 15 '15 at 14:05
  • @Elin: gives me `boolean false`. – Lion King Jan 15 '15 at 14:25
  • Well that's the problem then, you need to make sure that you have src. – Elin Jan 15 '15 at 14:46
  • @Elin: [**Thank you**]. I have been solved the problem. you must first check if the `$_GET['src']` has a value or no, and thus, I have used `isset()` function to check if there is a value or no. that's it. – Lion King Jan 15 '15 at 15:28

0 Answers0