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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted August 22, 2006 Author Share Posted August 22, 2006 THANK YOU! Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
ari_aaron Posted August 24, 2006 Author Share Posted August 24, 2006 As part of the image. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.