Jump to content

GD transparency problem from imagecopymerge


andrewgarn

Recommended Posts

Right so i'm creating an image with a plain colour hex background in GD, then merging another image (which has a transparent background) on top of it

 

Problem is, the image merged on top appears with a horrible white background, I am doing something wrong..

 

<?php
$time = date("y-m-d H:i:s");

$var = '5B69A6';
$var2 = '000000';
$imColor = hex2int(validHexColor($var));
$imText = hex2int(validHexColor($var2));

$im = imageCreate(320,125);
$font = imageloadfont('chowfun.gdf');

$background = imageColorAllocate($im, $imColor['r'], $imColor['g'], $imColor['b']);
$textcolor = imageColorAllocate($im, $imText['r'], $imText['g'], $imText['b']);

$insert = imagecreatefrompng("signatures/img/strength.png");
imagecolortransparent($insert,imagecolorat($im,0,0));

imagestring($im, 4, 50, 90, $time, $textcolor);

imagecopymerge($im,$insert,200,0,0,0,$insert_x,$insert_y,100);

header('Content-type: image/png');

imagePNG($im);
imageDestroy($im);

/**
 * @param    $hex string        6-digit hexadecimal color
 * @return    array            3 elements 'r', 'g', & 'b' = int color values
 * @desc Converts a 6 digit hexadecimal number into an array of
 *       3 integer values ('r'  => red value, 'g'  => green, 'b'  => blue)
 */
function hex2int($hex) {
		return array( 'r' => hexdec(substr($hex, 0, 2)), // 1st pair of digits
					  'g' => hexdec(substr($hex, 2, 2)), // 2nd pair
					  'b' => hexdec(substr($hex, 4, 2))  // 3rd pair
					);
}

/**
 * @param $input string     6-digit hexadecimal string to be validated
 * @param $default string   default color to be returned if $input isn't valid
 * @return string           the validated 6-digit hexadecimal color
 * @desc returns $input if it is a valid hexadecimal color, 
 *       otherwise returns $default (which defaults to black)
 */
function validHexColor($input = '000000', $default = '000000') {
	// A valid Hexadecimal color is exactly 6 characters long
	// and eigher a digit or letter from a to f
	return (eregi('^[0-9a-f]{6}$', $input)) ? $input : $default ;
}

 

 

 

this code seems to work...with a white border around the image:

 

<?php
$time = date("y-m-d H:i:s");

$var = '5B69A6';
$var2 = '000000';
$imColor = hex2int(validHexColor($var));
$imText = hex2int(validHexColor($var2));

$im = imageCreate(320,125);
$font = imageloadfont('chowfun.gdf');

$background = imageColorAllocate($im, $imColor['r'], $imColor['g'], $imColor['b']);
$textcolor = imageColorAllocate($im, $imText['r'], $imText['g'], $imText['b']);

$insert = imagecreatefromgif("signatures/img/strength2.gif");
imagecolortransparent($insert,imagecolorat($insert,0,0));

$insert_x = imagesx($insert); 
$insert_y = imagesy($insert);

imagestring($im, 4, 50, 90, $time, $textcolor);

imagecopymerge($im,$insert,200,0,0,0,$insert_x,$insert_y,100);

header('Content-type: image/png');

imagePNG($im);
imageDestroy($im);

/**
 * @param    $hex string        6-digit hexadecimal color
 * @return    array            3 elements 'r', 'g', & 'b' = int color values
 * @desc Converts a 6 digit hexadecimal number into an array of
 *       3 integer values ('r'  => red value, 'g'  => green, 'b'  => blue)
 */
function hex2int($hex) {
		return array( 'r' => hexdec(substr($hex, 0, 2)), // 1st pair of digits
					  'g' => hexdec(substr($hex, 2, 2)), // 2nd pair
					  'b' => hexdec(substr($hex, 4, 2))  // 3rd pair
					);
}

/**
 * @param $input string     6-digit hexadecimal string to be validated
 * @param $default string   default color to be returned if $input isn't valid
 * @return string           the validated 6-digit hexadecimal color
 * @desc returns $input if it is a valid hexadecimal color, 
 *       otherwise returns $default (which defaults to black)
 */
function validHexColor($input = '000000', $default = '000000') {
	// A valid Hexadecimal color is exactly 6 characters long
	// and eigher a digit or letter from a to f
	return (eregi('^[0-9a-f]{6}$', $input)) ? $input : $default ;
}

 

http://www.rstracking.com/test.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.