• 0

Generating Images on the Fly


Question

Does anyone know how I can get PHP or JSP to create JPEGs from some text?

I want to input a string $myString="Hello Worlds"; for example and get that to generate an image that looks like the one attached.

Thanks.

post-46-1044424317.jpg

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

there is a better way than this but the way ive done it with my sig is

$image=imageCreateFromPNG("123.png");

imageString($image,5,10,10,$mystring,000000);

imagePNG($image);

imageDestroy($image);

that will create a image from 123.png and put the text on it

Link to comment
Share on other sites

  • 0

<?

Header("Content-Type: image/jpeg"); //Image header

//Gets text, and converts spaces
$string=implode($argv," ");
$string = str_replace("%20", " ", $string); 
$string = str_replace("_", " ", $string);

$myimage = ImageCreateFromJpeg("baseimage.jpg"); //Base image

$black = ImageColorAllocate($myimage, 0, 0, 0); //colour (rgb last 3 numbers)

$fontsize = 5; ?//1 - 5 font size
$xcoord = 25; ?//X-coordinate
$ycoord = 150; ?//Y-coordinate, both from top left cornor

ImageString($myimage, $fontsize , $width, $height, $string, $black); //writes text

ImageJpeg($myimage); //creates image

ImageDestroy($myimage);

?>

save that in a php file, nothing else and there must be no spaces or anything out side the <? ?> bits

then to call it u can use in html: <img src="filename.php?Hello World" border="0">

if udont want to input the string from the link u can set it in the page, u can use either jpeg or png just change hat bit of all the commands, gif is no longer suport:( :(

if u dont want to use a base image then u can create one from scatch but im not sure how

Link to comment
Share on other sites

  • 0

Great stuff! Thanks.

There are 3 more questions that I am hoping you can help me with on this issue:

1) Can the image now be saved? I mean, I want it to automatically same the image in some directory for me.

2) How can I add a carriage return in the text?

I've tried using the * character and replacing it with a "\n ".

Here the code I used:

$string = str_replace("*", "\n", $string);

And here's the input string:

Hello*Worlds

But it didn't put a carriage return in between the two words. Any thoughts on this?

3) Can I use this code in my JSP pages too?

Thanks.

Edited by AshMan
Link to comment
Share on other sites

  • 0

I'm looking fo something similar to this, but i want it so that my users can have graphics made with the text they want using a base style (similar to flaming text)

Is this possible?

Link to comment
Share on other sites

  • 0

1) i have no idea,

2) i was tring to do the same thing all last night :p

finally got it to work, but it depends on certin limits of the image, this is the code i used:

&lt;?

Header("Content-Type: image/jpeg");

$maxlines = 8;
$maxperline = 30;

$string=implode($argv," ");

$string = str_replace("%20", " ", $string);
$string = str_replace("_", " ", $string);

$string = wordwrap( $string, $maxperline );
$stringarr = explode("\n", $string);
$lines = count($stringarr);

$myimage = ImageCreateFromJpeg("baseimage.jpg");

$black = ImageColorAllocate($myimage, 0, 0, 0);

$width = 25;

if($lines &gt; $maxlines)
{
	$lines = $maxlines;
}

switch($lines)
{
	case 1:
	$height = 180;
	break;

	case 2:
	$height = 175;
	break;

	case 3:
	$height = 165;
	break;

	case 4:
	$height = 160;
	break;

	case 5:
	$height = 155;
	break;

	case 6:
	$height = 150;
	break;

	case 7:
	$height = 140;
	break;

	case 8:
	$height = 135;
	break;
}

for ($i = 0; $i &lt; $lines; $i++)
{
    ImageString($myimage, 5, $width, $height, $stringarr[$i], $black);
    $height = $height + 15;
}

ImageJpeg($myimage);

ImageDestroy($myimage);

?&gt;

on my image its like a comic with character and then text box, so this will fit the text nicly in it

3) the image is self contained in that file, so u can call it from anything and just pass the right paramaters and it should function like a normal image

I'm looking fo something similar to this, but i want it so that my users can have graphics made with the text they want using a base style (similar to flaming text)

i think that might be a little beyond what this can do, the rest of the craphics options are very simplistic, like drawing basic shapes and stuff :unsure:

Link to comment
Share on other sites

  • 0

I dunno about JSP. It's done in php through gd_image, which is now an included library with the package. It's separate in terms of development, but I don't know how much it will plug into java or other projects...

Link to comment
Share on other sites

  • 0

I've already posted the full source to my sig, maybe that can help you a bit.. (the link is below my sig).

.. as for your 1) question, I'm trying to do that as well, I'm working on putting progressive level bars in my sig, but haven't figured it out yet. I'm thinking I would need to do a 2-pass thing: first generate the first image, save it, and then generate the image (with the graph) on top of that.

Link to comment
Share on other sites

This topic is now closed to further replies.