I need to take sequence of images as input from a folder and after resizing, the resized image should be stored on an output folder. I wrote code. But in my code after resizing image, output folder only has a black image.
To debug this code please create input & output folder and put some gif image on input folder and see the output on output folder. Please 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); ?>
Your error is in this line:
Your images are in the input folder, but this is setting the variable to just the image name, so when you try calling
imagecreatefromgifandgetimagesizeit is not finding an image, resulting in the black output you are getting. To fix, just add the correct directory before the name: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:
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.