0

This is currently what I have. When I include it in my index.php and then call the function on pageload, I get a blank page. So something is wrong here, but I don't know what. I feel like I'm really close though. I just want to create thumbnails of images in a directory, and then show them in HTML as a list of images you can click that trigger lightboxes.

I'm still really shaky in PHP. I'm trying to wrap my head around editing images in a directory.

<?php
function buildThumbGallery(){

    $h = opendir('/Recent_Additions/'); //Open the current directory
    while (false !== ($curDir = readdir($h))) {
        if (!file_exists('/Recent_Additions/thumbs/')) {
            $thumbDir = mkdir('/Recent_Additions/thumbs/', 0777, true);
        }else{
            $thumbDir = '/Recent_Additions/thumbs/';
        }
        $width = 200;
        $height = 200;
        foreach ($curDir as $image) {
            $filePath = $curDir."/".$image;
            $genThumbImg = $image->scaleImage($width, $height, true);
            $newThumb = imagejpeg($genThumbImg, $thumbDir, 100);

            echo '<li> <a class="fancybox" data-fancybox-group="'.basename($curDir).'" href="'.$filePath.'" title="'.basename($curDir)." ".strpbrk(basename($filePath, ".jpg"), '-').'"><img src="'.$newThumb.'"/>'.basename($curDir).'</a>';
            imagedestroy($newThumb);
        }echo '</li>';
}
?>
dotcommer
  • 171
  • 1
  • 11
  • It seems you are not saving images after their manipulation... Maybe you missed some call to a save method or something similar. – Aerendir Dec 12 '15 at 09:34
  • I wrote a related post some time ago http://www.picssel.com/create-a-filtered-image-gallery-with-jquery-and-fancybox-part-2-create-image-thumbnails-with-php/ – JFK Dec 21 '15 at 20:44

2 Answers2

1

You are doing several things wrong:

  • You're not closing the while loop.
  • Readdir already loops through a directory, your foreach is not doing anything.
  • You are missing quotes in your echo.
  • You are calling the method scaleImage on a string, I think you meant to call the function imagescale.

You're missing and misunderstanding a lot of stuff, take a look at how to create a thumbnail here: https://stackoverflow.com/a/11376379/4193448 Also see if you can enable PHP errors, getting a blank page while your code is full of errors is not really helping is it?

Community
  • 1
  • 1
mtricht
  • 427
  • 4
  • 16
  • Thanks for the advice. I'll take another pass at it with your suggestions. I appreciate it. – dotcommer Dec 12 '15 at 17:02
  • If you've got a moment, I've posted a new answer with code thats working, but not displaying my images, and I'd appreciate your input on it. Thanks! – dotcommer Dec 13 '15 at 04:45
0

::EDIT::

With help from @swordbeta, I got my script working properly. Here is the code for future reference:

<?php
function buildThumbGallery(){

    $curDir = "./Recent_Additions/";
    $thumbsPath = $curDir."thumbs/";

    if (!file_exists($thumbsPath)) {
        mkdir($thumbsPath, 0777, true);
    }
    foreach(scandir($curDir) as $image){
        if ($image === '.' || $image === '..' || $image === 'thumbs') continue;
        if(!file_exists($thumbsPath.basename($image, ".jpg")."_thumb.jpg")){
            // Max vert or horiz resolution
            $maxsize=200;

            // create new Imagick object
            $thumb = new Imagick($curDir.$image);  //'input_image_filename_and_location'

            $thumb->setImageFormat('jpg'); 

            // Resizes to whichever is larger, width or height
            if($thumb->getImageHeight() <= $thumb->getImageWidth()){
            // Resize image using the lanczos resampling algorithm based on width
                $thumb->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
            }else{
                // Resize image using the lanczos resampling algorithm based on height
                $thumb->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
            }

            // Set to use jpeg compression
            $thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
            $thumb->setImageCompressionQuality(100);
            // Strip out unneeded meta data
            $thumb->stripImage();
            // Writes resultant image to output directory
            $thumb->writeImage($thumbsPath.basename($image, ".jpg")."_thumb.jpg");  //'output_image_filename_and_location'
            // Destroys Imagick object, freeing allocated resources in the process
            $thumb->destroy();
        }else{
            echo '<a class="fancybox" data-fancybox-group="'.basename($curDir).'" href="'.$curDir.basename($image, "_thumb.jpg").'" title="Recent Addition - '.basename($image, ".jpg").'"><img src="'.$thumbsPath.basename($image, ".jpg")."_thumb.jpg".'"/></a>';
            echo '<figcaption>'.basename($image, ".jpg").'</figcaption>' . "<br/>";   
        }
    } 
}
?>

::Original Post::

Ok, after going back and doing some more research and suggestions from @swordbeta, i've got something that works. My only issue now is I can't get the images to show in my index.php. I'll style the output in CSS later, right now I just want to see the thumbnails, and then later build them into lightbox href links:

<?php
function buildThumbGallery(){

    $curDir = "./Recent_Additions/";
    $thumbsPath = $curDir."/thumbs/";

    if (!file_exists($thumbsPath)) {
        mkdir($thumbsPath, 0777, true);
    }

    $width = 200;

    foreach(scandir($curDir) as $image){
        if ($image === '.' || $image === '..') continue;
        if(file_exists($thumbsPath.basename($image)."_thumb.jpg")){
            continue;
        }else{
            // Max vert or horiz resolution
            $maxsize=200;

            // create new Imagick object
            $thumb = new Imagick($curDir.$image);  //'input_image_filename_and_location'

            // Resizes to whichever is larger, width or height
            if($thumb->getImageHeight() <= $thumb->getImageWidth()){
            // Resize image using the lanczos resampling algorithm based on width
                $thumb->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
            }else{
                // Resize image using the lanczos resampling algorithm based on height
                $thumb->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
            }

            // Set to use jpeg compression
            $thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
            $thumb->setImageCompressionQuality(100);
            // Strip out unneeded meta data
            $thumb->stripImage();
            // Writes resultant image to output directory
            $thumb->writeImage($thumbsPath.basename($image)."_thumb.jpg");  //'output_image_filename_and_location'
            // Destroys Imagick object, freeing allocated resources in the process
            $thumb->destroy();
        }
    }   echo '<img src="'.$thumbsPath.basename($image)."_thumb.jpg".'" />' . "<br/>";
}
?>

At the moment, the output from the echo isn't showing anything, but the rest of the script is working properly (i.e. generating thumbnail images in a thumbs directory).

I'm guessing i'm not formatting my echo properly. This script is called in my index.php as <?php buildThumbGallery(); ?> inside a styled <div> tag.

dotcommer
  • 171
  • 1
  • 11
  • Ok, after doing some digging and finally figuring out that PHP generates an error_log, it seems right at `$thumb = new Imagick($curDir.$image);` there is an issue with Imagick. The error is: no decode delegate for this image format After some googling, it seems like maybe my server doesn't support imagick or something? But its still able to create my thumbnails, it just throws a fatal error that borks my entire layout when its called... I'm not sure what to do about this. Any thoughts? – dotcommer Dec 13 '15 at 07:50
  • 1
    I think you're getting that error because you're trying to create a thumbnail from a directory as you are looping through the Recent_additions dirrctory which also happens to have a thunbs directory. Add an or condition with `$image == 'thumbs'` to skip it. – mtricht Dec 13 '15 at 09:49
  • Of course! I totally see it now, based on the logic of my loop. Thank you so much for your help. I wish I could up-vote you, but SO won't let me because my account is too new. – dotcommer Dec 13 '15 at 19:10