Current Location: Home> Latest Articles> Add text watermark after flipping the image using the imageflip function

Add text watermark after flipping the image using the imageflip function

gitbox 2025-05-27

PHP provides a rich variety of image processing functions, imageflip is one of them. It helps us flip images easily, and common uses include horizontal flip and vertical flip. If you are not familiar with this function yet, this article will show you how to use the imageflip function to flip an image and add a text watermark to the flipped image.

1. Environmental Requirements

Before using PHP image processing, make sure your server has GD library enabled. The GD library is a powerful image processing tool provided by PHP, which can be used to create and modify images.

You can check whether the GD library is installed by following the following code:

 <?php
if (function_exists('gd_info')) {
    echo 'GDThe library is installed!';
} else {
    echo 'GDThe library is not installed!';
}
?>

If the GD library is installed, you can use image processing related functions next.

2. Use imageflip function to flip the image

The imageflip function is used to flip an image. The basic syntax of this function is as follows:

 int imageflip ( resource $image , int $mode )
  • $image is an image resource loaded through imagecreatefromjpeg() , imagecreatefrommpng() or other functions.

  • $mode is the way to flip. Commonly used modes are:

    • IMG_FLIP_HORIZONTAL : Flip horizontally.

    • IMG_FLIP_VERTICAL : Flip vertically.

    • IMG_FLIP_BOTH : Flip horizontally and vertically at the same time.

3. Code example: Flip the image and add watermark

Suppose we have an image that we want to flip horizontally and add a text watermark to the flipped image. The code is as follows:

 <?php
// Loading the image
$image = imagecreatefromjpeg('https://gitbox.net/images/example.jpg');

// Determine whether the image is loading successfully
if (!$image) {
    die('Image loading failed!');
}

// Flip the image horizontally
imageflip($image, IMG_FLIP_HORIZONTAL);

// Add watermark
$watermark_text = "Gitbox Watermark";  // Watermark text
$font_path = 'path/to/your/font.ttf';  // Font file path
$font_size = 20;  // Font size
$text_color = imagecolorallocate($image, 255, 255, 255);  // White font
$angle = 0;  // Text rotation angle
$x_position = 10;  // Watermark text的 X coordinate
$y_position = 30;  // Watermark text的 Y coordinate

// 在图像上Add watermark文本
imagettftext($image, $font_size, $angle, $x_position, $y_position, $text_color, $font_path, $watermark_text);

// Set content type header,Output image
header('Content-Type: image/jpeg');
imagejpeg($image);

// Free memory
imagedestroy($image);
?>

4. Detailed code explanation

  • Loading an image : First use imagecreatefromjpeg() to load a JPEG image. You can choose different image formats as you want (e.g. imagecreatefrommpng() or imagecreatefromgif() ).

  • Flip the image : Use the imageflip() function to flip, and the IMG_FLIP_HORIZONTAL parameter represents the horizontal flipped image.

  • Add a watermark :

    • imagettftext() is used to add text watermarks to images. You need to provide information such as font path, font size, rotation angle and color.

    • The color of the watermark text is created by imagecolorallocate() , which we set to white (255, 255, 255) .

    • You can adjust $x_position and $y_position to control the position of the watermark.

5. Output image

Set the appropriate HTTP response header through header() and output the image using imagejpeg() . When the PHP file is opened in the browser, the image will be displayed with a flip effect and a watermark.

6. Things to note

  • Font File : Make sure to provide the correct font file path. You can use the fonts that come with your system, or download suitable font files from the Internet.

  • Performance : If the image is large, it may consume a certain amount of memory and computing resources when processing the image. It is recommended to optimize the code according to the actual situation.

Hope this article helps you! Through the imageflip function, we can easily flip the image and add text watermarks to the image through the imagettftext() function to achieve simple and effective image processing functions. If you have any questions, please leave a message or ask a question in the community!