Jump to content

GD ttf font color


nbarone

Recommended Posts

I am trying to make a shadow behind text...however, my font color is sticking to one color, even though I changed it...

 

edit: I just checked and the color it is displaying IS NOT any color I have allocated...

 

<?php
$font = "coopheavy.ttf"; // font file
$image = imagecreatefromgif('virtue_blank.gif');
header ("Content-type: image/png"); 

$virtue = "Faith";
$verse = "hebrews 11:1";
$line1 = "This month's virtue";
$line2 = "trusting in what you can't see";
$line3 = "because of what you can see";

$virtue_x = 300;
$virtue_y = 100;
$add_shadow = 2;
// 360* shadows
$fontColor = imagecolorallocate($image, 39, 121, 190); // shadow color
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);

//$fontColor2 = imagecolorallocate($image, 252, 199, 68); // base color 
$fontColor2 = imagecolorallocate($image, 0, 0, 0); // base color 
imagettftext($image, 36, 0, $virtue_x, $virtue_y, $fontColor2, $font, $virtue);
// display
imagepng($image);
imagedestroy($image);
?>

Link to comment
https://forums.phpfreaks.com/topic/164534-gd-ttf-font-color/
Share on other sites

I don't see anything wrong. Can you post a copy of how the picture looks and describe what exactly is wrong with how it looks?

 

This is the output

virtuephp.png

 

here is the "virtue_blank.gif"

virtue_blank.gif

 

it appears it's grabbing a random color off of the image and using that instead of the allocated images.

When I change imagecreatefromgif('file') to imagecreate(600,300) (and allocate a bg), everything works like it should...

Link to comment
https://forums.phpfreaks.com/topic/164534-gd-ttf-font-color/#findComment-867855
Share on other sites

Try creating a blank image using imagecreatetruecolor(), then use imagecopyresampled() to move the gif to the new image, then add your text

 

that worked, not exactly the method I wanted to use, but with time on the line I'm in no situation to be picky! thank you!

Link to comment
https://forums.phpfreaks.com/topic/164534-gd-ttf-font-color/#findComment-867870
Share on other sites

Here is my current script:

<?php
$font = "coopheavy.ttf"; // font file
$image = imagecreatetruecolor(625,168);
$file = imagecreatefromgif("virtue_blank.gif");
imagecopyresampled($image,$file,0,0,0,0,625,168,625,168);

header ("Content-type: image/png"); 
$tmv = "this month's virtue";
$virtue=$_REQUEST['virtue'];
$verse=$_REQUEST['verse'];
$line2=$_REQUEST['line2'];
$line3=$_REQUEST['line3'];
if(!$virtue) $virtue = "faith";
if(!$verse) $verse = "hebrews 11:1";
if(!$line2) $line2 = "trusting in what you can't see";
if(!$line3) $line3 = "because of what you can see";

$img_arr = imagettfbbox(36,0,$font,$virtue); 
$virtue_width = $img_arr[2]; 
$img_arr = imagettfbbox(12,0,$font,$line2); 
$line2_width = $img_arr[2]; 
$img_arr = imagettfbbox(12,0,$font,$line3); 
$line3_width = $img_arr[2]; 
$img_arr = imagettfbbox(12,0,$font,$tmv); 
$tmv_width = $img_arr[2]; 
$img_arr = imagettfbbox(12,0,$font,$verse); 
$verse_width = $img_arr[2];

$center_line = 325;
$tuck_verse = 0;
$virtue_center = ($virtue_width / 2);
$virtue_x = ($center_line - $virtue_center);
$tmv_center = ($tmv_width / 2);
$tmv_x = ($center_line - $tmv_center);
$line2_center = ($line2_width / 2);
$line2_x = ($center_line - $line2_center);
$line3_center = ($line3_width / 2);
$line3_x = ($center_line - $line3_center);
if($line3=="") { $line3_center = $line2_center; $tuck_verse = 15; }
$verse_x = (($line3_center - $verse_width) + $center_line);
if($verse_x < $line3_x) $verse_x = ($line3_x + 5);

$virtue_y = 95;
$fontColor = imagecolorallocate($image, 39, 121, 190);
$add_shadow = 2;
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-($add_shadow-1), $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-($add_shadow-1), $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+($add_shadow-1), $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+($add_shadow-1), $virtue_y-$add_shadow, $fontColor, $font, $virtue);
$fontColor2 = imagecolorallocate($image, 252, 199, 68);
imagettftext($image, 36, 0, $virtue_x, $virtue_y, $fontColor2, $font, $virtue);


$white = imagecolorallocate($image, 255, 255, 255);
imagettftext($image, 12, 0, $tmv_x, 50, $white, $font, $tmv);
imagettftext($image, 12, 0, $line2_x, 120, $white, $font, $line2);
imagettftext($image, 12, 0, $line3_x, 135, $white, $font, $line3);
$blue = imagecolorallocate($image, 0, 82, 165);
imagettftext($image, 12, 0, $verse_x, (155-$tuck_verse), $blue, $font, $verse);



// display
imagepng($image);
imagedestroy($image);
?>

 

 

I want to simplify this, if possible:

$fontColor = imagecolorallocate($image, 39, 121, 190);
$add_shadow = 2;
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-($add_shadow-1), $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-($add_shadow-1), $virtue_y+$add_shadow, $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+($add_shadow-1), $fontColor, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+($add_shadow-1), $virtue_y-$add_shadow, $fontColor, $font, $virtue);
$fontColor2 = imagecolorallocate($image, 252, 199, 68);
imagettftext($image, 36, 0, $virtue_x, $virtue_y, $fontColor2, $font, $virtue);

 

it makes a shadow outside of the image, 2px in range ... however, since it gets choppy around edges, I had to add the shadow in 1px range also. is there any GD formula to do this automatically? I didn't try a bigger font size, because I'm under the impression that it will go beyond the actual text eventually.

 

I have simplified to this; can I go any further? I'm trying to make this as light-weight as possible: (also changed to $shadow_color)

$add_shadow = 2;

for($i=0;$i<$add_shadow;$i++){
imagettftext($image, 36, 0, $virtue_x+($add_shadow-$i), $virtue_y+($add_shadow-$i), $shadow_color, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-($add_shadow-$i), $virtue_y-($add_shadow-$i), $shadow_color, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+$add_shadow, $virtue_y-($add_shadow-$i), $shadow_color, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-($add_shadow-$i), $virtue_y+$add_shadow, $shadow_color, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x-$add_shadow, $virtue_y+($add_shadow-$i), $shadow_color, $font, $virtue);
imagettftext($image, 36, 0, $virtue_x+($add_shadow-$i), $virtue_y-$add_shadow, $shadow_color, $font, $virtue);
}

Link to comment
https://forums.phpfreaks.com/topic/164534-gd-ttf-font-color/#findComment-867909
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.