FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Create,Resize,Thumb, Shadow of your Images

 


dafadagod
class.shadow.php
Code:

<?php
class thumb
{
    var $gdver=2;
    var $errWidthImg=103;
    var $errHeightImg=73;
    var $saveDir;
    var $saveImg;
    var $saveOverwrite;
    var $sourceDir;
    var $imgDir;
    var $errReturn=false;
    var $cPercent=100;
    var $fontSize=2;
    var $bgRGBColor=array(255,255,225);
    var $msgRGBColor=array(255,8,71);
    var $shadowDir;
    var $dropshadow=false;
    var $shadowImg=array();
    var $newx, $new_x, $newy, $new_y;

    /*
    ** $msg: the message you would like to show in error image
    ** $offset: the space of left to right (higher to right and lower to left | 0 all left)
    */
    function setErr($msg,$offset)
    {
        // if $this->saveImg set true, the return must be true if not the error image will show and you will not get a thumb.
        if($this->saveImg) $this->errReturn=true;
        else
        {
            $bgRGBColor=$this->bgRGBColor;
            $msgRGBColor=$this->msgRGBColor;

            $im = @imagecreate ($this->errWidthImg, $this->errHeightImg);
            $bgColor = imagecolorallocate ($im, $bgRGBColor[0], $bgRGBColor[1], $bgRGBColor[2]);
            $msgColor = imagecolorallocate ($im, $msgRGBColor[0], $msgRGBColor[1], $msgRGBColor[2]);
            imagefilledrectangle ($im, 0, 0, 90, 70, $bgColor);
            imagestring ($im, $this->fontSize, $offset, 28, $msg, $msgColor);
            header ("Content-type: image/jpeg");
            imagejpeg ($im,'',$this->cPercent);
            imagedestroy ($im);
            return false;
        }
    }

    /*
    ** $this->gdver: the version of GD lib (1 | 2)
    ** $src: the path to your image source
    ** $desiredw: the width you would like to resize from image source
    ** $desiredh: the height you would like to resize from image source
    */
    function resized($src,$desiredw,$desiredh,$shadow_color="FFFFFF")
    {
        if(!$desiredw||!$desiredh)
            $this->setErr("WIDTH|HIGHT ERR",6);

        $shadow_color=$this->toRGB($shadow_color);
        $shadow_color=explode(",",$shadow_color);

        $gdarr=array(1,2);
        for($i=0;$i<count($gdarr);$i++)
        {
            if($this->gdver!=$gdarr[$i])
                $test.="|";
        }
        $exp=explode("|",$test);

        (count($exp)==3)?$this->setErr("GD VER NOT FOUND",5):"";

        $size=@getimagesize($this->sourceDir.$src);

        if (!$size)
        {
            $this->setErr("Image Error",20);
            if($this->errReturn) return false;
        }
        else
        {
            (is_string($this->saveImg))?$this->setErr("VAR ERR",30):"";

            ((!$this->saveOverwrite)&&!isset($this->saveDir)&&$this->saveImg)?$this->setErr("VAR ERR",30):"";

            if ($size[0]>=$size[1]*((float)($desiredw/$desiredh)))
            {
                $this->newy = round(($size[1] * (float)($desiredw/$size[0])),0);
                $this->newx = $desiredw;
            }
            else
            {
                $this->newy = $desiredh;
                $this->newx = round(($size[0] * (float)($desiredh/$size[1])),0);
            }

            $this->shadowImg=array(
            'left'=>@imagecreatefrompng($this->shadowDir . "sim_left.png"),
            'right'=>@imagecreatefrompng($this->shadowDir . "sim_right.png"),
            'top'=>@imagecreatefrompng($this->shadowDir . "sim_top.png"),
            'bottom'=>@imagecreatefrompng($this->shadowDir . "sim_bottom.png"),
            'tlcorner'=>@imagecreatefrompng($this->shadowDir . "sim_tlcorner.png"),
            'trcorner'=>@imagecreatefrompng($this->shadowDir . "sim_trcorner.png"),
            'blcorner'=>@imagecreatefrompng($this->shadowDir . "sim_blcorner.png"),
            'brcorner'=>@imagecreatefrompng($this->shadowDir . "sim_brcorner.png"),

            );

            $this->new_x = @imagesx($this->shadowImg['left']) + @imagesx($this->shadowImg['right']) + $this->newx;
            $this->new_y = @imagesy($this->shadowImg['top']) + @imagesy($this->shadowImg['bottom']) + $this->newy;

            ($this->gdver==1)?$destimg = @imagecreate((!$this->dropshadow)?$this->newx:$this->new_x, (!$this->dropshadow)?$this->newy:$this->new_y) or $this->setErr("GD ERR",30):$destimg = @imagecreatetruecolor((!$this->dropshadow)?$this->newx:$this->new_x, (!$this->dropshadow)?$this->newy:$this->new_y ) or $this->setErr("GD2 ERR",30);

            @imagefill($destimg,0,0,imagecolorallocate($destimg,$shadow_color[0],$shadow_color[1],$shadow_color[2]));

            if($this->errReturn) return false;

            if ($size[2]==1)
            {
                $sourceimg=imagecreatefromgif($this->sourceDir.$src);

                $this->dropCreate($destimg,$sourceimg,$size[0],$size[1]);

                if(!$this->saveImg)header("content-type: image/gif");

                ($this->saveImg)?imagegif($destimg,(($this->saveOverwrite)?$this->sourceDir.$src:$this->saveDir.$src),$this->cPercent):imagegif($destimg,'',$this->cPercent);

                return true;
            }
            elseif ($size[2]==2)
            {
                $sourceimg=imagecreatefromjpeg($this->sourceDir.$src);

                $this->dropCreate($destimg,$sourceimg,$size[0],$size[1]);

                if(!$this->saveImg)header("content-type: image/jpeg");

                ($this->saveImg)?imagejpeg($destimg,(($this->saveOverwrite)?$this->sourceDir.$src:$this->saveDir.$src),$this->cPercent):imagejpeg($destimg,'',$this->cPercent);

                return true;
            }
            elseif ($size[2]==3)
            {
                $sourceimg=imagecreatefrompng($this->sourceDir.$src);

                $this->dropCreate($destimg,$sourceimg,$size[0],$size[1]);

                if(!$this->saveImg)header("content-type: image/png");

                ($this->saveImg)?imagepng($destimg,(($this->saveOverwrite)?$this->sourceDir.$src:$this->saveDir.$src),$this->cPercent):imagepng($destimg,'',$this->cPercent);


                return true;
            }
            else
                $this->setErr("TYPE ERR",30);

        }

        imagedestroy($destimg);
        imagedestroy($sourceimg);
    }

