Thumbnails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Thumbnails

Post 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);
    }
}
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it doesn't generate thumbnails to the screen, they are written to the server's thumbnail directory, as listed above..
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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.
Post Reply