-3

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" }
expiredninja
  • 1,397
  • 4
  • 25
  • 45
  • 2
    "Permission denied". do you have the right permissions set? – Paul Dessert Apr 26 '12 at 22:22
  • +1 to Paul.. if you will fix permissions, the second error will be fixed too :) – Anton Apr 26 '12 at 22:28
  • Why did you not accept if you would using my script http://stackoverflow.com/questions/10339863/how-to-automatically-copy-an-entire-directory-as-thumbnails-using-phpthumb-or-s ???? you should have also brought of new issues there instead of duplicate – Baba Apr 26 '12 at 22:29
  • sorry i wasn't sure how to structure the issue. people don't always follow up. I'll do it your way from here out. I will most likely accept your answer as soon as everything irons out. – expiredninja Apr 26 '12 at 22:40

1 Answers1

2

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 );
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • /images has .png's and /thumbnails is empty. – expiredninja Apr 27 '12 at 23:22
  • add `var_dump($images)` after `$images = scandir ( $dir );` lest see what scandir is getting – Baba Apr 27 '12 at 23:24
  • `Thumbs.db` is not a valid image you need to filter it out – Baba Apr 28 '12 at 02:47
  • I think there might be some differencesbetween the code we're using. I'm not using mysite/images or thumbnails/images as a directory style, it says they are not valid. What happened to foreach ($images as $image)?... Notice: Undefined variable: image in C:\xampp\htdocs\mysite\save_differentformat.php on line 61. – expiredninja Apr 28 '12 at 03:59
  • I made a mistake when updating the code .. hold i'll correct it now – Baba Apr 28 '12 at 11:10
  • I added `$errors` this would help you capture the errors so that you can see why it did not copy ... – Baba Apr 28 '12 at 21:34
  • it's now slowly printing a single thumbnail to the page. – expiredninja Apr 28 '12 at 23:07
  • yes because we called `$thumb->show ();` and also if you echo `echo $error . "
    ";` the image might not show properly .. that was just to show you how to see the errors ...
    – Baba Apr 28 '12 at 23:12
  • ok it works, but Windows isn't applying icons to the thumbnails. It seems kinda slow too. Thanks for your help! It's given me a lot to study. – expiredninja Apr 28 '12 at 23:32