Solved

PHP JQuery to convert number to image

Posted on 2014-02-01
11
649 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to count occurrences of each item in an array.

739 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