    function dropCreate($destimg,$sourceimg,$w,$h)
    {
        if(!$this->dropshadow)
        {
            @imagecopyresampled($destimg, $sourceimg, 0,0,0,0, $this->newx, $this->newy, $w, $h);
            return true;
        }

        $funcResize=($this->gdver==2)?"imagecopyresampled":"imagecopyresized";

        @$funcResize($destimg, $this->shadowImg['tlcorner'], 0, 0, 0, 0, @imagesx($this->shadowImg['tlcorner']), @imagesy($this->shadowImg['tlcorner']), @imagesx($this->shadowImg['tlcorner']), @imagesy($this->shadowImg['tlcorner']));

        @$funcResize($destimg, $this->shadowImg['top'], @imagesx($this->shadowImg['left']), 0, 0, 0, $this->newx, @imagesy($this->shadowImg['top']), @imagesx($this->shadowImg['top']), @imagesy($this->shadowImg['top']));

        @$funcResize($destimg, $this->shadowImg['trcorner'], ($this->new_x - @imagesx($this->shadowImg['right'])), 0,
        0, 0, @imagesx($this->shadowImg['trcorner']), @imagesy($this->shadowImg['trcorner']), @imagesx($this->shadowImg['trcorner']), @imagesy($this->shadowImg['trcorner']));

        @$funcResize($destimg, $this->shadowImg['left'], 0, @imagesy($this->shadowImg['top']), 0, 0,
        @imagesx($this->shadowImg['left']), $this->newy, @imagesx($this->shadowImg['left']), @imagesy($this->shadowImg['left']));

        @$funcResize($destimg, $this->shadowImg['right'], ($this->new_x - @imagesx($this->shadowImg['right'])), @imagesy($this->shadowImg['tlcorner']), 0, 0, @imagesx($this->shadowImg['right']), $this->newy, @imagesx($this->shadowImg['right']), @imagesy($this->shadowImg['right']));

        @$funcResize($destimg, $this->shadowImg['blcorner'], 0, ($this->new_y - @imagesy($this->shadowImg['bottom'])),
        0, 0, @imagesx($this->shadowImg['blcorner']), @imagesy($this->shadowImg['blcorner']), @imagesx($this->shadowImg['blcorner']), @imagesy($this->shadowImg['blcorner']));

        @$funcResize($destimg, $this->shadowImg['bottom'], @imagesx($this->shadowImg['left']), ($this->new_y - @imagesy($this->shadowImg['bottom'])), 0, 0, $this->newx, @imagesy($this->shadowImg['bottom']), @imagesx($this->shadowImg['bottom']), @imagesy($this->shadowImg['bottom']));

        @$funcResize($destimg, $this->shadowImg['brcorner'], ($this->new_x - @imagesx($this->shadowImg['right'])), ($this->new_y - @imagesy($this->shadowImg['bottom'])), 0, 0, @imagesx($this->shadowImg['brcorner']), @imagesy($this->shadowImg['brcorner']), @imagesx($this->shadowImg['brcorner']), @imagesy($this->shadowImg['brcorner']));

        @$funcResize($destimg, $sourceimg, @imagesx($this->shadowImg['left']), @imagesy($this->shadowImg['top']),
        0, 0, $this->newx, $this->newy, $w, $h);

    }

