Home > Article > Backend Development > How to use PHP to implement the image watermark function of CMS system
How to use PHP to implement the image watermark function of the CMS system
In modern CMS systems, the image watermark function is a very common requirement. It can be used to protect the copyright of pictures and also add personalized logos to pictures. This article will introduce how to use PHP to write code to implement the image watermark function of the CMS system.
<?php if (extension_loaded('gd') && function_exists('gd_info')) { echo "GD library is installed on your server"; } else { echo "GD library is not installed on your server"; } ?>
If the output is "GD library is installed on your server", it means that the GD library has been installed.
imagecreatefromjpeg()
, imagecreatefrompng()
or imagecreatefromgif()
function to open an image File, returns an image identifier. imagecreatefrompng()
function to open a watermark image and return a watermark image identifier. imagecopy()
function to merge the watermark image onto the original image. imagejpeg()
, imagepng()
or imagegif()
function to output the merged image to the browser or Save to file. <?php function addWatermark($imagePath, $watermarkPath, $outputPath) { // 打开原始图片 $image = imagecreatefromjpeg($imagePath); // 打开水印图片 $watermark = imagecreatefrompng($watermarkPath); // 获得原始图片和水印图片的宽高 $imageWidth = imagesx($image); $imageHeight = imagesy($image); $watermarkWidth = imagesx($watermark); $watermarkHeight = imagesy($watermark); // 计算水印位置 $x = $imageWidth - $watermarkWidth - 10; $y = $imageHeight - $watermarkHeight - 10; // 合并图片 imagecopy($image, $watermark, $x, $y, 0, 0, $watermarkWidth, $watermarkHeight); // 输出图片 imagejpeg($image, $outputPath); // 释放内存 imagedestroy($image); imagedestroy($watermark); } // 使用示例 $imagePath = 'path/to/original/image.jpg'; $watermarkPath = 'path/to/watermark.png'; $outputPath = 'path/to/output/image.jpg'; addWatermark($imagePath, $watermarkPath, $outputPath); ?>
In the above code, we define a function named addWatermark()
, which accepts three parameters: original image path, watermark image path and output Image path. Inside the function, we first open the original image and the watermark image, then calculate the position of the watermark, and finally merge the watermark onto the original image and output it to the specified path.
addWatermark()
function during the upload process of images that need to be added with watermarks. You can add watermarks to articles, photo albums, etc. according to the specific needs of the system. At the same time, the style and position of the watermark can also be customized according to the system settings. Summary
Through the above example code, we can see that using PHP to implement the image watermark function of the CMS system is not complicated. With the functions and methods provided by the GD library, we can easily complete the synthesis and output of image watermarks. Of course, in addition to the watermark function, it can be further expanded, such as supporting text watermarks, batch adding watermarks and other functions.
I hope this article will help you understand how to use PHP to implement the image watermark function of the CMS system!
The above is the detailed content of How to use PHP to implement the image watermark function of CMS system. For more information, please follow other related articles on the PHP Chinese website!