Solved

PHP JQuery to convert number to image

Posted on 2014-02-01
11
619 Views
Last Modified: 2014-02-03
Does anyone know if a piece of javascript, JQuery or PHP that can convert a number into an image?  It is for a visual that represents total money raised, like a thermometer?
0
Comment
Question by:rgranlund
11 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39826735
This article, while not exactly on point, shows how PHP renders string characters as images.  I'll try to find you a code sample, too.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_9849-Making-CAPTCHA-Friendlier-with-PHP-Image-Manipulation.html

Also, fair warning - I have never tested this, but it might be promising.
http://plugins.jquery.com/thermometer/
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39826784
You can use this image script as the src= attribute of the <img> tag.  Just give it the n= argument, and away you go!  Experiment with it to get the look and feel you want.

See http://www.laprbass.com/RAY_temp_rgranlund.php?n=1024

<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28354076.html

// GET THE NUMBER FROM THE URL ARGUMENT "n="
$num = isset($_GET['n']) ? $_GET['n'] : FALSE;
if ($num === FALSE){
    trigger_error("MISSING URL ARGUMENT n=", E_USER_ERROR);
}

// ADJUST THIS FOR YOUR NEEDS
$str = str_pad(number_format($num),8, ' ', STR_PAD_LEFT);

// ADJUST ALL OF THESE FOR YOUR NEEDS, TOO
// CREATE THE IMAGE RESOURCE
$img = imageCreateTrueColor
( 200     // WIDTH
, 60      // HEIGHT
)
;
// WHITE BACKGROUND COLOR
$bgc = imageColorAllocate
( $img   // IMAGE RESOURCE
, 255    // RED
, 255    // GREEN
, 255     // BLUE
)
;
// FIREY RED TEXT COLOR
$txt = imageColorAllocate
( $img   // IMAGE RESOURCE
, 233    // RED
, 14     // GREEN
, 91     // BLUE
)
;
// PAINT THE BACKGROUND
imageFill
( $img   // IMAGE RESOURCE
, 0      // X-OFFSET
, 0      // Y-OFFSET
, $bgc   // FILL COLOR
)
;
// WRITE THE TEXT
imageString
( $img   // IMAGE RESOURCE
, 5      // FONT (SEE THE MAN PAGE)
, 0      // X-OFFSET
, 0      // Y-OFFSET
, $str   // THE STRING DATA
, $txt   // THE TEXT COLOR
)
;
// SEND THE IMAGE INTO THE BROWSER OUTPUT STREAM
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);

Open in new window

HTH and best regards, ~Ray
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39827406
This is also a good option
http://www.daftlogic.com/projects-text-to-image.htm
have tested it earlier and work quite well
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39827662
@rinfo: I tested the daftlogic script and was unable to get it to work.  It looks like something left over from PHP4 days (no class constructor, use of "var" keyword, etc).  Can you please post a working example?   Thanks, ~Ray
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39827702
Hello Ray
I am submitting code that i have used.
I have used it as it is.
And i am also submitting snapshot of output.
txt2image.php
txt2image.png
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:rinfo
ID: 39827704
I have php 5.4 installed as of now.
0
 
LVL 9

Expert Comment

by:rinfo
ID: 39827706
Of course it would require a fonts directory with times.ttf in it .
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39828024
Great, thanks for posting that ~Ray
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39829121
If you want a thoroughly modern approach, then you could always use the HTML5 Canvas element and draw your thermometer directly using jQuery :)
0
 
LVL 7

Author Closing Comment

by:rgranlund
ID: 39830410
This answer led me to what I was trying to discover.  Thank you.  However, I will be looking at the rest of the answers soon.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39830421
Thanks for the points and thanks for using EE.  And good luck with your project, ~Ray
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show confirmation alert in Jquery 14 43
Amazon Redshift 2 26
Angular - break the dataset in chunks? 7 39
Bootstrap collapse causes odd behavior with php loop 7 24
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now