Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to make clean text-based watermark

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have the following code, what it basically does is take the standard method of writing text into an image, however because text methods ( that I know of ) , will not allow you to make a font color somewhat transparent, I am creating a blank image on the fly, filling the background, setting the background color transparent, writing the text onto that image, then doing a merge into the source image with the watermarked layer set to a degree of transparency, everything is fine except for one small nitpick, when looking at the text , the edges have the color of the background partly there, making it look like a very unclean crop out of a transparency, is there any way around this?

Code Below

Code:
			$src_img = imagecreatefromjpeg(---Path To Source Image---);

			$new_w = imagesx($src_img);
			$new_h = imagesy($src_img);

			$wm = @imagecreate($new_w, $new_h)
   				or die("Cannot Initialize new GD image stream");
			$background_color = imagecolorallocate($wm, 255, 255, 255);
			$black = imagecolorallocate($wm, $red, $green, $blue);
			$font_file = "/home/kb244/arial.ttf";
			$font_size = 28;
			$y_start = 200;
			$angle = 0;
			$max_width= 400;
			$line_width = imagettfbbox($font_size,$angle, $font_file, "String Here");
				
			$x_start = ($new_w - ($line_width[2] - $line_width[0])) / 2;
			imagettftext($wm, $font_size, $angle, $x_start, $y_start, $black, $font_file, "String here");
			imagecolortransparent($wm,$background_color) ;
			imagecopymerge( $src_img, $wm, $new_w /50 , $new_h /50 , 0, 0, $new_w, $new_h, $tran);
			
			imagepng($src_img);
			imagedestroy($wm);
			imagedestroy($src_img);

And yes I'm aware of caching the image for later use and so on, I just want to get this working before I move onto that part.

Karl Blessing
PHP/MySQL Developer
 
I found a work-around of sort. Rather than trying to figure out how to make the text layer transparent, I just had to find a way to make it appear transparent, so what I ended up doing in the end was making two copies of the image. Wrote the text to one copy, then used the second copy to merge on top of the altered image at the reverse ammount of transparency I wanted for the text ( so if I wanted 25% transparency on text, I set 75% to the top layer to make the text appear 25% ), here is the final code. With the caching and all.

Code:
<?
Header ("Content-type: image/png");

if ($_REQUEST["gallery"])
	$gallery = $_REQUEST["gallery"];
else
	$gallery = "";

if ($_REQUEST["file"])
	$file = $_REQUEST["file"];
else
	$file = "";

if ($_REQUEST["red"])
	$red = $_REQUEST["red"];
else
	$red = 0;

if ($_REQUEST["blue"])
	$red = $_REQUEST["blue"];
else
	$red = 0;
	
if ($_REQUEST["green"])
	$red = $_REQUEST["green"];
else
	$red = 0;

if ($_REQUEST["tran"])
	$tran = (100 - $_REQUEST["tran"]);
else
	$tran = 0;
	
if ($_REQUEST["clear"])
	$clear = $_REQUEST["clear"];
else
	$clear = 0;
	
if ($_REQUEST["position"])
	$position = $_REQUEST["position"];
else
	$position = "center";

$wm_cache = $_SERVER["DOCUMENT_ROOT"]."/wm/";
if ((file_exists($exif_cache.$_REQUEST["gallery"]."_".$_REQUEST["imgname"].".jpg")) && ($clear == 0))
{
	$dst_img = imagecreatefromjpeg($wm_cache.$_REQUEST["gallery"]."_".$_REQUEST["imgname"].".jpg");
	imagejpeg($dst_img);
	imagedestroy($dst_img);
}
else
{
			$src_img = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT']."/gallery/".$gallery."/".$file.".jpg");
			$cpy_img = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT']."/gallery/".$gallery."/".$file.".jpg");

			$new_w = imagesx($src_img);
			$new_h = imagesy($src_img);

			

			$black = imagecolorallocate($src_img, $red, $green, $blue);
			$font_file = "/home/kb244/arial.ttf";
			$font_size = 28;
			$angle = 0;
			$line_width = imagettfbbox($font_size,$angle, $font_file, "Karl Blessing Photography");
			$textheight = $line_width[3] - $line_width[5];
			$textwidth = $line_width[2] - $line_width[0];

			if (strcmp($position, "top") == 0)
				$y_start = $textheight;
			elseif (strcmp($position, "bottom") == 0)
				$y_start = $new_h - ($textheight / 2);
			else
				$y_start = ($new_h / 2) - ($textheight / 2);
			
			$x_start = ($new_w - ($line_width[2] - $line_width[0])) / 2;
			imagettftext($src_img, $font_size, $angle, $x_start, $y_start, $black, $font_file, "Karl Blessing Photography");
			imagecopymerge( $src_img, $cpy_img, 0 , 0 , 0, 0, $new_w, $new_h, $tran);
			
			imagejpeg($src_img,$wm_cache.$gallery."_".$file.".jpg", 80); 
			imagejpeg($src_img,"",80);
			imagedestroy($cpy_img);
			imagedestroy($src_img);
}

I changed the file type to jpeg to allow for some sort of compression, couldnt see much since in a truecolor image to have 400KB images from under 100KB jpegs.

To add a tiny bit of a touch I threw this into my .htaccess as part of a mod_rewrite to make the url a lil simpler.

Code:
RewriteRule ^w/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/ /wmtest.php?gallery=$1&file=$2&red=$3&green=$4&blue=$5&tran=$6&position=$7&clear=$8 [L]

(I'm not a regex expert so I just used what works)

Karl Blessing
PHP/MySQL Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top