ari_aaron Posted August 22, 2006 Share Posted August 22, 2006 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? Link to comment https://forums.phpfreaks.com/topic/18302-image-generator/ Share on other sites More sharing options...
ShogunWarrior Posted August 22, 2006 Share Posted August 22, 2006 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 More sharing options...
ari_aaron Posted August 22, 2006 Author Share Posted August 22, 2006 THANK YOU! Link to comment https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-78623 Share on other sites More sharing options...
foreverhex Posted August 22, 2006 Share Posted August 22, 2006 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 More sharing options...
ari_aaron Posted August 24, 2006 Author Share Posted August 24, 2006 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 More sharing options...
AndyB Posted August 24, 2006 Share Posted August 24, 2006 [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 More sharing options...
ari_aaron Posted August 24, 2006 Author Share Posted August 24, 2006 I want to show an image using the code that I said, and then underneath it, I want some text. Link to comment https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-79823 Share on other sites More sharing options...
AndyB Posted August 24, 2006 Share Posted August 24, 2006 [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 https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-79830 Share on other sites More sharing options...
ari_aaron Posted August 24, 2006 Author Share Posted August 24, 2006 As part of the image. Link to comment https://forums.phpfreaks.com/topic/18302-image-generator/#findComment-79831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.