Solved

PHP JQuery to convert number to image

Posted on 2014-02-01
11
614 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Expert Comment

by:rinfo
Comment Utility
I have php 5.4 installed as of now.
0
 
LVL 9

Expert Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
Great, thanks for posting that ~Ray
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points and thanks for using EE.  And good luck with your project, ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

9 Experts available now in Live!

Get 1:1 Help Now