Jump to content

Simple: Write some text to an image


aximbigfan

Recommended Posts

Hi,

 

I need to create a function to make an image (600X600) and simply write some text in the center (or wherever) of said image. The image must then be returned in the form of a string.

 

I have never worked with images in PHP, or any other editor other than Mspaint. What is the best place to start?

 

Thanks,

Chris

Link to comment
https://forums.phpfreaks.com/topic/131277-simple-write-some-text-to-an-image/
Share on other sites

This is what I made. Sure, it isn't the best of the best, but it works perfectly. You need to have the 'arial.ttf' font file in the same dir as the script for this to work.

 

<?php


function texttoimg($input_txt)
{
$handle = imagecreatetruecolor(600, 600);
$white  = imagecolorallocate($handle, 255, 255, 255);
$black  = imagecolorallocate($handle, 0, 0, 0);
imagefilledrectangle($handle, 0, 0, 599, 599, $white);
imagefttext($handle, 20, 0, 300, 300, $black, './arial.ttf', $input_txt);
header('Content-type: image/png');
imagepng($handle);
imagedestroy($handle);
}

texttoimg('Test');


?>

 

Chris

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.