Page 1 of 1

Thumbnails

Posted: Wed Mar 23, 2005 7:17 pm
by Steveo31
Why doesn't this code make thumbnails?

Code: Select all

<?php
$dir = opendir("images");
$thumb_dir = opendir("images/thumbs");

while(false !== ($file = readdir($dir)) && (false !== ($thumb = readdir($thumb_dir)))){
    if($file != $thumb){

        $image_path = "images/".(basename($file));
        $target = "images/thumbs/".(basename($file));
        
        $large_image = $image_path;
        $large_size = GetImageSize($image_path);

        $large_maxwidth = 120;
        $large_maxheight = 95;

        $large_width = $large_size[0];
        $large_height = $large_size[1];

        //start sizing
        $large_x_ratio = $large_maxwidth/$large_width;
        $large_y_ratio = $large_maxheight/$large_height;

        if(($large_width <= $large_maxwidth) && ($large_height <= $large_maxheight)){
            $large_tn_width = $large_width;
            $large_tn_height = $large_height;
        }elseif(($large_x_ratio * $large_height) < $large_maxheight){
            $large_tn_height = ceil($large_x_ratio * $large_height);
            $large_tn_width = $large_maxwidth;
        }else{
            $large_tn_width = ceil($large_y_ratio * $large_width);
            $large_tn_height = $large_maxheight;
        }   
        //end sizing issues
        $large_src = ImageCreateFromJpeg($large_image);
        $large_dst = imageCreateTrueColor($large_tn_width, $large_tn_height);
    
        ImageCopyResampled($large_dst, $large_src, 0, 0, 0, 0, $large_tn_width, $large_tn_height, $large_width, $large_height);
        ImageJpeg($large_dst, "$target", 60);
        
        ImageDestroy($large_src);
        ImageDestroy($large_dst);
    }
}
?>

Posted: Wed Mar 23, 2005 7:23 pm
by Ambush Commander
Things that jump out to me:

Code: Select all

ImageJpeg($large_dst, "$target", 60);
The quotes around $target are unnecessary, although it doesn't make a syntactic difference.

Another thing is that you're not issuing a proper content header for this thumbnail. What exactly do you mean by "not make thumbnails"? Does that me you see a bunch of gibberish on the screen, or perhaps there's no output at all (if so, then response headers would be helpful)

Posted: Wed Mar 23, 2005 7:51 pm
by feyd
it doesn't generate thumbnails to the screen, they are written to the server's thumbnail directory, as listed above..

Posted: Fri Mar 25, 2005 2:20 am
by Steveo31
feyd wrote:it doesn't generate thumbnails to the screen, they are written to the server's thumbnail directory, as listed above..
See thats what I would have thought but it doesn't. Ambush- Sorry for the ambigiuity.. thought about that just before I opened the post.

There's no anythign. Almost as if the script doesn't work at all.

So there's nothing you guys can see that's wrong.. hm, could be a path thing.

Posted: Fri Mar 25, 2005 4:24 am
by phpScott
I would check the paths as you suggest as well put some silly echo's in to see where the code is getting to if it is even getting inside your if statement.
At worst nothing changes, slightly better is that you will get an error message stating someting about header already sent.

Best is that you will be able to isolate the problem.

if you are trying to stream the image directly to the screen then read this quite from php.net


The filename argument is optional, and if left off, the raw image stream will be output directly. To skip the filename argument in order to provide a quality argument just use an empty string (''). By sending an image/jpeg content-type using header(), you can create a PHP script that outputs JPEG images directly.
so you have the var $target as the filename which means that the image is being saved to $target and not direcetly displayed.
Other wise you have to use $target to display your image.

Posted: Fri Mar 25, 2005 11:55 am
by Steveo31
so you have the var $target as the filename which means that the image is being saved to $target and not direcetly displayed.
Other wise you have to use $target to display your image.
Right. At the moment I'm just trying to get a system set up where I can throw images into a folder (and eventually, multiple folders) and the page will check if the thumbnail exists. If it doesn't, create the thumbnail. Save time on my part to where I don't have to go into photoshop and resize 100+ images.

Once I can get the thumbs created, I can while through it and display em.