imagecreatetruecolor() transparency problem

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
Civil Engineer
Forum Newbie
Posts: 4
Joined: Mon Sep 06, 2010 7:28 am

imagecreatetruecolor() transparency problem

Post by Civil Engineer »

Hi all,

I've been doing some digging for the past 4 hrs and have yet to find a work around for this. I'm trying to add text over a .png image (name and level). Using imagecreatetruecolor() creates a black background which is normal. The problem is that I'm still getting black edges around my images after making the black bg transparent. Is there a way I can avoid using imagecreatetruecolor() altogether and simply put the text over the existing .png image? I really appreciate any help you can give me!!!

My code is shown below..

Code: Select all

if($aid == $id){

// The adoptable exists, so let's try and show the image

$usingimage = "no";

$image = getcurrentimage($id);

	// Let's see if the server has support for GD or not
	// Also to use fancy images the image must be a gif and fancy images must be enabled...

	$usegd = grabanysetting("gdimages");
	$imageinfo = @getimagesize($image);
	$imagemime = $imageinfo["mime"]; // Mime type of the image file, should be a .gif file...

	if(function_exists('imagepng') and $usegd == "yes" and $imagemime == "image/png")
	{

	$usingimage = "yes"; //Turn the template system off

	// BEGIN NEW CODE

	list($width, $height, $type, $attr) = getimagesize($image); // The size of the original adoptable image

	// Begin the fancy outputs...

	// Lets create the new target image, with a size big enough for the text for the adoptable

	$newheight = $height + 20;

	if($newwidth < 120){
	$newwidth = 120;
	}
	else{
	$newwidth = $width;
	}

      $img_temp = imagecreatetruecolor($newwidth, $newheight); 


      $alphablending = true;  


    	// Lets create the image and save its transparency  
      $img_old = @imagecreatefrompng($image);  
      imagealphablending($img_old, true);  
      imagesavealpha($img_old, true);
   
     // Lets copy the old image into the new image with  
     // the given size  
     ImageCopyResampled(  
         $img_temp,  
         $img_old,  
         0, 0, 0, 0,  
         $width,  
         $height,  
         $width,  
         $height  
     );  
   
	
	$textheight = 115;

	$image = $img_temp;

	$bgi = imagecreatetruecolor($newwidth, $newheight);

	$color = imagecolorallocate($bgi, 51, 51, 51);
        

        $str1 = "".$name;
	$str2 = "lvl: ".$totalclicks;
        $text_width = imagefontwidth(2)*strlen($str1);
        $x = 60 - (ceil($text_width/2));
        $text_width = imagefontwidth(2)*strlen($str2);
        $x2 = 60 - (ceil($text_width/2));

	
	imagestring ($image, 2, $x, $textheight,  $str1, $color);
	imagestring ($image, 2, $x2, $textheight + 13,  $str2, $color);
	

	$background = imagecolorallocate($image, 0, 0, 0);  
      ImageColorTransparent($image, $background);  
 
	header("Content-Type: image/PNG");
	ImagePng ($image);
	imagedestroy($image);
	imagedestroy($img_temp);
	imagedestroy($img_old);
	imagedestroy($bgi);

	}
	else{
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: imagecreatetruecolor() transparency problem

Post by josh »

Civil Engineer wrote:Is there a way I can avoid using imagecreatetruecolor() altogether and simply put the text over the existing .png image
No.

Any way you can remove the error suppression operators and try again, and report back with any differences in results, like any errors you are getting? I can't speak for other programmers but if you won't take the time to ask your compiler first, I don't feel like you can ask the community in all good conscious. It might be something obvious and you/we would be tearing our hair out over it. First step in this situation is to never use error suppression, otherwise expect to have some hair pulling sessions.

Once you confirm you aren't suppressing any important errors, I will be more inclined to help
Civil Engineer
Forum Newbie
Posts: 4
Joined: Mon Sep 06, 2010 7:28 am

Re: imagecreatetruecolor() transparency problem

Post by Civil Engineer »

To be honest I know pretty much nothing about php.. this code was written by someone else and I've been editing it bits and pieces at a time to get to show text over my image using a .png file. The original code supported text over a .gif but as you gifs aren't as clear and leave a pixelated white border around my images.

How would I go about this part?
remove the error suppression operators and try again, and report back with any differences in results, like any errors you are getting?
Civil Engineer
Forum Newbie
Posts: 4
Joined: Mon Sep 06, 2010 7:28 am

Re: imagecreatetruecolor() transparency problem

Post by Civil Engineer »

Also I have an error log and it's not reporting any errors right now..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: imagecreatetruecolor() transparency problem

Post by josh »

Google for 'php error suppression operator' ;-) It may be not reporting them because you suppressed them. It does that.
Civil Engineer
Forum Newbie
Posts: 4
Joined: Mon Sep 06, 2010 7:28 am

Re: imagecreatetruecolor() transparency problem

Post by Civil Engineer »

Well with no knowledge of php what so ever I somehow managed a work around to get this to work..

After some extensive work I've manged an adoptable’s name and level to its image.
This should make tracking its growth much easier, and prevent any confusion between adoptables of the same type.

You can see an example of this in my Siggymon ‘Max’ below.

Image

You can see the new code below..

Code: Select all

if($aid == $id){

// The adoptable exists, so let's try and show the image

$usingimage = "no";

$image = getcurrentimage($id);

	// Let's see if the server has support for GD or not
	// Also to use fancy images the image must be a gif and fancy images must be enabled...

	$usegd = grabanysetting("gdimages");
	$imageinfo = @getimagesize($image);
	$imagemime = $imageinfo["mime"]; // Mime type of the image file, should be a .gif file...

	if(function_exists('imagepng') and $usegd == "yes" and $imagemime == "image/png")
	{

	$usingimage = "yes"; //Turn the template system off

	// BEGIN NEW CODE

	list($width, $height, $type, $attr) = getimagesize($image); // The size of the original adoptable image

	// Begin the fancy outputs...

	// Lets create the new target image, with a size big enough for the text for the adoptable

	$newheight = $height + 20;

	if($newwidth < 120){
	$newwidth = 120;
	}
	else{
	$newwidth = $width;
	}

      $img_temp = imagecreatetruecolor($newwidth, $newheight); 




    	// Lets create the image and save its transparency  
      $img_old = @imagecreatefrompng($image);  
      imagealphablending($img_old, false);  
      imagesavealpha($img_old, true);
   
     // Lets copy the old image into the new image with  
     // the given size  
     ImageCopyResampled(  
         $img_temp,  
         $img_old,  
         0, 0, 0, 0,  
         $width,  
         $height,  
         $width,  
         $height  
     );  
   
	
	$textheight = 100;

	$image = $img_old;

	$bgi = imagecreatetruecolor($newwidth, $newheight);

	$color = imagecolorallocate($bgi, 51, 51, 51);
        

        $str1 = "".$name;
	$str2 = "lvl: ".$totalclicks;
        $text_width = imagefontwidth(2)*strlen($str1);
        $x = 50 - (ceil($text_width/2));
        $text_width = imagefontwidth(2)*strlen($str2);
        $x2 = 50 - (ceil($text_width/2));

	
	imagestring ($image, 2, $x, $textheight,  $str1, $color);
	imagestring ($image, 2, $x2, $textheight + 13,  $str2, $color);
	

	$background = imagecolorallocate($image, 0, 0, 0);  
      ImageColorTransparent($image, $background);  
 
	header("Content-Type: image/PNG");
	ImagePng ($image);
	imagedestroy($image);
	imagedestroy($img_temp);
	imagedestroy($img_old);
	imagedestroy($bgi);

	}
	else{
Post Reply