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
https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-78620
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
https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-78624
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
https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-79817
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
https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-79820
Share on other sites

Archived

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

×
×
  • 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.