PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
PHP provides several functions to draw text on images through the GD library. The following example demonstrates how to create a new image and draw some text on it:
<?php // Create a new true color image $image = imagecreatetruecolor(400, 300); // Allocate colors $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); // Fill the image with white color imagefill($image, 0, 0, $white); // Define the text string $text = "Hello, world!"; // Define the font size. This value is the point size $font_size = 20; // Path to your font file. $font_path = 'path/to/your/font.ttf'; // Draw the text on image using TrueType fonts imagettftext($image, $font_size, 0, 50, 50, $black, $font_path, $text); // Output the image header('Content-type: image/png'); imagepng($image); // Free memory imagedestroy($image); ?>
In this example, imagecreatetruecolor()
is used to create a new true color image. imagecolorallocate()
is used to allocate colors for use in the image. imagefill()
is used to fill the image with white color. imagettftext()
is used to draw the text on the image. Finally, imagepng()
is used to output the image in PNG format.
Remember to call imagedestroy()
when you're done with an image resource to free up memory.
This will output an image of the text "Hello, world!" on a white background.
Make sure your PHP installation has the GD library and FreeType support enabled. You can check this by calling phpinfo()
and looking for the section on GD. If GD is not enabled or FreeType support is not available, you will need to install or enable it. This process depends on your server configuration and your PHP installation.
Also, you need to replace 'path/to/your/font.ttf'
with the actual path to your TrueType font file.
How to use GD library to add text to images in PHP: Use the GD library to add text to an image:
<?php $image = imagecreatefromjpeg('background.jpg'); $textColor = imagecolorallocate($image, 255, 0, 0); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 20, 0, 50, 100, $textColor, $font, 'Hello, PHP!'); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); ?>
Drawing dynamic text on images with PHP: Add dynamic text to images based on variables:
<?php $username = 'John Doe'; $image = imagecreatefrompng('template.png'); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 18, 0, 50, 100, $textColor, $font, 'Welcome, ' . $username); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
PHP imagettftext()
function for TrueType font text rendering:
Use imagettftext()
for rendering TrueType font text:
<?php $image = imagecreatefromjpeg('background.jpg'); $textColor = imagecolorallocate($image, 255, 0, 0); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 20, 0, 50, 100, $textColor, $font, 'Hello, PHP!'); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
Adding custom fonts to PHP GD for text drawing: Load and use a custom font for text drawing:
<?php $image = imagecreatefrompng('background.png'); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'custom-font.ttf'; // Replace with the path to a custom TTF font file imagettftext($image, 18, 0, 50, 100, $textColor, $font, 'Custom Font Text'); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
PHP GD library for drawing multiline text on images:
Draw multiline text on images using \n
:
<?php $image = imagecreatefromjpeg('background.jpg'); $textColor = imagecolorallocate($image, 255, 0, 0); $font = 'arial.ttf'; // Replace with the path to a TTF font file $multilineText = "Line 1\nLine 2\nLine 3"; imagettftext($image, 18, 0, 50, 100, $textColor, $font, $multilineText); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
Customizing text color and size in PHP image text drawing: Customize text color and size:
<?php $image = imagecreatefrompng('background.png'); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 24, 45, 50, 100, $textColor, $font, 'Custom Text'); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
Drawing rotated text on images with PHP GD: Draw rotated text on images:
<?php $image = imagecreatefromjpeg('background.jpg'); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 20, 45, 50, 100, $textColor, $font, 'Rotated Text'); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
PHP imagettfbbox()
for calculating text bounding box:
Use imagettfbbox()
to calculate the bounding box of text:
<?php $image = imagecreatetruecolor(400, 200); $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file $text = 'Bounding Box Test'; $bbox = imagettfbbox(20, 0, $font, $text); $x = 50; $y = 100; imagettftext($image, 20, 0, $x, $y, $textColor, $font, $text); // Draw bounding box imagerectangle($image, $x + $bbox[0], $y + $bbox[1], $x + $bbox[2], $y + $bbox[3], $textColor); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
Adding transparent text to images in PHP: Add transparent text to images:
<?php $image = imagecreatetruecolor(400, 200); $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); $textColor = imagecolorallocatealpha($image, 0, 0, 255, 50); // 50 is alpha (transparency) $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 20, 0, 50, 100, $textColor, $font, 'Transparent Text'); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
PHP GD text alignment options:
Align text using imagettftext()
and alignment calculations:
<?php $image = imagecreatefromjpeg('background.jpg'); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file $text = 'Centered Text'; $fontSize = 20; $bbox = imagettfbbox($fontSize, 0, $font, $text); $textWidth = $bbox[4] - $bbox[0]; $textHeight = $bbox[1] - $bbox[5]; $x = (imagesx($image) - $textWidth) / 2; $y = (imagesy($image) + $textHeight) / 2; imagettftext($image, $fontSize, 0, $x, $y, $textColor, $font, $text); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
Watermarking images with text using PHP: Add a watermark to images with text:
<?php $image = imagecreatefromjpeg('original.jpg'); $watermarkText = 'Watermark'; $textColor = imagecolorallocatealpha($image, 255, 255, 255, 50); $font = 'arial.ttf'; // Replace with the path to a TTF font file imagettftext($image, 20, 45, 50, 100, $textColor, $font, $watermarkText); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image);
Handling special characters and encoding in PHP text drawing: Handle special characters and encoding:
<?php $image = imagecreatetruecolor(400, 200); $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file $specialText = 'Special Characters: é ç ñ'; imagettftext($image, 18, 0, 50, 100, $textColor, $font, utf8_decode($specialText)); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
Dynamic text placement based on image dimensions in PHP: Dynamically place text based on image dimensions:
<?php $image = imagecreatetruecolor(400, 200); $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file $dynamicText = 'Dynamic Placement'; // Calculate x, y based on image dimensions $x = (imagesx($image) - strlen($dynamicText) * 10) / 2; $y = imagesy($image) / 2; imagettftext($image, 20, 0, $x, $y, $textColor, $font, $dynamicText); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);
PHP imagestring()
for simple text drawing on images:
Use imagestring()
for simple text drawing:
<?php $image = imagecreate(400, 200); $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); $textColor = imagecolorallocate($image, 0, 0, 255); $font = 'arial.ttf'; // Replace with the path to a TTF font file $simpleText = 'Simple Text'; // Draw simple text imagestring($image, 5, 50, 100, $simpleText, $textColor); header('Content-Type: image/png'); imagepng($image); imagedestroy($image);