Jump to content

Archived

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

ari_aaron

Image generator?

Recommended Posts

How can I have a script toake some text, and make it into an image?

I am getting my data from an SQL database, and I want to display the text it generates as a jpg file.

How can I do this?

Share this post


Link to post
Share on other sites
Here's some code which will right the specified text [b]$text[/b] onto an image, centering the text and then return an image.
[code]
<?php

//Width, Height , Font Text
$width = 100;
$height = 20;
$font = 3;  //This is probably fine
$text = 'Put your text here';

$im = imagecreate( $width,$height );
$background = imagecolorallocate( $im ,255 , 255 , 255 );
$color = imagecolorallocate( $im , 0 , 0 ,0 );

imagestring( $im , $font , ($width-(imagefontwidth($font)*strlen($text)))/2 ,($height-(imagefontheight($font)))/2 , $text , $color  );
header( 'Content-type: image/jpeg' );
imagejpeg( $im , False , 80 );[/code]

Here's a version where it gets the text from the URL:
[code]
<?php

//Width, Height , Font Text
$width = 100;
$height = 20;
$font = 3;  //This is probably fine
$text = (( isset($_GET['text']) )?($_GET['text']):('N/A'));

$im = imagecreate( $width,$height );
$background = imagecolorallocate( $im ,255 , 255 , 255 );
$color = imagecolorallocate( $im , 0 , 0 ,0 );

imagestring( $im , $font , ($width-(imagefontwidth($font)*strlen($text)))/2 ,($height-(imagefontheight($font)))/2 , $text , $color  );
header( 'Content-type: image/jpeg' );
imagejpeg( $im , False , 80 );
[/code]

So, if you put this code in a page called [b]drawimage.php[/b] then all you have to do is put this url in an [b]<img src="">[/b] tag: drawimage.php?text=17373 and it will draw 17373 on the image.

You can set the width/height by changing the variables at the top of the script.

Cheers.

Share this post


Link to post
Share on other sites
I have done this a couple of times. What you want to do is use GD library. I think it is only installed on 4.3+ (I may be wrong). This is a really great feature php has. So you show research it well. Go here http://us3.php.net/gd its got lots of info on anything you can think of.

Share this post


Link to post
Share on other sites
Can someone help me to get the text to be added on at the END of the image, please?

I am using this code to get the image:
[code]$fp = fopen($name, 'rb');
header("Content-Type: image/jpg");
header("Content-Length: " . filesize($name));
fpassthru($fp);[/code]

Is there a way that I can add text onto the end?

Share this post


Link to post
Share on other sites
[quote author=ari_aaron link=topic=105188.msg421361#msg421361 date=1156426948]
Can someone help me to get the text to be added on at the END of the image, please?
[/quote]

What's the "end of an image" mean?  The imagestring() function allows you to choose the x/y location to begin the text. 

Share this post


Link to post
Share on other sites
I want to show an image using the code that I said, and then underneath it, I want some text.

Share this post


Link to post
Share on other sites
[quote author=ari_aaron link=topic=105188.msg421367#msg421367 date=1156427653]
I want to show an image using the code that I said, and then underneath it, I want some text.
[/quote]

As part of the image, or is this just an html question?

Share this post


Link to post
Share on other sites

×

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.