Creating an image in PHP

You can do a lot with PHP and creating an image is one of them. You can add text or even other images onto your base image, here is how:

function create_image($width, $height, $text)
{
        $image = ImageCreate($width, $height);
        $white = ImageColorAllocate($image, 255, 255, 255);//white
        $grey = imagecolorallocate($image, 128, 128, 128);//grey
        $black = imagecolorallocate($image, 0, 0, 0);//black
        ImageFill($image, 0, 0, $white);
        $font = 'fonts/arial.ttf';//make sure chosen font is in the directory!!
        imagettftext($image, 20, 0, 4, 5, $grey, $font, $text);//text shadow
        imagettftext($image, 20, 0, 3, 4, $black, $font, $text);//text
        $save = "phpimage.png";//image will be save in this files directory as "phpimage.png"
        imagepng($image, $save);
        return $save;
        ImageDestroy($image);//Free memory
}

echo create_image(600, 200, 'Sample text for image');


The function create_image will create a white background image with your defined dimensions. It will then add your text and add text shadows then saving the image into the directory. Please not this method has a ‘custom’ font defined and you must have arial.ttf in the fonts folder in your directory for this script to work.

To add an image:

$picture = 'logo.png';
imagecopy($image, $picture, 55, 70, 0, 0, 48, 48);

For more understanding search: imagettftext and imagecopy.