I wanted a script that automatically generates thumbnails when from a folder full of larger images. I've ran into some problems implementing it.
I was getting some errors early on because I hadn't included all the phpthumb files i needed, but now the errors are coming from within the phpthumb code itself. Will post more code as needed.
The errors:
Warning: getimagesize(.\.) [function.getimagesize]: failed to open stream: Permission denied in C:\xampp\htdocs\mysite\GdThumb.inc.php on line 1070
Fatal error: Uncaught exception 'Exception' with message 'File is not a valid image: .\.' in C:\xampp\htdocs\mysite\ThumbBase.inc.php:193 Stack trace: #0 C:\xampp\htdocs\mysite\GdThumb.inc.php(1081): ThumbBase->triggerError('File is not a v...') #1 C:\xampp\htdocs\mysite\GdThumb.inc.php(98): GdThumb->determineFormat()
#2 C:\xampp\htdocs\mysite\ThumbLib.inc.php(127): GdThumb->__construct('.\.', Array, false) #3 C:\xampp\htdocs\mysite\save_differentformat.php(47): PhpThumbFactory::create('.\.') #4 {main} thrown in C:\xampp\htdocs\mysite\ThumbBase.inc.php on line 193
the script:
require_once '../mysite/ThumbLib.inc.php';
$dir = "../mysite/images" ;
$destination = "../mysite/thumbnails" ;
$images = scandir($dir);
foreach ($images as $image)
{
$ext = pathinfo($dir . DIRECTORY_SEPARATOR . $image,PATHINFO_EXTENSION);
$thumb = PhpThumbFactory::create($image . DIRECTORY_SEPARATOR. $image);
$thumb->adaptiveResize(100, 100);
$thumb->save($destination . DIRECTORY_SEPARATOR. $image, $ext);
$thumb->show();
}
?>
EDIT
Here's the error messages i'm getting right now. It doesn't know what $mode is and i may have a mix of image types in that folder.
Notice: Undefined variable: mode in C:\xampp\htdocs\mysite\save_differentformat.php on line 51
Warning: getimagesize(.\.) [function.getimagesize]: failed to open stream: Permission denied in C:\xampp\htdocs\mysite\GdThumb.inc.php on line 1070
Fatal error: Uncaught exception 'Exception' with message 'File is not a valid image: .\.' in C:\xampp\htdocs\mysite\ThumbBase.inc.php:193 Stack trace: #0 C:\xampp\htdocs\mysite\GdThumb.inc.php(1081): ThumbBase->triggerError('File is not a v...') #1 C:\xampp\htdocs\mysite\GdThumb.inc.php(98): GdThumb->determineFormat()
#2 C:\xampp\htdocs\mysite\ThumbLib.inc.php(127): GdThumb->__construct('.\.', Array, false) #3 C:\xampp\htdocs\mysite\save_differentformat.php(64): PhpThumbFactory::create('.\.') #4 {main} thrown in C:\xampp\htdocs\mysite\ThumbBase.inc.php on line 193
Here's what scandir() is returning:
array(19) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(36) "13912d3e8b4ae1890457fe935cc0c7fe.png" [3]=> string(36) "266c484ca488d053255c7767af212bee.png" [4]=> string(36) "8af941c37874dc3a7edee2772b60c323.png" [5]=> string(9) "Thumbs.db" [6]=> string(15) "butterflies.png" [7]=> string(22) "butterfliesinverse.png" [8]=> string(11) "favicon.png" [9]=> string(36) "fb9e25f57d12ec5019aa548665f34fa5.png" [10]=> string(20) "image1.png" [11]=> string(25) "image11 (1).png" [12]=> string(21) "image11.png" [13]=> string(25) "image12 (1).png" [14]=> string(21) "image12.png" [15]=> string(21) "image15.png" [16]=> string(20) "image2.png" [17]=> string(21) "image25.png" [18]=> string(21) "image28.png" }
OK Apology accepted
A. It not really advice able to use ../
when dealing with path .. this can lead to a lot of issues my advice is use real path
B. Always check if the folder exist and you can write to the folder before performing file operations
Example
$dir = "mysite/images";
$destination = "mysite/thumbnails";
$mode = 0777;
if (! is_dir ( $dir )) {
die ( "Source Direcotry Does not exist : $dir" );
}
if (! is_readable ( $dir )) {
die ( "Source Direcotry Does not readable : $dir" );
}
if (! mkdir_recursive ( $destination, $mode )) {
die ( "Can't wite create $destination" );
}
$images = scandir ( $dir );
$allowedExtention = array (
"jpg",
"gif",
"png"
);
$errors = array ();
foreach ( $images as $image ) {
$imageFile = $dir . DIRECTORY_SEPARATOR . $image;
if (is_file ( $dir . DIRECTORY_SEPARATOR . $image )) {
if ($image == "." || $image == "..") {
continue;
}
$ext = pathinfo ( $dir . DIRECTORY_SEPARATOR . $image, PATHINFO_EXTENSION );
if (! in_array ( $ext, $allowedExtention )) {
continue;
} else {
$errors [] = $imageFile . " has invalid extention $ext";
}
try {
$thumb = PhpThumbFactory::create ( $dir . DIRECTORY_SEPARATOR . $image );
$thumb->adaptiveResize ( 100, 100 );
$thumb->save ( $destination . DIRECTORY_SEPARATOR . $image, $ext );
$thumb->show ();
} catch ( Exception $e ) {
$errors [] = "PhpThumbFactory : " . $e->getMessage ();
}
} else {
$errors [] = $imageFile . " not a valid File";
}
}
// Check for errors
if (count ( $errors )) {
foreach ( $errors as $error ) {
echo $error . " <br />";
}
}
function mkdir_recursive($pathname, $mode) {
is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
}