Hi all,
I have been given a function that reads a folder and then creates thumbnails for every image in that folder, placing the thumb elsewhere. I now need to modify it so that it looks for one image only, and the value of that image is stored in a database. the script works, until now. Therefore I have posted it before, and then after so you can see where I am at:
$pathToImages = "files/";
$pathToThumbs = "thumbs/";
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname1 = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $image);
// continue only if this is a JPEG image
if (( strtolower($info['extension']) == 'jpg' )|| ( strtolower($info['extension']) == 'gif' ))
{
echo "Creating thumbnail for {$image} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$image}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$image}" );
}
}
// close the directory
closedir( $dir );
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("files/","thumbs/",100);
}
Now here it is with $image1 incorporated and it is not working, I cannot echo any of the variables including $info, $image1 to begin debugging, I have no error messages and no thumbs created in the thumbs folder, please see any notes in CAPITALS:
$pathToImages = "files/";
$pathToThumbs = "thumbs/";
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
//////NOTICE HERE THAT I HAVE COMMENTED OUT THE PART WHERE IT SEARCHES THE ENTIRE DIRECTORY, MAYBE I SHOULD LEAVE IT IN?
//while (false !== ($fname1 = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $image1);
// continue only if this is a JPEG image
if (( strtolower($info['extension']) == 'jpg' )|| ( strtolower($info['extension']) == 'gif' ))
{
echo "Creating thumbnail for {$image1} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$image1}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$image1}" );
}
}
// close the directory
closedir( $dir );
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("files/","thumbs/",100);
}
Any ideas?
Thanks,
G