note 68552 deleted from function.imagecopymerge by bjori

From: Date: Wed, 02 Aug 2006 13:08:08 +0000
Subject: note 68552 deleted from function.imagecopymerge by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-115472@lists.php.net to get a copy of this message
Note Submitter: DurkdeNoSpamVries at Gmail dot com ---- 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]

« previous php.notes (#115472) next »