Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dynamic PNG image

Tags:

php

image

png

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 soon.media.mit.edu

like image 881
apnerve Avatar asked May 23 '09 09:05

apnerve


People also ask

How to create a PNG file in PHP?

PNG creation with PHP ¶header("Content-type: image/png"); $string = $_GET['text']; $im = imagecreatefrompng("images/button1. png");

How to generate image using PHP?

imagecreate() function in PHP The imagecreate() function is used to create a new image. It is preferred to use imagecreatetruecolor() to create an image instead of imagecreate(). This is because the image processing occurs on the highest quality image possible which can be created using imagecreatetruecolor().


1 Answers

This is a good example, you can do virtually everything using these functions. While possible, creating an image like the one you described would be pretty hard by I have done some weird stuff with gradients, loops and colors though.

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.

There is a lot of fun you can have.

Edit: Oh by the way, if your interested giving an invalid parameter shows some of the python code that is responsible for creating the image and causing the error. It would be a good place to get some idea of the code.

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 header("Content-type:image/jpeg"); $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); $i++; } //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); $i++; }  // Main Text imagettftext_cr($image,35,0,250,46,$white,$font,$name); imagettftext_cr($image,10,0,250,76,$white,$otherFont,$text); imagejpeg($image);  ?> 
like image 155
Sam152 Avatar answered Sep 30 '22 22:09

Sam152