Go Premium for a chance to win a PS4. Enter to Win


Create PNG using PHP

Posted on 2009-07-14
Medium Priority
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 35

Accepted Solution

gr8gonzo earned 2000 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() http://us.php.net/manual/en/function.imagecreatefrompng.php

...to 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

972 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