Jump to content

PHP and thumbnails


peterbarone

Recommended Posts

Without seeing your code it's hard to help you...

Here's some code that I use to get thumbnail pictures (final size of thumbs ~120 x 160 px):
[code]<?php
    function makethumb($fn,$rt=true,$dbg=false) // $fn is the file containing the image
                                                                            // $rt -- true: return the thumbnail size, etc
                                                                            //          false: display the thumbnail
                                                                            // $dbg -- debug on/off
    {
        if ($dbg) global $fp;
        $ok = false;
        if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Getting thumbnail for ' . $fn . ' $rt: ' . var_export($rt,true) . "\r\n");
        list($ow, $oh, $type, $attr) = getimagesize($fn);
        if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Original width: ' . $ow . ' original height: ' . $oh . "\r\n");
        $pct = 75;
        while (!$ok) {
            $th = $oh * ($pct/100);
            $tw = $ow * ($pct/100);
            if (($oh > $ow) && ($th <= 160 && $w <= 120)) $ok = true;
            if (($ow >= $oh) && ($tw <= 160 && $th <= 120)) $ok = true;
            if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Percent: ' . $pct . ' Ok: ' . var_export($ok,true) . ' Computed width: ' . $tw . ' computed height: ' . $th . "\r\n");
            $pct -= 1;
            if ($pct < 5) {
                $ok = true;
                $th = 120;
                $tw = 160;
            }
        }
        $w = round($tw);
        $h = round($th);
        $tn = imagecreatetruecolor($w, $h);
        $img = imagecreatefromjpeg($fn);
        imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh);
        if ($rt) {
            if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Returning type: ' . $type . ' width: ' . $w . ' height: ' . $h . "\r\n");
            return (array($tn, $type, $w, $h));
        } else {
            if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Writing mime type: ' . image_type_to_mime_type($type) . "\r\n");
           header('Content-type: ' .image_type_to_mime_type($type));
            imagejpeg($tn,'tmp.tmp');
            readfile('tmp.tmp');
        }
    }
?>[/code]

Of course if the images come from a fairly recent digital camera, most of those images have thumbnails embedded in the EXIF data and you can use the [a href=\"http://www.php.net/exif_thumbnail\" target=\"_blank\"]exif_thumbnail[/a]() function to extract them.

Ken
Link to comment
Share on other sites

[!--quoteo(post=351438:date=Mar 3 2006, 04:52 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 3 2006, 04:52 PM) [snapback]351438[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Without seeing your code it's hard to help you...

Here's some code that I use to get thumbnail pictures (final size of thumbs ~120 x 160 px):
[code]<?php
    function makethumb($fn,$rt=true,$dbg=false) // $fn is the file containing the image
                                                                            // $rt -- true: return the thumbnail size, etc
                                                                            //          false: display the thumbnail
                                                                            // $dbg -- debug on/off
    {
        if ($dbg) global $fp;
        $ok = false;
        if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Getting thumbnail for ' . $fn . ' $rt: ' . var_export($rt,true) . "\r\n");
        list($ow, $oh, $type, $attr) = getimagesize($fn);
        if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Original width: ' . $ow . ' original height: ' . $oh . "\r\n");
        $pct = 75;
        while (!$ok) {
            $th = $oh * ($pct/100);
            $tw = $ow * ($pct/100);
            if (($oh > $ow) && ($th <= 160 && $w <= 120)) $ok = true;
            if (($ow >= $oh) && ($tw <= 160 && $th <= 120)) $ok = true;
            if ($dbg) fwrite($fp,date('m/d/Y G:i a') . ' Percent: ' . $pct . ' Ok: ' . var_export($ok,true) . ' Computed width: ' . $tw . ' computed height: ' . $th . "\r\n");
            $pct -= 1;
            if ($pct < 5) {
                $ok = true;
                $th = 120;
                $tw = 160;
            }
        }
        $w = round($tw);
        $h = round($th);
        $tn = imagecreatetruecolor($w, $h);
        $img = imagecreatefromjpeg($fn);
        imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh);
        if ($rt) {
            if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Returning type: ' . $type . ' width: ' . $w . ' height: ' . $h . "\r\n");
            return (array($tn, $type, $w, $h));
        } else {
            if ($dbg) fwrite($fp, date('m/d/Y G:i a') . ' Writing mime type: ' . image_type_to_mime_type($type) . "\r\n");
           header('Content-type: ' .image_type_to_mime_type($type));
            imagejpeg($tn,'tmp.tmp');
            readfile('tmp.tmp');
        }
    }
?>[/code]

Of course if the images come from a fairly recent digital camera, most of those images have thumbnails embedded in the EXIF data and you can use the [a href=\"http://www.php.net/exif_thumbnail\" target=\"_blank\"]exif_thumbnail[/a]() function to extract them.

Ken
[/quote]


wow that is aot of code for a thumbnail !!!


But thanks and yes all pics are from a good quality digital camra. So I guess that I'll do some reasearch on
exif_thumbnail() that sounds like what I could be looking for
Link to comment
Share on other sites

I may have extra code because I may not know the original size of the picture and I always wanted the thumbnails to be approximately the same size, so I reduce the width and height by a decreasing percentage. You can see it in action at [a href=\"http://www.ny-njdogtrainer.com/gallery.php\" target=\"_blank\"]http://www.ny-njdogtrainer.com/gallery.php[/a] and [a href=\"http://www.rutgerspromenaders.org/pictures/\" target=\"_blank\"]http://www.rutgerspromenaders.org/pictures/[/a] (click on the menus to the left -- not all have pictures yet.)

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.