Jump to content


Photo

How to create dynamic images by using text-based variables?


  • Please log in to reply
6 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 30 June 2006 - 12:38 PM

so I have a database with a bunch of "missions", and for each one I have lots of stats, including processor, data bus, etc. I'd like to retrieve these variables (not a problem) then put them in a certain place on my template image (problem). I have GD on my server for image manipulation, but have no explerience with it. can someone explain to me how to put the contents of a variable containing text into an image using GD?

Here's an example:

Posted Image

And my template:

Posted Image
http://www.DaveLinger.com
dave at linger dot com

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 30 June 2006 - 12:45 PM

Here's an example from the manual, using the function imagettftext() :
<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>


#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 30 June 2006 - 12:52 PM

thanks! Just a few questions...

how would I use my own image instead of a filled white rectangle? ;D

also, in "imagettftext($im, 20, 0, 10, 20, $black, $font, $text);", are 20,0,10,20 the co-ordinates for the location of the text or what? ???
http://www.DaveLinger.com
dave at linger dot com

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 12:59 PM

20- size
0- angle
10- x-coord
20- y-coord

imagettftext()

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 30 June 2006 - 01:00 PM

[hug]

now what about using my own image as the background instead of the white block?
http://www.DaveLinger.com
dave at linger dot com

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 30 June 2006 - 01:10 PM

You could use imagecreatefromjpeg()

All of the GD functions can be found at php.net/gd for future reference! :)

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 30 June 2006 - 01:16 PM

Thanks!

(these new smileys are like satanic)
http://www.DaveLinger.com
dave at linger dot com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users