List Info

Thread: note 78379 added to function.imagecolortransparent




note 78379 added to function.imagecolortransparent
user name
2007-10-09 09:55:22
This block of code should preserve transparency when
resizing an image using the GD library.

<?php
/**
 * Try to preserve transparency info
 * $orig_type - Original Image Type
 * $orig_img  - GD Image Resource for original image
 * $new_img   - GD Image Resource for resized image
 */

// Transparency only available for GIFs & PNGs
if ( ($orig_type == IMAGETYPE_GIF) || ($orig_type ==
IMAGETYPE_PNG) ) {
	$trnprt_indx = imagecolortransparent($orig_img);

	// If we have a specific transparent color
	if ($trnprt_indx >= 0) {

		// Get the original image's transparent color's RGB
values
		$trnprt_color	= imagecolorsforindex($orig_img,
$trnprt_indx);

		// Allocate the same color in the new image resource
		$trnprt_indx	= imagecolorallocate($new_img,
$trnprt_color['red'], $trnprt_color['green'],
$trnprt_color['blue']);

		// Completely fill the background of the new image with
allocated color.
		imagefill($new_img, 0, 0, $trnprt_indx);

		// Set the background color for new image to transparent
		imagecolortransparent($new_img, $trnprt_indx);

	// Always make a transparent background color for PNGs that
don't have one allocated already
	} elseif ($orig_type == IMAGETYPE_PNG) {

		// Turn off transparency blending (temporarily)
		imagealphablending($new_img, false);

		// Create a new transparent color for image
		$color = imagecolorallocatealpha($new_img, 0, 0, 0, 127);

		// Completely fill the background of the new image with
allocated color.
		imagefill($this->new_img, 0, 0, $color);

		// Restore transparency blending
		imagesavealpha($this->new_img, true);
	}
}
?>
----
Server IP: 216.92.131.65
Probable Submitter: 74.92.61.189
----
Manual Page -- http://www.php.net/manual/en/function.imagecolort
ransparent.php
Edit        -- https://master
.php.net/note/edit/78379
Del: integrated  -- h
ttps://master.php.net/note/delete/78379/integrated
Del: useless     -- http
s://master.php.net/note/delete/78379/useless
Del: bad code    -- htt
ps://master.php.net/note/delete/78379/bad+code
Del: spam        -- https:/
/master.php.net/note/delete/78379/spam
Del: non-english -- 
https://master.php.net/note/delete/78379/non-english
Del: in docs     -- http
s://master.php.net/note/delete/78379/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78379
Reject      -- https://mast
er.php.net/note/reject/78379
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )