note 68552 added to function.imagecopymerge

From: Date: Tue, 01 Aug 2006 16:52:29 +0000
Subject: note 68552 added to function.imagecopymerge
Groups: php.notes 
Request: Send a blank email to php-notes+get-115422@lists.php.net to get a copy of this message
I was looking for some code to put a watermark on my images, but somehow, these functions here don't support alpha blending. This is what I wanted: To add a .png file at a specific location to a .jpg file while taking alpha blending into account. I don't need fancy scripts that don't fit on my monitor screen with numerous functions like centering the watermark, resizing it or making it 50% transparent. If I would like to make the watermark more transparent or resize it, I would use photoshop. So after getting a bit frustrated, I wrote my own function, I hope it is of some use to you. Note: If there are nasty breaks in the code, you should copy it to notepad or something.. NoteII: I used [ and ] in the html tags.. [html] [head][title]Watermarker[/title][/head] [body style="margin:100"] <?php //use an url like: index.php? Image=Image.jpg &Watermark=Watermark.png &Xoffset=250 &Yoffset=250 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //load image identifier.. $Image = imagecreatefromjpeg($_GET["Image"]); //load watermark identifier.. $WaterMark = imagecreatefrompng($_GET["Watermark"]); //set destination x and y.. $Xoffset = $_GET["Xoffset"]; $Yoffset = $_GET["Yoffset"]; //call function.. $Image = watermark($Image, $WaterMark, $Xoffset, $Yoffset); //save the original.. imagejpeg($Image, $_GET["Image"], 80); //clear memory.. imagedestroy ($Image); imagedestroy ($WaterMark); //show photo.. echo "[img src='" . $_GET["Image"] . "']\n"; // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //function definition.. function watermark($DstIm, $SrcIm, $DstX, $DstY) { //get size of the water mark.. $SrcImWidth = imagesx($SrcIm); $SrcImHeight = imagesy($SrcIm); //create blending space.. $DstImPart = imagecreatetruecolor($SrcImWidth, $SrcImHeight); //copy the part of the image that will be covered by the water mark to blending space.. imagecopy($DstImPart, $DstIm, 0, 0, $DstX, $DstY, $SrcImWidth, $SrcImHeight); //combine the water mark and the part of the original image pixel by pixel.. for($y = 0; $y < $SrcImHeight; $y++) { for($x = 0; $x < $SrcImWidth; $x++) { //get the colors per pixel of both images.. $SrcImColor = imagecolorsforindex($SrcIm, imagecolorat($SrcIm, $x, $y)); $DstImPartColor = imagecolorsforindex($DstImPart, imagecolorat($DstImPart, $x, $y)); //define alpha value.. $Alpha = $SrcImColor['alpha']/127; //combine the colors of both images.. $DstImPartRed = round(($Alpha * $DstImPartColor['red']) + ((1 - $Alpha) * $SrcImColor['red'])); $DstImPartGreen = round(($Alpha * $DstImPartColor['green']) + ((1 - $Alpha) * $SrcImColor['green'])); $DstImPartBlue = round(($Alpha * $DstImPartColor['blue']) + ((1 - $Alpha) * $SrcImColor['blue'])); //set the pixel color of the destination image.. $DstImPartColor = imagecolorallocate($DstImPart, $DstImPartRed, $DstImPartGreen, $DstImPartBlue); imagesetpixel($DstImPart, $x, $y, $DstImPartColor); } } //copy the part of the image that has changed back to the original image.. imagecopy($DstIm, $DstImPart, $DstX, $DstY, 0, 0, $SrcImWidth, $SrcImHeight); //clear memory.. imagedestroy ($DstImPart); //return the result.. return $DstIm; } // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ?> [/body] [/html] ---- Server IP: 66.163.161.117 Probable Submitter: 62.51.42.18 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php Edit -- https://master.php.net/note/edit/68552 Del: integrated -- https://master.php.net/note/delete/68552/integrated Del: useless -- https://master.php.net/note/delete/68552/useless Del: bad code -- https://master.php.net/note/delete/68552/bad+code Del: spam -- https://master.php.net/note/delete/68552/spam Del: non-english -- https://master.php.net/note/delete/68552/non-english Del: in docs -- https://master.php.net/note/delete/68552/in+docs Del: other reasons-- https://master.php.net/note/delete/68552 Reject -- https://master.php.net/note/reject/68552 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115422) next »