Home > Backend Development > PHP Tutorial > How to use PHP and UniApp to implement the watermark function of images

How to use PHP and UniApp to implement the watermark function of images

WBOY
Release: 2023-07-05 09:54:01
Original
1438 people have browsed it

How to use PHP and UniApp to implement the watermark function of pictures

Introduction:
In today's social media era, pictures have become one of the commonly used communication methods for people. In order to better protect their picture works, many people often add watermarks to pictures. This article will introduce how to use PHP and UniApp to implement the watermark function of images to make your images more personalized and secure.

1. PHP implements image watermark function

  1. Create a PHP file and name it watermark.php.

// Define watermark text
$text = 'Watermark';

// Define watermark font
$font = 'msyh .ttc'; // Microsoft Yahei font is used here, please ensure that the font file is available on the server

// Define the watermark font size
$fontsize = 40;

// Define the watermark text color
$color = imagecolorallocatealpha($image, 255, 255, 255, 50);

// Open the source image file
$sourceImage = imagecreatefromjpeg('source.jpg') ;

// Get the width and height of the source image
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

// Create a new Image, used to add watermark
$newImage = imagecreatetruecolor($sourceWidth, $sourceHeight);

// Copy the source image to a new image
imagecopy($newImage, $sourceImage, 0, 0, 0, 0, $sourceWidth, $sourceHeight);

// Add watermark text to the new image
imagettftext($newImage, $fontsize, 0, $sourceWidth * 0.5 - $fontsize 0.5, $sourceHeight * 0.5 $fontsize 0.5, $color, $font, $text);

// Output the image with watermark
header('Content-Type: image/ jpeg');
imagejpeg($newImage);

// Release image resources
imagedestroy($sourceImage);
imagedestroy($newImage);
?>

  1. Name the image to be watermarked as source.jpg and place it in the same directory as the watermark.php file.
  2. Visit watermark.php in your browser to see the watermarked image.

2. UniApp implements the image watermark function

  1. Create a new page in the pages directory of UniApp and name it Watermark.
  2. In the vue file of the Watermark page, add the following content:

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template