Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Adding text to images

I am trying to write text to images with the script below.
Only works when I use  $black = ImageColorAllocate($im, 0,0,0);
and then the text is shown in a very light gold color.
ttf font files are in the directory and I am able to select fonts OK
What do I need to change to get colors in text?

<?php
Header("Content-Type: image/png");
$im=imageCreateFromPng("Cruise.png");

$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255, 255, 255);
$red =ImageColorAllocate($im,255,0,0);
$green=ImageColorAllocate($im,0,255,0);
$blue=ImageColorAllocate($im,0,0,255);

$font="VeraIt.ttf";
$font2="VeraBd.ttf";

imageTTFtext($im,50,0,20,100,$black,$font2,"Test for black");
imageTTFtext($im,50,0,20,200,$white,$font,"Test for white");
imageTTFtext($im,50,0,20,300,$red,$font,"Letters in red");
imagettftext($im,50,0,20,400,$green,$font2,"The story of green");
imagettftext($im,50,0,20,500,$blue,$font,"Blue Blue Blue");

imagepng($im);

?>
0
aijohn
Asked:
aijohn
  • 7
  • 5
  • 5
2 Solutions
 
ClickCentricCommented:
My guess is that it's something to do with the Cruise.png image that you're using as the base.  I remember having similar issues when I first played around with it.  Try creating a new image ( imagecreatetruecolor() ) with the same dimensions, then do your imagecolorallocate() calls on that and then copy $im into the new image ( imagecopy() ) and then doing your imagettftext() calls.  This should work around the issue.

Check out www.php.net for information on the respective functions.
0
 
aijohnAuthor Commented:
Think this is what you were suggesting.
Same results.
Comments?

<?php
Header("Content-Type: image/png");
$im2=imageCreateFromJpeg("picture.jpg");
$size = getimagesize("picture.jpg");
$width=$size[0];
$height=$size[1];
$im = imagecreatetruecolor( $width,$height );


$white = ImageColorAllocate($im, 255, 255, 255);
$black = ImageColorAllocate($im, 0,0,0);

$red =ImageColorAllocate($im,255,0,0);
$green=ImageColorAllocate($im,0,255,0);
$blue=ImageColorAllocate($im,0,0,255);
$font="VeraIt.ttf";
$font2="VeraBd.ttf";


imagecopy ( $im, $im2, 0,0,0,0,$width,$height);

imageTTFtext($im,20,0,20,100,$black,$font2,"Test for black");
imageTTFtext($im,20,0,20,200,$white,$font,"Test for white");
imageTTFtext($im,20,0,20,300,$red,$font,"Letters in red");
imagettftext($im,20,0,20,400,$green,$font2,"The story of green");
imagettftext($im,20,0,20,500,$blue,$font,"Blue Blue Blue");
imagepng($im);

?>
0
 
keteracelCommented:
can GD find the fonts?

quick test: copy the required fonts into the same directory as the script. This will ensure that the GD can find the fonts.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
keteracelCommented:
oops, ignore me, didn't read the question fully :S
0
 
ClickCentricCommented:
That is strange.  I duplicated your original code sample and it works fine for me.  What version of GD do you have installed?  And I'm assuming no errors were generated that would indicate that you don't have freetype installed?
0
 
keteracelCommented:
might be worth doing a <?php phpinfo(); ?> to see if you have freetype as ClickCentric suggests!
0
 
aijohnAuthor Commented:
From phpinfo


gd
GD Support  enabled  
GD Version  bundled (2.0.15 compatible)  
FreeType Support  enabled  
FreeType Linkage  with TTF library  
GIF Read Support  enabled  
JPG Support  enabled  
PNG Support  enabled  
WBMP Support  enabled  
XBM Support  enabled  
0
 
ClickCentricCommented:
I have version 2.0.28.  The problem may be in that.  It does work for me, though.  I can provide a link to a sample for you if you want.
0
 
aijohnAuthor Commented:
Link would be greatly and gratefully appreciated.



I've tried this on two servers where I have domains and also on my own linux server with the same miserable results.
0
 
