Create PNG using PHP

Posted on 2009-07-14
Last Modified: 2013-12-13
I have a MySQL database and would like to have a script in PHP that for each row that the script is looking at, would be able to take the data and arrange them on the page at the given dimension on a newly created PNG or JPEG.  One of the fields in the database would point to an image saved on the server that I would also like to appear on the new image file (as part of the new image, NOT showing the link to the image).

How can this be done?
Question by:tru504187211
LVL 34

Accepted Solution

gr8gonzo earned 500 total points
ID: 24855333
Use the GD library:

// Create a 500x400 image
$im = imagecreate(500,400);

// Allocate some colors
$background_color = imagecolorallocate($im, 255, 255, 255); // White
$text_color = imagecolorallocate($im, 0, 0, 0); // Black

// Write some text at 20 pixels from the left, 10 pixels from the top
imagestring ($im, 1, 20, 10, "Some text", $text_color);

// Write some more text at 60 pixels from the left, 10 pixels from the top
imagestring ($im, 1, 60, 10, "Column 2", $text_color);

// Write some more text at 60 pixels from the left, 10 pixels from the top
imagestring ($im, 1, 60, 10, "Column 2", $text_color);

// Create a PNG file
imagepng($im, "newimage.png");

// Show in the browser
print "<img src='newimage.png'>";

Yo ucan also use functions like imagecreatefrompng() and imagecopy() copy an existing image INTO the newimage.png image you are creating.

There's a LOT of ways to do this, but if you go to those links, you'll be able to  see all the different GD functions on the left-hand side. It's worth time to go through them and look at all the examples.

Author Closing Comment

ID: 31603540
gr8gonzo - it appears that this will work, however, the solution to my problem is going to have to be a completely different route.  I do plan on using this function for a separate project I am working on, so THANK YOU!

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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…

831 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