Jump to content

GD transparent image question


tcloud

Recommended Posts

I have generated a transparent PNG image and want to place rotated text on it.

 

To that end, I am using a modified version of a function to generate a watermark -- found on the PHP manual page for ImageTTFText()

http://php.net/manual/en/function.imagettftext.php

... it is about 2/3 way down entitled "ben at evolutioncomputing co uk

05-Nov-2006 09:25; A centralised text watermark - of  any length, that automatically sizes to about 70% of the width, and can be rotated to any angle"

 

The attached images show the image before and after using the "watermark" code.

 

Here is the code I believe is relevant to my question (how to keep the transparency in the original image):

 

This code creates my original image and I use ImageArc() and ImageLine() on it to get the first image shown, and it has transparency for the white background.

 

$img = ImageCreateTrueColor($width, $height) ;
ImageSaveAlpha($img, true) ;
// set transparent color
$trans_color = ImageColorAllocateAlpha($img, 0, 0, 0, 127) ;
ImageFill($img, 0, 0, $trans_color) ;

 

I modified the watermark function so I could pass it parameters.  It is named RotateTTF().  The function works great -- it just loses the transparency of the original image.

 

The commented lines in the RotateTTF() function code show some of the things I've tried, including numerous implementations of:

ImageAlphaBlending() ; ... trying both TRUE and FALSE

ImageSaveAlpha() ;

$trans_color = ImageColorAllocateAlpha($OriginalImage, 0, 0, 0, 127) ;

ImageFill($OriginalImage, 0, 0, $trans_color) ;

 

function RotateTTF($Image, $width, $height, $FontSize, $FontColor, $Font, $ShadowColor, $ShadowOffset, $Text, $Rotation) {
//
// Make a copy image
$OriginalImage = ImageCreateTrueColor($width, $height) ;
ImageAlphaBlending($OriginalImage, true) ;
//
//ImageSaveAlpha($OriginalImage, true) ;
// set transparent color
//$trans_color = ImageColorAllocateAlpha($OriginalImage, 0, 0, 0, 127) ;
//ImageFill($OriginalImage, 0, 0, $trans_color) ;
//
//
ImageCopy ($OriginalImage, $Image, 0, 0, 0, 0, $width, $height) ;
//
// Iterate to get the size up
if ($FontSize == 0) {
	$FontSize = 1 ;
	do
	{
		$FontSize *= 1.1 ;
		$Box = @ImageTTFBBox($FontSize, 0, $Font, $Text) ;
		$TextWidth = abs($Box[4] - $Box[0]) ;
		$TextHeight = abs($Box[5] - $Box[1]) ;
	}
	while ($TextWidth < $width*0.7) ;
}
//
//  position text
$x = $width/2 - cos(deg2rad($Rotation)) * $TextWidth/2 ;
$y = $height/2 + sin(deg2rad($Rotation)) * $TextWidth/2 + cos(deg2rad($Rotation)) * $TextHeight/2 ;
// Make shadow text first followed by solid text
ImageTTFText($Image, $FontSize, $Rotation, $x + $ShadowOffset, $y + $ShadowOffset, $ShadowColor, $Font, $Text) ;
ImageTTFText($Image, $FontSize, $Rotation, $x, $y, $FontColor, $Font, $Text) ;
// merge original image into version with text to show image through text
ImageCopyMerge ($Image, $OriginalImage, 0, 0, 0, 0, $width, $height, 50) ;
//
//ImageAlphaBlending($Image, false) ;
//ImageSaveAlpha($Image, true) ;
ImageDestroy($OriginalImage) ;
}

 

Any help preserving the image transparency would be most appreciated -- or perhaps a different approach to placing rotated text on the image.

 

thanks,

Tom

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/201878-gd-transparent-image-question/
Share on other sites

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.