LinuxSA Mailing list archives

Index: [thread] [date] [subject] [author] [stats]
  From: Andrew Braund <abraund_news@mail.com>
  To  : <lloy0076@rebel.net.au>
<benw@webmedia.com.au> Date: Thu, 14 Jun 2001 12:13:56 +0930

RE: jpeg converter

Snipets from PHP list (when it was working) from the mighty Rasmus around
July 2000 so may not be current.
HTH
Andrew Braund




> So, my first question is what role does ImageMagick play in PHP's
> configuration/installation, since it does look for it? I haven't seen any
> ImageMagick specific functions, nor have I seen any configuration options
> (such as --with-imagemagick or the like).

None, ImageMagick support is broken and deprecated.

<snip>


>      I have a form where I allow a user to upload either a jpg, png, or
gif.
> I'd like to have PHP automatically generate a thumbnail to scale; however,
I
> don't know what function to use.

There is no decent built in function to do this.  I use the command line
conversion tools from the pbmplus package.

My thumbnail function looks like this:


    /* globals */
    $theight=96;
    $djpeg = "/usr/bin/djpeg"; /* decompresses a jpeg to ppm format    */
    $cjpeg = "/usr/bin/cjpeg"; /* compreses a ppm to jpeg format       */
    $pnmscale = "/usr/bin/pnmscale"; /* scales a ppm image          */
    $giftopnm = "/usr/bin/giftopnm"; /* convert a gif to ppm format */
    $ppmtogif = "/usr/bin/ppmtogif"; /* convert a ppm to gif format */
    $ppmquant = "/usr/bin/ppmquant"; /* colour quantize a ppm image */
    $pnmpad = "/usr/bin/pnmpad";
    $shadow = "/usr/local/bin/shadow"; /* Put a drop-shadow on a GIF */

    /*
     * The thumbnail function uses the pnmscale program from
     * the popular Netpbm image manipulation tools package
     * to create a thumbnail of the given image.  If a thumbnail
     * already exists for the image, the function simply returns.
     */
    function thumbnail($filename) {
        global $theight, $djpeg, $cjpeg, $pnmscale, $giftopnm, $ppmtogif,
$ppmquant, $pnmpad, $shadow;

        $tdir = dirname($filename)."/thumbnails"; /* thumbnail directory */
        if(!filetype($tdir)) {
            if(!@mkdir($tdir,0777)) {
                echo "Unable to create $tdir directory - check
permissions<br>\n";
                return;
            }
        }
        $tfile = $tdir."/".basename($filename);   /* thumbnail file */
        $tfile = ereg_replace("\.[^\.]+$",".gif",$tfile);
        if(!filesize($tfile)) {
            if(eregi("\.gif$",$filename)) {  /* Look for .gif extension */
                exec("$giftopnm $filename | $pnmscale -height $theight |
$pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif | $shadow > $tfile");
            } elseif(eregi("\.jpe?g",$filename)) { /* Look for .jpg or .jpeg
extension */
                exec("$djpeg $filename | $pnmscale -height $theight |
$pnmpad -white -l6 -b6 | $ppmquant 248 | $ppmtogif | $shadow > $tfile");
            } else {  /* not a GIF or JPG file */
                return("");
            }
        }
        return($tfile);
    }

/usr/local/bin/shadow is pretty funny actually.  It is a little script I
wrote to stick a drop shadow on a thumbnail.  It was written as a bit of a
joke, but turned out to actually be somewhat useful:

#!/usr/local/bin/php -q
<?
    $arr = array("000000",
                 "000011",
                 "001122",
                 "011334",
                 "124556",
                 "145666");
    $ary = array("045566",
                 "034556",
                 "024556",
                 "011445",
                 "000124",
                 "000001");
    $bot = "666541";

    $im = imagecreatefromgif("/dev/stdin");
    $black = ImageColorAllocate($im, 0,0,0);
    $c0 = ImageColorAllocate($im, 255,255,255);
    $c1 = ImageColorAllocate($im, 220,220,220);
    $c2 = ImageColorAllocate($im, 185,185,185);
    $c3 = ImageColorAllocate($im, 150,150,150);
    $c4 = ImageColorAllocate($im, 135,135,135);
    $c5 = ImageColorAllocate($im, 95,95,95);
    $c6 = ImageColorAllocate($im, 45,45,45);
    $x=0; $y=0;
    $sy = imagesy($im);
    $sx = imagesx($im);
    /* top left */
    while(list(,$v)=each($arr)) {
        $x=0;
        while($x<strlen($v)) {
            ImageSetPixel($im,$x,$y,${"c".$v[$x]});
            $x++;
        }
        $y++;
        $last=$v;
    }
    /* left edge */
    $x=0;
    while($x<strlen($last)) {
        ImageLine($im,$x,$y,$x,$sy-5,${"c".$last[$x]});
        $x++;
    }
    $y=$sy-5;
    /* bottom left */
    while($y<=$sy) {
        $x=0;
        while($x<strlen($ary[$y-$sy+5])) {
            $l = $ary[$y-$sy+5];
            ImageSetPixel($im,$x,$y,${"c".$l[$x]});
            $x++;
        }
        $y++;
    }
    /* bottom edge */
    $y=$sy-6;
    while($y<$sy) {
        ImageLine($im,$x-1,$y,$sx-5,$y,${"c".$bot[$y-$sy+6]});
        $y++;
    }
    /* bottom right */
    $y=$sy-6;
    while($y<$sy) {
        $x=$sx-6;
        while($x-$sx+6 < strlen($ary[$y-$sy+6])) {
            $l = $ary[$y-$sy+6];
            ImageSetPixel($im,$x,$y,${"c".$l[$sx-$x-1]});
            $x++;
        }
        $y++;
    }
    ImageGif($im);
    ImageDestroy($im);
?>

Sorry for the lack of comments in that second one.  But you should be able
to read through it and figure out what it does.  And when you do you will
see why it is so silly.

-Rasmus











I wrote a little command-line shadow tool in PHP that puts a drop shadow
on an image thumbnail.  I'll let the code speak for itself.  It's a pretty
weird way to approach it, I know.  But it only took me about 20 minutes to
write and it solved the immediate problem.


#!/usr/local/bin/php -q
<?
    $arr = array("000000",
                 "000011",
                 "001122",
                 "011334",
                 "124556",
                 "145666");
    $ary = array("045566",
                 "034556",
                 "024556",
                 "011445",
                 "000124",
                 "000001");
    $bot = "666541";

    $im = imagecreatefromgif("/dev/stdin");
    $black = ImageColorAllocate($im, 0,0,0);
    $c0 = ImageColorAllocate($im, 255,255,255);
    $c1 = ImageColorAllocate($im, 220,220,220);
    $c2 = ImageColorAllocate($im, 185,185,185);
    $c3 = ImageColorAllocate($im, 150,150,150);
    $c4 = ImageColorAllocate($im, 135,135,135);
    $c5 = ImageColorAllocate($im, 95,95,95);
    $c6 = ImageColorAllocate($im, 45,45,45);
    $x=0; $y=0;
    $sy = imagesy($im);
    $sx = imagesx($im);
    /* top left */
    while(list(,$v)=each($arr)) {
        $x=0;
        while($x<strlen($v)) {
            ImageSetPixel($im,$x,$y,${"c".$v[$x]});
            $x++;
        }
        $y++;
        $last=$v;
    }
    /* left edge */
    $x=0;
    while($x<strlen($last)) {
        ImageLine($im,$x,$y,$x,$sy-5,${"c".$last[$x]});
        $x++;
    }
    $y=$sy-5;
    /* bottom left */
    while($y<=$sy) {
        $x=0;
        while($x<strlen($ary[$y-$sy+5])) {
            $l = $ary[$y-$sy+5];
            ImageSetPixel($im,$x,$y,${"c".$l[$x]});
            $x++;
        }
        $y++;
    }
    /* bottom edge */
    $y=$sy-6;
    while($y<$sy) {
        ImageLine($im,$x-1,$y,$sx-5,$y,${"c".$bot[$y-$sy+6]});
        $y++;
    }
    /* bottom right */
    $y=$sy-6;
    while($y<$sy) {
        $x=$sx-6;
        while($x-$sx+6 < strlen($ary[$y-$sy+6])) {
            $l = $ary[$y-$sy+6];
            ImageSetPixel($im,$x,$y,${"c".$l[$sx-$x-1]});
            $x++;
        }
        $y++;
    }
    ImageGif($im);
    ImageDestroy($im);
?>


> -----Original Message-----
> From: lloy0076@rebel.net.au [mailto:lloy0076@rebel.net.au]
> Sent: Thursday, 14 June 2001 11:05
> To: Ben Williams
> Cc: LinuxSA
> Subject: Re: jpeg converter
>
>
>
> Ben!
>
> > i need a program that can take a jpg image and make a thumbnail
> out of it
> > (i.e. resize it and produce a new, smaller file) on the command
> line (i need
> > to call it from a PHP script). does anyone know any suitable
> packages off
>
> http://www.imagemagick.org/
>
> Is probably what you want. There's no direct PHP interface but it does
> mention a tool that's part of the whole package called "montage" which,
> it says, does thumbnails...
>
> DSL
> --
> ACCUSATION:
>  "David Newall, you have not read your Microsoft Manual"
> REPLY:
>  "No I haven't" (quoting David Newall with permission)
>

-- 
LinuxSA WWW: http://www.linuxsa.org.au/  IRC: #linuxsa on irc.linux.org.au
To unsubscribe from the LinuxSA list:
  mail linuxsa-request@linuxsa.org.au with "unsubscribe" as the subject


Index: [thread] [date] [subject] [author] [stats]
Return to the LinuxSA Mailing List Information Page