note 69123 added to function.imagecopyresized

From: Date: Thu, 24 Aug 2006 00:06:11 +0000
Subject: note 69123 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-116242@lists.php.net to get a copy of this message
Belows is the code snipet that allows you to resize a transparent PNG and composite it into another image. The code is tested to work with PHP5.1.2, GD2, but I think it can also work with other versions of PHP and GD. The code has been commented to help you read through it. The idea of resizing the transparent PNG image is to create a new destination image which is completely transparent then turn off the imageAlphaBlending of this new image so that when the PNG source file is copied, its alpha channel is still retained. <?php /** * Compose a PNG file over a src file. * If new width/ height are defined, then resize the PNG (and keep all the transparency info) * Author: Alex Le - http://www.alexle.net */ function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false) { if( $ovr_w && $ovr_h ) $ovr = imageResizeAlpha( $ovr, $ovr_w, $ovr_h ); /* Noew compose the 2 images */ imagecopy($src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr) ); } /** * Resize a PNG file with transparency to given dimensions * and still retain the alpha channel information * Author: Alex Le - http://www.alexle.net */ function imageResizeAlpha(&$src, $w, $h) { /* create a new image with the new width and height */ $temp = imagecreatetruecolor($w, $h); /* making the new image transparent */ $background = imagecolorallocate($temp, 0, 0, 0); ImageColorTransparent($temp, $background); // make the new temp image all transparent imagealphablending($temp, false); // turn off the alpha blending to keep the alpha channel /* Resize the PNG file */ /* use imagecopyresized to gain some performance but loose some quality */ imagecopyresized($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src)); /* use imagecopyresampled if you concern more about the quality */ //imagecopyresampled($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src)); return $temp; } ?> Example usage: <?php header('Content-type: image/png'); /* Open the photo and the overlay image */ $photoImage = ImageCreateFromJPEG('images/MiuMiu.jpg'); $overlay = ImageCreateFromPNG('images/hair-trans.png'); $percent = 0.8; $newW = ceil(imagesx($overlay) * $percent); $newH = ceil(imagesy($overlay) * $percent); /* Compose the overlay photo over the target image */ imageComposeAlpha( $photoImage, $overlay, 86, 15, $newW, $newH ); /* Open another PNG file, then resize and compose it */ $watermark = imagecreatefrompng('images/watermark.png'); imageComposeAlpha( $photoImage, $watermark, 10, 20, imagesx($watermark)/2, imagesy($watermark)/2 ); /** * Open the same PNG file then compose without resizing * As the original $watermark is passed by reference, it was resized already. * So we have to reopen it. */ $watermark = imagecreatefrompng('images/watermark.png'); imageComposeAlpha( $photoImage, $watermark, 80, 350); Imagepng($photoImage); // output to browser ImageDestroy($photoImage); ImageDestroy($overlay); ImageDestroy($watermark); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 71.201.61.142 ---- 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.imagecopyresized.php Edit -- https://master.php.net/note/edit/69123 Del: integrated -- https://master.php.net/note/delete/69123/integrated Del: useless -- https://master.php.net/note/delete/69123/useless Del: bad code -- https://master.php.net/note/delete/69123/bad+code Del: spam -- https://master.php.net/note/delete/69123/spam Del: non-english -- https://master.php.net/note/delete/69123/non-english Del: in docs -- https://master.php.net/note/delete/69123/in+docs Del: other reasons-- https://master.php.net/note/delete/69123 Reject -- https://master.php.net/note/reject/69123 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116242) next »