Coder Social home page Coder Social logo

Comments (2)

GoogleCodeExporter avatar GoogleCodeExporter commented on May 19, 2024
<?
/**
* transparentWatermark  class
* Put watermark in image with transparent and randomize effect
*
* Last change: 2004-04-16
*
* author Lionel Micault <lionel 'dot' micault 'at' laposte 'dot' net>
* version 1.01
*
* constructor transparentWatermark( $stampFile="")
* public function markImageFile ( $imageFile, $resultImageFile="")
* public function markImage ( $imageResource)
* public function setStampPosition ( $Xposition, $Yposition)
* public function setStamp( $stampFile)
* public function getLastErrror()
*/


// position constants
define ("transparentWatermarkOnTop", -1);
define ("transparentWatermarkOnMiddle", 0);
define ("transparentWatermarkOnBottom", 1);
define ("transparentWatermarkOnLeft", -1);
define ("transparentWatermarkOnCenter", 0);
define ("transparentWatermarkOnRight", 1);

// randomize level constants
define ("transparentWatermarkRandPixelLightLevel", 7);
define ("transparentWatermarkRandPixelPositionLevel", 2);

class transparentWatermark  {
    var $stampImage=0;
    var $stampWidth;
    var $stampHeight;
    var $stampPositionX= transparentWatermarkOnRight;
    var $stampPositionY= transparentWatermarkOnBottom;

    var $errorMsg="";

    /**
    * Constructor
    *
    * @param string $stampFile  filename of stamp image
    * @return boolean
    * @access public
    * @uses setStamp()
    */
    function transparentWatermark( $stampFile="") {
        return( $this->setStamp( $stampFile));
    }

    /**
    * mark an image file and  display/save it
    *
    * @param int $imageFile  image file (JPEG or PNG format)
    * @param int $resultImageFile new image file (same format)
    * @return boolean
    * @access public
    * @uses readImage()
    * @uses markImage()
    * @uses writeImage()
    * @uses readImage()
    * @uses errorMsg
    */
    function markImageFile ( $imageFile, $resultImageFile="") {
        if (!$this->stampImage) {
            $this->errorMsg="Stamp image is not set.";
            return(false);
        }

        $imageinfos = @getimagesize($imageFile);
        $type   = $imageinfos[2];

        $image=$this->readImage($imageFile, $type);
        if (!$image) {
            $this->errorMsg="Error on loading '$imageFile', image must be a valid PNG
or JPEG file.";
            return(false);
        }

        $this->markImage ( $image);

        if ($resultImageFile!="") {
            $this->writeImage( $image, $resultImageFile, $type);
        }
        else {
            $this->displayImage( $image, $type);
        }
        return( true);

    }

    /**
    * mark an image
    *
    * @param int $imageResource resource of image
    * @return boolean
    * @access public
    * @uses stampWidth
    * @uses stampHeight
    * @uses stampImage
    * @uses stampPositionX
    * @uses stampPositionY
    */
    function markImage ( $imageResource) {
        if (!$this->stampImage) {
            $this->errorMsg="Stamp image is not set.";
            return(false);
        }
        $imageWidth  = imagesx( $imageResource);
        $imageHeight = imagesy( $imageResource);

        //set position of logo
        switch ($this->stampPositionX) {
            case transparentWatermarkOnLeft:
            $leftStamp=0;
            break;
            case transparentWatermarkOnCenter:
            $leftStamp=($imageWidth - $this->stampWidth)/2;
            break;
            case transparentWatermarkOnRight:
            $leftStamp=$imageWidth - $this->stampWidth;
            break;
            default :
            $leftStamp=0;
        }
        switch ($this->stampPositionY) {
            case transparentWatermarkOnTop:
            $topStamp=0;
            break;
            case transparentWatermarkOnMiddle:
            $topStamp=($imageHeight - $this->stampHeight)/2;
            break;
            case transparentWatermarkOnBottom:
            $topStamp=$imageHeight - $this->stampHeight;
            break;
            default:
            $topStamp=0;
        }

        // randomize position
        $leftStamp+=rand(-transparentWatermarkRandPixelPositionLevel,
+transparentWatermarkRandPixelPositionLevel);
        $topStamp+=rand(-transparentWatermarkRandPixelPositionLevel,
+transparentWatermarkRandPixelPositionLevel);

        // for each pixel of stamp
        for ($x=0; $x<$this->stampWidth; $x++) {
            if (($x+$leftStamp<0)||($x+$leftStamp>=$imageWidth)) continue;
            for ($y=0; $y<$this->stampHeight; $y++) {
                if (($y+$topStamp<0)||($y+$topStamp>=$imageHeight)) continue;

                // search RGB values of stamp image pixel
                $indexStamp=ImageColorAt($this->stampImage, $x, $y);
                $rgbStamp=imagecolorsforindex ( $this->stampImage, $indexStamp);


                // search RGB values of image pixel
                $indexImage=ImageColorAt( $imageResource, $x+$leftStamp, $y+$topStamp);
                $rgbImage=imagecolorsforindex ( $imageResource, $indexImage);

                // randomize light shift
                $stampAverage=($rgbStamp["red"]+$rgbStamp["green"]+$rgbStamp["blue"])/3;
                if ($stampAverage>10)
$randomizer=rand(-transparentWatermarkRandPixelLightLevel,
+transparentWatermarkRandPixelLightLevel);
                else $randomizer=0;

                // compute new values of colors pixel
                $r=max( min($rgbImage["red"]+$rgbStamp["red"]+$randomizer-0x80,
0xFF), 0x00);
                $g=max( min($rgbImage["green"]+$rgbStamp["green"]+$randomizer-0x80,
0xFF), 0x00);
                $b=max( min($rgbImage["blue"]+$rgbStamp["blue"]+$randomizer-0x80,
0xFF), 0x00);

                // change  image pixel
                imagesetpixel ( $imageResource, $x+$leftStamp, $y+$topStamp,
($r<<16)+($g<<8)+$b);
            }
        }
    }

