views:

224

answers:

1

Hi experts,

I need to take sequence of images as input from a folder and after resizing the resized image should store on output folder. I write a code. But in my code after resizing image, out put folder only show black image.

To debug this code pls create input & output folder and put some gif image on input folder and see the output on output folder.

Pls HELP!

<?php

$dir=opendir("input/");
$i=0;
while($imgfile=readdir($dir))
{
     if ($imgfile != "." && $imgfile!="..")
         {
        $imgarray[$i]=$imgfile;
    $newwidth=240; //new width
    $newheight=320; //new height
    $uploadedfile = $imgarray[$i];
    $src = imagecreatefromgif($uploadedfile);
    list($width,$height)=getimagesize($uploadedfile);
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $filename = "output/".$imgarray[$i];
    imagegif($tmp,$filename,100);
    imagedestroy($src);
    imagedestroy($tmp);
    $i++;
        }
}
closedir($dir);
?>
+2  A: 

Your error is in this line:

$uploadedfile = $imgarray[$i];

Your images are in the input folder, but this is setting the variable to just the image name, so when you try calling imagecreatefromgif and getimagesize it is not finding an image, resulting in the black output you are getting. To fix, just add the correct directory before the name:

$uploadedfile = "input/" . $imgarray[$i];

Tested with this change and it works.

As a side note, I was able to quickly find what was wrong with your code by having error_reporting on:

error_reporting(E_ALL);
ini_set('display_errors', 1);

By doing this, you will get warning messages and such that you may not see otherwise. These can be the difference between a quick fix and tearing your hair out for hours. Use it when you are debugging.

Paolo Bergantino
OH, GREAT. It's working fine.Thanks bro.Thanks a lot.
riad
can u pls help me to use:error_reporting(E_ALL);ini_set('display_errors', 1);How I use it or add the code on which portion? i don’t use it before.
riad
You would add the code at the very top of your script. You could also modify your php.ini so that by default this is the behavior (as I believe it is) and then modify it on the top so that it is not when you are in production.
Paolo Bergantino