a Basic GD wrapper class (mostly for 1 image handling)

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

a Basic GD wrapper class (mostly for 1 image handling)

Post by Flamie »

Yeah I know a lot can be added to it, but I did it for personal purposes and those were the only things I needed for it =)
Feel free to use/modify it as you wish =]

Code: Select all

<?
class Image
{
    var $ext;
    var $img;
    function Image($url)
    {
        $this->ext = substr($url, -3);
        $this->LoadImage($url);
    }
    function LoadImage($url)
    {
        switch($this->ext)
        {
            case "png":
            case "PNG":
                $this->img = imagecreatefrompng($url);
                break;
            case "jpg":
            case "JPG":
                $this->img = imagecreatefromjpeg($url);
                break;
            case "gif":
            case "GIF":
                    $this->img = imagecreatefromgif($url);
                    break;
        }
    }
    function Output($dest = '')
    {
        header("Content-type: image");
        if($dest == '')
        {
            switch($this->ext)
            {
                case "png":
                case "PNG":
                    imagepng($this->img);
                    break;
                case "jpg":
                case "JPG":
                    imagejpeg($this->img);
                    break;
                case "gif":
                case "GIF":
                    imagegif($this->img);
                    break;
            }
        }
        else 
        {
            switch($this->ext)
            {
                case "png":
                case "PNG":
                    imagepng($this->img, $dest);
                    break;
                case "jpg":
                case "JPG":
                    imagejpeg($this->img, $dest);
                    break;
                case "gif":
                case "GIF":
                    imagegif($this->img, $dest);
                    break;
            }
        }
    }
    function Destroy()
    {
        imagedestroy($this->img);
    }
    function Transparent($r, $g, $b)
    {
        $color = imagecolorallocate($this->img, $r, $g, $b);
        imagecolortransparent($this->img, $color);
    }
    function Resize($percent)
    {
        $img = $this->img;
        $imw = imagesx($this->img);
        $imh = imagesy($this->img);
        $dw = round($imw*($percent/100));
        $dh = round($imh*($percent/100));
        $this->img = imagecreatetruecolor($dw, $dh);
        imagecopyresampled($this->img, $img, 0, 0, 0, 0, $dw, $dh, $imw, $imh);
        imagedestroy($img);
        
    }
    function Colorize($r, $g, $b, $factor)
    {
        $temp = imagecreatetruecolor(imagesx($this->img), imagesy($this->img));
        $color = imagecolorallocate($r,$g,$b);
        imagefill($temp, 0, 0, $color);
        imagecopymerge($this->img, $temp, 0, 0, 0, 0, imagesx($temp), imagesy($temp), $factor);
        imagedestroy($temp);
    }
}
?>
Post Reply