Help generating a transparent png image using php

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
So I am trying to use a php code to make a png image that has a transparent background with a sentence written on it. So far I have a transparent png image of the size I want and the code below. The only problem I have is that the background color comes out as black and not transparent. I am sure I have overlooked something really simple. If anyone can help, I would appreciate it. Thank you.

PHP:
<?php

header("Content-type: image/png");
$string = "Working";
$im     = imagecreatefrompng("http://i271.photobucket.com/albums/jj149/violetvalor/075400.png");
$txtcolor = imagecolorallocate($im, 102, 51, 153);
imagestring($im, 3, 3, 3, $string, $txtcolor);
imagepng($im);
imagedestroy($im);

?>
Address: http://purpleflame.exofire.net/phptests/textimagetest.php
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
I may be wrong, but I don't think you can create transparent images using the gd library. I've never been able to when I try. Sorry!

--Neil
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
As you can see from the image in my sig, I have successfully done this.
EDIT: Well, ok then. It is hard to tell against a plain white background... but it is!


Here's a quick method that you could use:
PHP:
<?php
header("Content-type: image/png");
$image = imagecreatetruecolor(500, 150)
   or die("Cannot Initialize new GD image stream");
$col_transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
$col_red   = imagecolorallocate($image, 200, 0, 0);

imagefill($image, 0, 0, $col_transparent);  // set the transparent colour as the background.
imagecolortransparent ($image, $col_transparent); // actually make it transparent

imagestring($image, 5, 10, 5,  "Working", $col_red);
imagepng($image);
imagedestroy($image);
?>
The whole trick here is to allocate a colour as being 'transparent' at the start, then setting it as the background colour and allocating it as actually being transparent. Of course, you don't necessary have to set it as the background. You could choose to only use it in certain places. Just draw it like any other colour.
 
Last edited:

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
As you can see from the image in my sig, I have successfully done this.
EDIT: Well, ok then. It is hard to tell against a plain white background... but it is!


Here's a quick method that you could use:
PHP:
<?php
header("Content-type: image/png");
$image = imagecreatetruecolor(500, 150)
   or die("Cannot Initialize new GD image stream");
$col_transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
$col_red   = imagecolorallocate($image, 200, 0, 0);

imagefill($image, 0, 0, $col_transparent);  // set the transparent colour as the background.
imagecolortransparent ($image, $col_transparent); // actually make it transparent

imagestring($image, 5, 10, 5,  "Working", $col_red);
imagepng($image);
imagedestroy($image);
?>
The whole trick here is to allocate a colour as being 'transparent' at the start, then setting it as the background colour and allocating it as actually being transparent. Of course, you don't necessary have to set it as the background. You could choose to only use it in certain places. Just draw it like any other colour.

I'm gunna vouch for this without trying it actually - the thing in the lower-right of my sig's using pretty much the same method to get the background transparent.


One note though: in imagecolorallocatealpha($image, 255, 255, 255, 127);

As long as the last number (alpha) is 127 (it's on a scale from 0 (opaque) to 127 (fully transparent)), the first one's don't matter. Mine's using all 0's so I don't get confused which one's alpha :)
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
you can do it as scoochi said. i use transparent png's resized w/ gd library for parts of my site.

for the most part, those look like the fundamentals for making it work.
 

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
I must have messed up somehow since I tried the following code, however, the browser only showed me the message:

The image "http://purpleflame.exofire.net/phptests/textimagetest.php" cannot be displayed, because it contains errors.
PHP:
<?php

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

$text = "Working";

$image  = imagecreatetruecolor(400,75);
$bg_color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$fg_color = imagecolorallocate($image, 102, 51, 153);
//$font   = "arial.ttf";
//imagettftext($image, 12, 0, 3, 3, $fg_color, $font, $text);

imagefill($image, 0, 0, $bg_color);
imagecolortrasparent($image, $bg_color);

imagestring($image, 4, 3, 3, $text, $fg_color);

imagepng($image);
imagedestroy($image);

?>

I also tried including that line that begins with "or die", but it only gave an error message saying it cannot parse the logical operator "or". Thanks in advance.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
I think it may only be a typo...

imagecolortransparent($image, $bg_color);
 

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
(* '.' *) Thanks for catching that. That fixed it.

So I found this other function to place text called imagettftext. However, its makes the same problem. I liked this function since it lets me use a normal font like arial. However, it seems that FreeType library needs to be available to use it. Is that causing the problem?

More importantly, is there some way to wrap the text in case the string is too long? Thanks in advance.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
So I found this other function to place text called imagettftext. However, its makes the same problem. I liked this function since it lets me use a normal font like arial. However, it seems that FreeType library needs to be available to use it. Is that causing the problem?
Not really, you just need the .TTF file for the font that you wish to use on the server.
PHP:
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';

// Print the text, using the predefined font file
imagettftext($image, 20, 0, 10, 20, $col_red, $font, 'Working');
?>
As for the wrapped text, a quick Google search yielded the following. I've not tested it, but it looks ok. You could change it slightly to use imagettftext instead of imagestring.

Also, I would do some major editing on it so that it doesn't give the image, it just adds the text... Maybe I'll do that tomorrow :)
PHP:
<?php
function make_wrapped_txt($txt, $color = 000000, $space = 4, $font = 4, $w = 300)
{
    if (strlen($color) != 6)
        $color = 000000;
    $int = hexdec($color);
    $h = imagefontheight($font);
    $fw = imagefontwidth($font);
    $txt = explode("\n", wordwrap($txt, ($w / $fw), "\n"));
    $lines = count($txt);
    $im = imagecreate($w, (($h * $lines) + ($lines * $space)));
    $bg = imagecolorallocate($im, 255, 255, 255);
    $color = imagecolorallocate($im, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8),
        0xFF & $int);
    $y = 0;
    foreach ($txt as $text)
    {
        $x = (($w - ($fw * strlen($text))) / 2);
        imagestring($im, $font, $x, $y, $text, $color);
        $y += ($h + $space);
    }
    header('Content-type: image/jpeg');
    die(imagejpeg($im));
}
?>
 
