note 102597 modified in function.imagesavealpha by danbrown

From: Date: Wed, 23 Feb 2011 19:03:53 +0000
Subject: note 102597 modified in function.imagesavealpha by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177264@lists.php.net to get a copy of this message
After much trial and error and gnashing of teeth I finally figured out how to composite a png with an 8-bit alpha onto a jpg. This was not obvious to me so I thought I'd share. Hope it helps. I'm using this to create a framed thumbnail image: <?php // load the frame image (png with 8-bit transparency) $frame = imagecreatefrompng('path/to/frame.png'); // load the thumbnail image $thumb = imagecreatefromjpeg('path/to/thumbnail.jpg'); // get the dimensions of the frame, which we'll also be using for the // composited final image. $width = imagesx( $frame ); $height = imagesy( $frame ); // create the destination/output image. $img=imagecreatetruecolor( $width, $height ); // enable alpha blending on the destination image. imagealphablending($img, true); // Allocate a transparent color and fill the new image with it. // Without this the image will have a black background instead of being transparent. $transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); imagefill( $img, 0, 0, $transparent ); // copy the thumbnail into the output image. imagecopyresampled($img,$thumb,32,30,0,0, 130, 100, imagesx( $thumb ), imagesy( $thumb ) ); // copy the frame into the output image (layered on top of the thumbnail) imagecopyresampled($img,$frame,0,0,0,0, $width,$height,$width,$height); imagealphablending($img, false); // save the alpha imagesavealpha($img,true); // emit the image header('Content-type: image/png'); imagepng( $img ); // dispose imagedestroy($img); // done. exit; ?> --was-- After much trial and error and gnashing of teeth I finally figured out how to composite a png with an 8-bit alpha onto a jpg. This was not obvious to me so I thought I'd share. Hope it helps. I'm using this to create a framed thumbnail image: <? // load the frame image (png with 8-bit transparency) $frame = imagecreatefrompng('path/to/frame.png'); // load the thumbnail image $thumb = imagecreatefromjpeg('path/to/thumbnail.jpg'); // get the dimensions of the frame, which we'll also be using for the // composited final image. $width = imagesx( $frame ); $height = imagesy( $frame ); // create the destination/output image. $img=imagecreatetruecolor( $width, $height ); // enable alpha blending on the destination image. imagealphablending($img, true); // Allocate a transparent color and fill the new image with it. // Without this the image will have a black background instead of being transparent. $transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); imagefill( $img, 0, 0, $transparent ); // copy the thumbnail into the output image. imagecopyresampled($img,$thumb,32,30,0,0, 130, 100, imagesx( $thumb ), imagesy( $thumb ) ); // copy the frame into the output image (layered on top of the thumbnail) imagecopyresampled($img,$frame,0,0,0,0, $width,$height,$width,$height); imagealphablending($img, false); // save the alpha imagesavealpha($img,true); // emit the image header('Content-type: image/png'); imagepng( $img ); // dispose imagedestroy($img); // done. exit; ?> http://php.net/manual/en/function.imagesavealpha.php

« previous php.notes (#177264) next »