    /**
    * set stamp position on image
    *
    * @param int $Xposition x position
    * @param int $Yposition y position
    * @return void
    * @access public
    * $this->stampPositionX
    * $this->stampPositionY
    * @uses errorMsg
    */
    function setStampPosition ( $Xposition, $Yposition) {
        // set X position
        switch ($Xposition) {
            case transparentWatermarkOnLeft:
            case transparentWatermarkOnCenter:
            case transparentWatermarkOnRight:
            $this->stampPositionX=$Xposition;
            break;
        }
        // set Y position
        switch ($Yposition) {
            case transparentWatermarkOnTop:
            case transparentWatermarkOnMiddle:
            case transparentWatermarkOnBottom:
            $this->stampPositionY=$Yposition;
            break;
        }
    }

    /**
    * set stamp image for watermak
    *
    * @param string $stampFile  image file (JPEG or PNG)
    * @return boolean
    * @access public
    * @uses readImage()
    * @uses stampImage
    * @uses stampWidth
    * @uses stampHeight
    * @uses errorMsg
    */
    function setStamp( $stampFile) {
        $imageinfos = @getimagesize($stampFile);
        $width  = $imageinfos[0];
        $height = $imageinfos[1];
        $type   = $imageinfos[2];

        if ($this->stampImage) imagedestroy( $this->stampImage);

        $this->stampImage=$this->readImage($stampFile, $type);

        if (!$this->stampImage) {
            $this->errorMsg="Error on loading '$stampFile', stamp image must be a
valid PNG or JPEG file.";
            return(false);
        }
        else {
            $this->stampWidth=$width;
            $this->stampHeight=$height;
            return(true);
        }
    }




    /**
    * retrieve last error message
    *
    * @return string
    * @access public
    * @uses errorMsg
    */
    function getLastErrror() {
        return($this->errorMsg);
    }

    /**
    * read image from file
    *
    * @param string $file  image file (JPEG or PNG)
    * @param int $type  file type (2:JPEG or 3:PNG)
    * @return resource
    * @access protected
    * @uses errorMsg
    */
    function readImage( $file, $type) {
        switch ($type) {
            case 2:    //JPEG
            return(ImageCreateFromJPEG($file));
            break;

            case 3:    //PNG
            return(ImageCreateFromPNG($file));
            break;

            default:
            $this->errorMsg="File format not supported.";
            return(false);
        }
    }
    /**
    * write image to file
    *
    * @param resource $image  image
    * @param string $file  image file (JPEG or PNG)
    * @param int $type  file type (2:JPEG or 3:PNG)
    * @return void
    * @access protected
    * @uses errorMsg
    */
    function writeImage( $image, $file, $type) {
        switch ($type) {
            case 2:    //JPEG
            Imagejpeg( $image, $file);
            break;

            case 3:    //PNG
            Imagepng( $image, $file);
            break;

            default:
            $this->errorMsg="File format not supported.";
        }
    }
    /**
    * send image to stdout
    *
    * @param resource $image  image
    * @param int $type  image type (2:JPEG or 3:PNG)
    * @return void
    * @access protected
    * @uses errorMsg
    */
    function displayImage( $image, $type) {
        switch ($type) {
            case 2:    //JPEG
            header("Content-Type: image/jpeg");
            Imagejpeg( $image);
            break;

            case 3:    //PNG
            header("Content-Type: image/png");
            Imagepng( $image);
            break;

            default:
            $this->errorMsg="File format not supported.";
        }
    }
}
?> 

Original comment by [email protected] on 13 Oct 2007 at 11:25

from ajax-browser.

GoogleCodeExporter avatar GoogleCodeExporter commented on May 19, 2024

Mon code, cour et efficace

$src = image d'origine (*.gif, *.jpg, *.png)
$dir = dossier d'enregistrement de la nouvelle image
$wmk = image a ajouter en filigrame format *.png

function AddWatermark($src, $dir, $wmk)
{
    $FileDest = $dir.'Watermark@'.md5_file($src).'.png';
    if(($src_size = getimagesize($src))!=false && ($wmk_size = getimagesize($wmk))!=false)
    {
        if ($src_size[0]>$wmk_size[0] && $src_size[1]>$wmk_size[1] &&
function_exists('imagejpeg'))
        {
            if (!is_dir(dirname($FileDest)))
                               mkdir(dirname($FileDest), 0777, true);
            $wmk_img = imagecreatefrompng($wmk);
            imagealphablending($wmk_img,true);
            switch ($src_size[2])
            {
                case 1:
                    $dest_img = imagecreatefromgif($src);
                    break;
                case 2:
                    $dest_img = imagecreatefromjpeg($src);
                    break;
                case 3:
                    $dest_img = imagecreatefrompng($src);
                    break;
            }
            imagealphablending($dest_img,true);
            imagecopy($dest_img, $wmk_img, ($src_size[0]-$wmk_size[0]),
($src_size[1]-$wmk_size[1]), 0, 0, $wmk_size[0], $wmk_size[1]);

            imagepng($dest_img, $FileDest);
            imagedestroy($dest_img);
            imagedestroy($wmk_img);
            if (!is_file($FileDest))
                return $src;;
            else return $FileDest;
        }
        else return $src;
    }
    else return $src;;
}

Original comment by [email protected] on 31 Jan 2008 at 9:26

  • Changed state: Fixed

from ajax-browser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.