Create a dynamic PNG image

Active3 hr before
Viewed126 times

5 Answers


Here's the code that I used before to generate an image with two names, which are accepted from query string parameters. I use a prepared background image and put the names on top of it.,I want to create a small function in PHP which takes in arguments like color, shape, transparency etc. and outputs a PNG image. I heard about PHP GD library but I want to know how can one create something as creative as,If you wanted to make an image like that dynamically based on some parameters you can always create the images beforehand in photoshop and then overlay them based on what a user selects.,2nd Edit: This is just something I have done with this sort of technology. Bear in mind it was quite a while ago. It accepts a name based on the query string and basically does a few loops with a lot of random numbers.

Here is the source code, I apologize for any stupid code/quotes. This was written quite a while ago, when I was about 14 I believe (probably many flaws).

< ? php
$array = array("I am a monument to all your sins", "Currently making pizza", "Best before 12/7/09", "Farming Onions");

function imagettftext_cr( & $im, $size, $angle, $x, $y, $color, $fontfile, $text) {
   // retrieve boundingbox
   $bbox = imagettfbbox($size, $angle, $fontfile, $text);
   // calculate deviation
   $dx = ($bbox[2] - $bbox[0]) / 2.0 - ($bbox[2] - $bbox[4]) / 2.0; // deviation left-right
   $dy = ($bbox[3] - $bbox[1]) / 2.0 + ($bbox[7] - $bbox[1]) / 2.0; // deviation top-bottom
   // new pivotpoint
   $px = $x - $dx;
   $py = $y - $dy;
   return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text);
$image = imagecreate(500, 90);
$black = imagecolorallocate($image, 0, 0, 0);
$grey_shade = imagecolorallocate($image, 40, 40, 40);
$white = imagecolorallocate($image, 255, 255, 255);

$text = $array[rand(0, sizeof($array) - 1)];

// Local font files, relative to script
$otherFont = 'army1.ttf';
$font = 'army.ttf';

if ($_GET['name'] == "") {
   $name = "Sam152";
} else {
   $name = $_GET['name'];
$name = substr($name, 0, 25);

//BG text for Name
while ($i < 10) {
   imagettftext_cr($image, rand(2, 40), rand(0, 50), rand(10, 500), rand(0, 200), $grey_shade, $font, $name);
//BG text for saying
while ($i < 10) {
   imagettftext_cr($image, rand(0, 40), rand(90, 180), rand(100, 500), rand(200, 500), $grey_shade, $otherFont, $text);

// Main Text
imagettftext_cr($image, 35, 0, 250, 46, $white, $font, $name);
imagettftext_cr($image, 10, 0, 250, 76, $white, $otherFont, $text);

load more v

The following code generates a random dynamic image using PHP. The GD image functions are used to create dynamic image with PHP.,The example code creates a dynamic image with PHP based on the specified configuration. You can customize the image width, height, background color, text color, and text.,The image functions of GD library is the easiest way to create dynamic image with text in PHP. In some situation, you need to create an image on the fly and write dynamic text to image. To generate a dynamic random image with PHP, GD library needs to be installed. In this tutorial, we will show you how to create dynamic image with PHP.,Usage: Specify the URL of the dynamic image generation script with the configuration option in the src tag.

Specify the URL of the dynamic image generation script with the configuration option in the src tag.

<img src="create_image.php?s=000_FFF_350_350&t=Dynamic Image">

For example, the following code generates a page of HTML and image tags using dynamic buttons, as shown in Figure 15-9: , You want to create an image based on a existing image template and dynamic data (typically text). For instance, you want to create a hit counter. ,In this script, if a value is passed in for $_GET['button'], we generate a button and send out the PNG. If $_GET['button'] isn't set, we print a basic HTML page with two embedded calls back to the script with requests for button images — one for a Previous button and one for a Next button. A more general solution is to create a separate button.php page that returns only graphics and set the image source to point at that page. ,Load the template image, find the correct position to properly center your text, add the text to the canvas, and send the image to the browser:

// Configuration settings
$image = ImageCreateFromPNG('button.png');
$text = $_GET['text'];
$font = ImagePSLoadFont('Times');
$size = 24;
$color = ImageColorAllocate($image, 0, 0, 0); // black
$bg_color = ImageColorAllocate($image, 255, 255, 255); // white

// Print centered text
list($x, $y) = pc_ImagePSCenter($image, $text, $font, $size);
ImagePSText($image, $text, $font, $size, $color, $bg_color, $x, $y);

// Send image
header('Content-type: image/png');

// Clean up
load more v

Using imagecopymerge to create a translucent watermark ,Image Processing and Generation, Examples PNG creation with PHP Adding watermarks to images using alpha channels Using imagecopymerge to create a translucent watermark ,Example #1 PNG creation with PHP

? phpheader("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagedestroy($im); ? >
load more v

Figure 6: A JPEG image converted from PNG format,Images can also be rotated! Perhaps you’d like to rotate the above image 45 degrees in a clockwise direction? Easy!, Figure 7: A rotated image, Figure 3: A cropped image

Regardless of the type of operation you want to perform on an image, first of all, you need to load that image into computer memory, and assign it to a MagickWand resource. The following PHP script loads a sample image file from the current directory, then displays it in the web browser:

<?php // script_1.php

< ? php // script_1.php 
load more v

Other "dynamic-create" queries related to "Create a dynamic PNG image"