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
// 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);
?>
2. UniApp implements the image watermark function
<image src="../../static/source.jpg" mode="aspectFit" @tap="addWatermark" />
Conclusion:
By using PHP and UniApp, we can easily implement the watermark function of images. PHP can process images on the server side, while UniApp can add watermarks on the mobile side. In this way, we can not only perform watermark processing through PHP on the computer, but also perform watermark operations on the mobile phone through UniApp, which is convenient and practical. Hope this article will be helpful to you.
The above is the detailed content of How to use PHP and UniApp to implement the watermark function of images. For more information, please follow other related articles on the PHP Chinese website!