Opacity in color fill,

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Opacity in color fill,

Post by Pezmc »

I am using this script to produce thumbnails for logos.

Code: Select all

function createthumb($name,$filename,$new_w,$new_h)
{
	$system=explode(".",$name);
	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=($old_y*($new_h/$old_x));
	}
	if ($old_x < $old_y) 
	{
		$thumb_w=($old_x*($new_w/$old_y));
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w+2,$thumb_h+2);
	$red = imagecolorallocate($dst_img, 0, 0, 0);
	imagefill($dst_img, 0, 0, $red);
	imagecopyresampled($dst_img,$src_img,1,1,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
	
	if (preg_match("/png/",$system[1]))
	{
		imagepng($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename); 
	}
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}
This produces a image like the one at this address: http://games.pezmc.com/logos/Hobo.jpg

However I now need a way to label these images with peoples names, I have been asked to produce them so they would appear like the image at this address: http://games.pezmc.com/logos/Hobosss.jpg.

I know how to do the text but am unsure on how to create a white 50% opacity background for the output. After some research I found the only way to set opacity is with imagecopymerge. Yet I cannot see how I would apply this to my script.

All people that help receive a free website link on my thanks page.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

All people that help receive a free website link on my thanks page.
That won't be necessary.

imagecreatetruecolor() combined with imagecolorallocatealpha(255, 255, 255, 64) should get you started.
Pezmc
Forum Commoner
Posts: 53
Joined: Mon Nov 06, 2006 2:15 pm

Post by Pezmc »

Thanks, for anyone else trying to do what I am doing, use this

Code: Select all

$opacity = 40; // 0 = transparent, 100 = opaque
    $font = 5;  // 1 thru 5
    $textcolor = array('red' => 0, 'green' => 0, 'blue' => 64); // 0 thru 255
    $transparancyColor = array('red' => 255, 'green' => 255, 'blue' => 255); // 0 thru 255
    $quality = 100; // 0 = worst, 100 = best
    
    // now lets add the watermark
    $transparancyColor = imagecolorallocatealpha($dst_img, $transparancyColor['red'], $transparancyColor['green'], $transparancyColor['blue'], round(127 - ($opacity * 1.27)));
	
    imagefilledrectangle($dst_img, 1, 1, 50, 50, $transparancyColor);
Post Reply