?
Solved

Using GD Library to create dynamic image AND add it to page content

Posted on 2012-03-24
3
Medium Priority
?
285 Views
Last Modified: 2012-03-28
Hi,
I have some code below that takes an existing png file, adds some dynamic copy to it and generates a new image with the copy included into the image. Works great, no issues.
Problem I'm finding is I need to include the image along with some typical html content when the page is called. I can't add any html etc. to it because of the header info. Without it, the page just displays as raw gibberish for the image.

How can I get the dynamic image to display along with some html content?



$name = $_REQUEST["name"];


$filename = "certificate.png";  //name of image file
$fontsize = 30;                    //size of font
$fontfile = "Arial";                //name of ttf file, w/o ttf extension (courier in this case)
$text = "Congratulations ".$name."";             //text to write centered
$text2 = "You will receive $1000";             //second line to write centered


$im = imagecreatefrompng ($filename); //create the image resource


list ($im_width, $im_height) = getimagesize($filename);

//calculate the size of the bounding box  
$bbox = imagettfbbox($fontsize, 0, $fontfile, $text);
$bbox2 = imagettfbbox($fontsize, 0, $fontfile, $text2);
$text_width = $bbox[2]-$bbox[0];
$text_height = $bbox[1]-$bbox[7];
$text_width2 = $bbox2[2]-$bbox2[0];
$text_height2 = $bbox2[1]-$bbox2[7];

//write the text to the file, centered as calculated from (W-w)/2
//where W is the image dimension and w is the text dimension
imagettftext($im, 30, 0, ($im_width-$text_width)/2, ($im_height-$text_height)/2, 0, $fontfile, $text);
imagettftext($im, 20, 0, ($im_width-$text_width2)/2, ($im_height-$text_height2)/2 + 40, 0, $fontfile, $text2);

header('Content-type: image/png');
imagepng($im);
//destroy the image resource
imagedestroy($im);

Open in new window

0
Comment
Question by:tjyoung
[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
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37761403
You can use an HTML tag like this to create the image:

<img src="path/to/image.php?img=image.png&text=Hello+World" />

The browser will request the file at path/to/image.php and will send the GET request containing img=image.png&text=Hello+World

once you have that working, you can add that tag to the rest of your HTML and you should be good to go!
0
 
LVL 1

Author Comment

by:tjyoung
ID: 37761412
Yep, good to go.
Thanks as always.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37761439
You're welcome.  Give it a try and if something goes Charlie Sheen on you, post back and I'll try to help!
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

762 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