    function toRGB($hex)
    {
        $_1stHex=$hex[0].$hex[1];
        $_2ndHex=$hex[2].$hex[3];
        $_3rdHex=$hex[4].$hex[5];
        $R=strtoupper(hexdec($_1stHex));
        $G=strtoupper(hexdec($_2ndHex));
        $B=strtoupper(hexdec($_3rdHex));
        return $R.",".$G.",".$B;
    }

}

?>

here is some example how to use it:
example.php
Code:

<?php

//--------------------------------------------Thumb class (Global)
include_once "class.shadow.php";
$thumb=new thumb;

$thumb->gdver=2;

// Shadow conf
$thumb->shadowDir="Images/Shadow/";
$thumb->dropshadow=true;
$thumb->sourceDir="Images/Uploaded/";
// true for save thumb image | false just resize image
$thumb->saveImg=true;
$thumb->resized($_GET[src],$_GET[w],$_GET[h],$_GET[hex]);
/*
Save from file

$thumb->dropshadow=true;

// the source directory of your image which you would like to resize
$thumb->sourceDir="/path/source/";
// true | false // true for save direct from source (overwrite) | false for save to the save directory
$thumb->saveOverwrite=false;

$thumb->saveDir="/path/save/";
// true for save thumb image | false just resize image
$thumb->saveImg=true;

// The file will get from $thumb->sourceDir
$thumb->resized('filename.jpg','width','height');
*/

/*
Thumb directly

<img src="thumb.php?src=filename.jpg&w=111&h=83&hex=FFFFFF">

in thumb.php file

include_once "class.thumb.php";
$thumb=new thumb;
// GD version
$thumb->gdver=2;

// Shadow conf
$thumb->shadowDir="/path/shadow/";
$thumb->dropshadow=true;

$thumb->sourceDir="/path/source/";

// true for save thumb image | false just resize image
$thumb->saveImg=false;

$thumb->resized($_GET[src],$_GET[width],$_GET[height],$_GET[hex]);

*/
?>



use any browser like IE or opera and use this link:
http://www.yourdomain.com/Path/to/Shadow/Example.php?src=filename.jpg&w=100&h=100&hex=FFFFFF

Save these images to folder name "images/shadow/":

rename this image to : sim_blcorner.png

rename this image to : sim_bottom.png

rename this image to : sim_brcorner.png

rename this image to : sim_left.png

rename this image to :sim_right.png

rename this image to :sim_tlcorner.png

rename this image to :sim_top.png

rename this image to : sim_trcorner.png

Save this file to folder images/uploaded/ and rename it to 1.jpg



now u can use any web browser to see it by this address:
http://www.domain.com/example.php?src=1.jpg&w=100&h=100&hex=FFFFFF

hope u enjoy this script
Nae_splash
nice tutorial! I like it!
it will definitley help me..Smile
~Nae
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.