ClickCentricCommented:
http://www.devios.com/eetestpng.html

Note that I just pulled an image out of nowhere, but as you can see, it does work.  The specific code I used for eetestpng.php was:

<?php
Header("Content-Type: image/png");
$im=imageCreateFromJpeg("images/dealcentric-logo.jpg");
$size = getimagesize("images/dealcentric-logo.jpg");
$width=$size[0];
$height=$size[1];
//$im = imagecreatetruecolor( $width,$height );


$white = ImageColorAllocate($im, 255, 255, 255);
$black = ImageColorAllocate($im, 0,0,0);

$red =ImageColorAllocate($im,255,0,0);
$green=ImageColorAllocate($im,0,255,0);
$blue=ImageColorAllocate($im,0,0,255);
$font="arial.ttf";
$font2="verdana.ttf";


//imagecopy ( $im, $im2, 0,0,0,0,$width,$height);

imagettftext($im,10,0,20,10,$black,$font2,"Test for black");
imagettftext($im,10,0,20,20,$white,$font,"Test for white");
imagettftext($im,10,0,20,30,$red,$font,"Letters in red");
imagettftext($im,10,0,20,40,$green,$font2,"The story of green");
imagettftext($im,10,0,20,50,$blue,$font,"Blue Blue Blue");

imagepng($im);

?>

I was testing out the modified version when I decided to retrofit it to the original as the modified version worked for me.  The only difference is, though it shouldn't matter, I used a jpg, but you tried that in your second attempt too, so that shouldn't be it.
0
 
keteracelCommented:
I also have 2.0.28 and it works (when I use fonts I have!).

I used a png though as the source image so it's not a png/jpeg thing!

http://www.keteracel.com/test/test671.php

src:

<?php
$im1=imageCreateFromPng("../keteracelMainPic.png");
$size = getimagesize("../keteracelMainPic.png");
$width=$size[0];
$height=$size[1];
$im = imagecreatetruecolor( $width,$height );


$white = ImageColorAllocate($im, 255, 255, 255);
$black = ImageColorAllocate($im, 0,0,0);

$red =ImageColorAllocate($im,255,0,0);
$green=ImageColorAllocate($im,0,255,0);
$blue=ImageColorAllocate($im,0,0,255);
$font=$font2="framd.ttf";


imagecopy ( $im, $im1, 0,0,0,0,$width,$height);

imagettftext($im,10,0,20,10,$black,$font2,"Test for black");
imagettftext($im,10,0,20,20,$white,$font,"Test for white");
imagettftext($im,10,0,20,30,$red,$font,"Letters in red");
imagettftext($im,10,0,20,40,$green,$font2,"The story of green");
imagettftext($im,10,0,20,50,$blue,$font,"Blue Blue Blue");

Header("Content-Type: image/png");
imagepng($im);

?>

What exactly happens? If you view the source of the returned page, are there any warning messages?
0
 
aijohnAuthor Commented:
No warning messages.

Created an all white png.

All tests show the black and blue lines printing  a light yellow and nothing for red, green, and white.
0
 
keteracelCommented:
can you link us your test page? And give us the full source?
0
 
aijohnAuthor Commented:
Decided to put scripts up on another server to show the problem.

No problem, all code worked.

This server has 2.0.28

Two servers where I was trying to use the code.

#1   2.0.15
#2   2.0.23

Thanks for all helpful posts. Guess I will use images from Server #3 on #1 and #2 or
get my friendly provider to upgrade 1 and 2 to 2.0.26
0
 
aijohnAuthor Commented:
Sorry, typo, not 2.0.26  but truly, truly 2.0.28
0
 
aijohnAuthor Commented:
One last question.

How do i write the image to /home/site/images/newpic.png
0
 
ClickCentricCommented:
bool imagepng ( resource image [, string filename] )

Just put the filename: imagepng($im,"images/newpic.png");
This is assuming that /home/site/ is where the script is running from, otherwise modify the filename as needed.
http://us2.php.net/imagepng

Note: I think you're technically supposed to open a new question for this, so if you tell anyone I told you, I'll deny it. :P
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now