Last edited:

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
Alright. So I tried to incorporate that as best as I could. However, I still get the error that it contains errors so it cannot be displayed. Thanks again.

PHP:
<?php

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

$TEXT = "123456789012345678901234567890123456789012345678901234567890";

$width = 300;
$font = 3;
$space = 3;

$fh = imagefontheight($font);
$fw = imagefontwidth($font);
$txt = explode("\n", wordwrap($TEXT, ($width / $fw), "\n"));
$lines = count($txt);

$image = imagetruecolor($width, (($fh * $lines) + ($lines * $space)));
$bg_color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$fg_color = imagecolorallocate($image, 102, 51, 153);

imagefill($image, 0, 0, $bg_color);
imagecolortransparent($image, $bg_color);

$y = 0;
foreach ($txt as $text)
{
    $x = (($w - ($fw * strlen($text))) / 2);
    imagestring($image, $font, $x, $y, $text, $fg_color);
    $y += ($fh + $space);
}

imagepng($image);
imagedestroy($image);

?>
 
F

Fahad

Guest
Freetype is enabled on x10 servers.
We need to know every error that is output, not just the Cannot Display Image.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
Alright. So I tried to incorporate that as best as I could. However, I still get the error that it contains errors so it cannot be displayed. Thanks again.
try commenting the line that has
PHP:
header("Content-type: image/png");

//--- to ---

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

that'll make it so you can see what errors it's trying to tell you, making you more help for us to help you ;)
 
Last edited:

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
Fahad, I gave all the info I knew how to get at the time. Sorry I did not know how to get more. According to xPlozion's instructions I get the additional info:

Fatal error: Call to undefined function imagetruecolor() in /home/pf/public_html/phptests/textimagetest.php on line 20
I investigate and find that there is no function called imagetruecolor, but instead there is one called imagecreatetruecolor!


Thanks everyone for all the help! I really appreciate it!
Seems like my friend who referred me was right about all the helpful forum users. :) ;)
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
np, assuming that you fixed the problem, you can close this thread.
 

purpleflame

New Member
Messages
17
Reaction score
0
Points
0
So getting the text done is working just fine except the font does not seem to be read as well I would hope. Well the imagestring font works well until I can find a good font to use.

I have another issue now. I have this picture: http://i271.photobucket.com/albums/jj149/violetvalor/SigPic02.png . This is the picture I want to make the php script output. I used the code below, but it did not fade to transparency properly.

PHP:
<?php

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

$filename = "http://i271.photobucket.com/albums/jj149/violetvalor/SigPic02.png";

$image = imagecreatefrompng($filename);

imagepng($image);
imagedestroy($image);

?>;

So then I tried the below. The result of which you can see here: http://purpleflame.exofire.net/age/sigpic.php . As you can see, this does not produce the picture properly either.

PHP:
<?php

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

$filename = "http://i271.photobucket.com/albums/jj149/violetvalor/SigPic02.png";

$image = imagecreatefrompng($filename);
$trans_color = imagecolorallocatealpha($image, 0, 0, 0, 127);

imagefill($image, 0, 0, $trans_color);
imagecolortransparent($image, $trans_color);

imagepng($image);
imagedestroy($image);

?>

Sorry to be a bother.
 
Last edited:
Top