Jump to content


Photo

Image generator?


  • Please log in to reply
8 replies to this topic

#1 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 22 August 2006 - 02:43 PM

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?

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 22 August 2006 - 03:05 PM

Here's some code which will right the specified text $text onto an image, centering the text and then return an image.
<?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 );

Here's a version where it gets the text from the URL:
<?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 );

So, if you put this code in a page called drawimage.php then all you have to do is put this url in an <img src=""> 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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 22 August 2006 - 03:08 PM

THANK YOU!

#4 foreverhex

foreverhex
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts

Posted 22 August 2006 - 03:11 PM

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.

#5 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 24 August 2006 - 01:42 PM

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:
$fp = fopen($name, 'rb');
header("Content-Type: image/jpg");
header("Content-Length: " . filesize($name));
fpassthru($fp);

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

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 24 August 2006 - 01:52 PM

Can someone help me to get the text to be added on at the END of the image, please?


What's the "end of an image" mean?  The imagestring() function allows you to choose the x/y location to begin the text. 
Legend has it that reading the manual never killed anyone.
My site

#7 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 24 August 2006 - 01:54 PM

I want to show an image using the code that I said, and then underneath it, I want some text.

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 24 August 2006 - 02:00 PM

I want to show an image using the code that I said, and then underneath it, I want some text.


As part of the image, or is this just an html question?
Legend has it that reading the manual never killed anyone.
My site

#9 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 24 August 2006 - 02:01 PM

As part of the image.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users