Jump to content

Image generator?


ari_aaron

Recommended Posts

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.
Link to comment
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.
Link to comment
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?
Link to comment
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. 
Link to comment
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?
Link to comment
Share on other sites

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.