Solved

PHP JQuery to convert number to image

Posted on 2014-02-01
11
653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 110

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 110

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 110

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
 
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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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