Page 1 of 1

Improved Image Thumbnail class from phpgd.com (transparency)

Posted: Sat Oct 13, 2007 10:03 am
by markg85
Hey,

I was just looking for a nice thumbnailing script to use in my file database script (MageDB) and found the thumbnailing script on phpgd.com.
The issue i had with it was that it wasn't supporting true png alpha transparency so i added that and will share the improved script here of cause.

Changed:
- True alpha transparency in thumbnails (png)
- Maked use of already defined constants in the switch. (don't know if it's faster or not.. i just used it)
- Changed the identing and spacing to my likings

Code: Select all

<?php



    $i = new imagethumbnail();

    $i->open("aqua.png");

    $i->setX(150);



    header("Content-type: image/png;");

    $i->imagepng();



    class imagethumbnail

    {



        var $filename;

        var $x;

        var $y;

        var $image;

        var $thumbnail;



        function imagethumbnail()

        {



        }



        function open($filename)

        {



            $this->filename = $filename;

            $imageinfo = array();

            $imageinfo = getimagesize($this->filename, $imageinfo);



            $this->old_x = $imageinfo[0];

            $this->old_y = $imageinfo[1];



            switch ($imageinfo[2]) {

                case IMAGETYPE_GIF:

                    $this->image = imagecreatefromgif($this->filename);

                break;



                case IMAGETYPE_JPEG:

                    $this->image = imagecreatefromjpeg($this->filename);

                break;



                case IMAGETYPE_PNG:

                    $this->image = imagecreatefrompng($this->filename);

                break;

            }



        }



        function setX($x = "")

        {

            if (isset($x))

            {

                $this->x = $x;

            }

            return $this->x;

        }



        function setY($y = "")

        {

            if (isset($y))

            {

                $this->y = $y;

            }

            return $this->y;

        }



        function generate()

        {



            if ($this->x > 0 and $this->y > 0)

            {

                $new_x = $this->x;

                $new_y = $this->y;

            }

            elseif ($this->x > 0 and $this->x != "")

            {

                $new_x = $this->x;

                $new_y = ($this->x / $this->old_x) * $this->old_y;

            }

            else

            {

                $new_x = ($this->y / $this->old_y) * $this->old_x;

                $new_y = $this->y;

            }



            $this->thumbnail = imagecreatetruecolor($new_x, $new_y);

            imageantialias($this->thumbnail, true);

            imagealphablending($this->thumbnail, false);

            imagesavealpha($this->thumbnail, true);

            imagecopyresampled($this->thumbnail, $this->image, 0, 0, 0, 0, $new_x, $new_y, $this->old_x, $this->old_y);

        }



        function imagegif($filename = "")

        {

            if (!isset($this->thumbnail))

            {

                $this->generate();

            }

            imagetruecolortopalette($this->thumbnail, 0, 256);

            if ($filename == "")

            {

                imagegif($this->thumbnail);

            }

            else

            {

                imagegif($this->thumbnail, $filename);

            }

        }



        function imagejpeg($filename = "", $quality = 80)

        {

            if (!isset($this->thumbnail))

            {

                $this->generate();

            }

            imagejpeg($this->thumbnail, $filename, $quality);

        }



        function imagepng($filename = "")

        {

            if (!isset($this->thumbnail))

            {

                $this->generate();

            }

            if ($filename == "")

            {

                imagepng($this->thumbnail);

            }

            else

            {

                imagepng($this->thumbnail, $filename);

            }

        }



    }



?>
The only thing i see that can still be improved now is the math stuff for scaling down a image.
And perhaps adding "@" signs in front of the php functions is better as well to prevent error messages from popping up (for security reasons).

Good luck.
Mark.

Posted: Sat Oct 13, 2007 10:13 am
by spamyboy
Thanks.