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
https://forums.phpfreaks.com/topic/4016-php-and-thumbnails/#findComment-14009
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
https://forums.phpfreaks.com/topic/4016-php-and-thumbnails/#findComment-14039
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
https://forums.phpfreaks.com/topic/4016-php-and-thumbnails/#findComment-14059
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.