What are the advantages and disadvantages of using PHP scripts to add watermarks to images compared to using image editing software with batch processing capabilities?
When adding watermarks to images, using PHP scripts can be advantageous as it allows for automation and can be integrated into existing web applications easily. However, using image editing software with batch processing capabilities may provide more control over the placement and appearance of the watermark.
<?php
// Function to add watermark to an image
function addWatermark($imagePath, $watermarkPath, $outputPath) {
$image = imagecreatefromjpeg($imagePath);
$watermark = imagecreatefrompng($watermarkPath);
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);
$destX = $imageWidth - $watermarkWidth - 10;
$destY = $imageHeight - $watermarkHeight - 10;
imagecopy($image, $watermark, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight);
imagejpeg($image, $outputPath);
imagedestroy($image);
imagedestroy($watermark);
}
// Usage
addWatermark('image.jpg', 'watermark.png', 'output.jpg');
?>
Related Questions
- What are some best practices for handling user input containing HTML in PHP forms to prevent security vulnerabilities?
- Why is the input name not being outputted in the PHP file as expected?
- What are the potential benefits of using a database instead of text files for storing and manipulating data in PHP?