Home > Backend Development > PHP Tutorial > Render Text and Shapes on Images in PHP

Render Text and Shapes on Images in PHP

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-03-05 10:17:09
Original
587 people have browsed it

This tutorial demonstrates how to create images from scratch using PHP's GD library. We'll cover drawing basic shapes, controlling line attributes, adding text, and creating simple designs. Here's a preview of the final result:

Render Text and Shapes on Images in PHP

Key Concepts:

  • Drawing basic shapes with PHP's GD functions.
  • Customizing line thickness, style, and fill colors.
  • Rendering text onto images using TrueType fonts.
  • Creating simple designs and graphical elements.

Drawing Basic Shapes

This section focuses on fundamental shapes. Line thickness, brushes, and styles will be covered later.

Drawing Lines

The imagettfbbox() function determines the bounding box of text rendered with TrueType fonts. This is useful for scaling text to fit within image dimensions. imagettftext() then draws the text onto the image.

Render Text and Shapes on Images in PHP

The following code snippet, inserted before the image save operation, adds concentric filled circles to the corners:

$edge_distance = 20;
$largest_size = 200;
$size_drop = 10;
$central_point = $largest_size/2 + $edge_distance;

for($i = 0; $i < 20; $i++){
    $ellipse_size = $largest_size - $size_drop*$i;
    $ellipse_color = $white;
    if($i%2 != 0){
        $ellipse_color = $blue;
    }
    imagefilledellipse($img, $central_point, $img_height, $ellipse_size, $ellipse_size, $ellipse_color);
    imagefilledellipse($img, $img_width - $central_point, 0, $ellipse_size, $ellipse_size, $ellipse_color);
}
Copy after login

Render Text and Shapes on Images in PHP

As demonstrated, simple mathematical calculations combined with PHP's GD functions allow for the creation of complex patterns.

Conclusion

This tutorial introduced several GD functions for drawing basic shapes in PHP. With some mathematical manipulation, these functions can be used to create more intricate shapes (e.g., regular polygons, rounded rectangles). PHP's GD library also provides robust text rendering capabilities, ensuring clean text integration into images. Share your creative text effects in the comments!

The above is the detailed content of Render Text and Shapes on Images in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template