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
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
LVL 35

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

690 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