Jump to content

Creating text images with GD Library


Go to solution Solved by Barand,

Recommended Posts

just started messing with the php images tonight so this is a mess.  ive been patching together bits and pieces from scripts i found online and reading the PHP Manual to get something that prints graphic text with transparency for me, as expected.

what im not understanding, is how the background color is dictated. my desire is to have no background. so ive been trying to make it transparent. with no luck.

what i want:
text with say, only 10% transparency so that i can use it as a watermark. no background to the text at all

what im getting:
the transparent text but a solid black bg. i cant even get the background color to change, much less have transparency.
 

Header("Content-type: image/png");
class textPNG
{
var $font = 'font/Avant Garde Demi BT.ttf'; //default font. directory relative to script directory.
var $msg = "no text"; // default text to display.
var $size = 88; // default font size.
var $rot = 33; // rotation in degrees.
var $pad = 110; // padding.
var $transparent = 1; // transparency set to on.
var $red = 255; // black text...
var $grn = 255;
var $blu = 255;
var $bg_red = 255; // on white background.
var $bg_grn = 255;
var $bg_blu = 255;

function draw()
{
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";

// get the font height.
$bounds = imagettfbbox($this->size, $this->rot, $this->font, "W");

if ($this->rot < 0)
{
$font_height = abs($bounds[7]-$bounds[1]);
}

else if ($this->rot > 0)
{
$font_height = abs($bounds[1]-$bounds[7]);
}

else
{
$font_height = abs($bounds[7]-$bounds[1]);
}

// determine bounding box.
$bounds = imagettfbbox($this->size, $this->rot, $this->font, $this->msg);

if ($this->rot < 0)
{
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;
}

else if ($this->rot > 0)
{
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);
}

else
{
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;;
$offset_x = 0;
}

//$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
$image = imagecreatetruecolor($width+($this->pad*2)+1,$height+($this->pad*2)+1);

// Turn off alpha blending and set alpha flag
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);


//$background = imagecolorallocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
//$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
//$background = imagecolorallocatealpha($image, $this->bg_red, $this->bg_grn, $this->bg_blu, 127);
$background = imagecolorallocatealpha($image, 255, 255, 255, 127);
$foreground = imagecolorallocatealpha($image, $this->red, $this->grn, $this->blu, 100);



if ($this->transparent){ imagecolortransparent($image, $background); }

imageinterlace($image, false);

// turn alphblending back on before laying text
ImageAlphaBlending($image, true);

// render the image
imagettftext($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);

// output PNG object.
imagepng($image);
imagepng( $image, "bmw_img.png" ); // save img to file

}
}

$text = new textPNG;
//$text->red = '111';
//$text->grn = '111';
//$text->blu = '111';


if (isset($msg)) $text->msg = $msg; // text to display
if (isset($font)) $text->font = $font; // font to use (include directory if needed).
if (isset($size)) $text->size = $size; // size in points
if (isset($rot)) $text->rot = $rot; // rotation
if (isset($pad)) $text->pad = $pad; // padding in pixels around text.
if (isset($red)) $text->red = $red; // text color
if (isset($grn)) $text->grn = $grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // background color.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
if (isset($tr)) $text->transparent = $tr; // transparency flag (boolean).

$text->draw();
Link to comment
https://forums.phpfreaks.com/topic/297602-creating-text-images-with-gd-library/
Share on other sites

  • Solution

If you use imagecreate() then, by default, the background colour is the first colour allocated with imagecolorallocate().

 

But, if you use imagecreatetruecolor() then you need to perform an explicit fill with the defined colour

imagefill($image,0,0,$background);
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.