gpt4 book ai didi

php - 如何使用 PHP 调整图像上的水印大小?

转载 作者:行者123 更新时间:2023-12-02 04:15:10 24 4
gpt4 key购买 nike

嗨,我正在尝试调整上传图像上的水印 png 大小,但就是不明白,我做错了什么。

这是我的代码:

// BEGIN WATERMARK

$watermark = imagecreatefrompng ('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$_Dim[x] = imageSX($destImage);
$_Dim[y] = imageSY($destImage);
$logo_Dim[x] = imageSX($watermark);
$logo_Dim[y] = imageSY($watermark);
$x = $_Dim[x] - $logo_Dim[x];
$y = $_Dim[y] - $logo_Dim[y];
imagecopy ($destImage, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height);
imagedestroy ($watermark);

// END WATERMARK

最佳答案

您可以使用此功能来调整图章的大小。不要使用imagecopyresized,因为它会损坏您的图像并且您的质量会降低。更好的是imagecopyresampledimagesavealphaimagealphablending 为您的图章提供透明背景

$image = imagecreatefromstring(file_get_contents('The_img.jpg'));
$stamp = imagecreatefrompng('the_watermark.png');

$stamp_new = imagecreatetruecolor(100,50);
imagealphablending($stamp_new, false);
imagesavealpha($stamp_new, true);
imagecopyresampled($stamp_new, $stamp, 0, 0, 0, 0, 100, 50, imagesx($stamp),imagesy($stamp));

$margin = ['right' => 20, 'bottom' => 20]; // Смещение от края
imagecopy($image, $stamp_new,
imagesx($image) - imagesx($stamp_new) - $margin['right'],
imagesy($image) - imagesy($stamp_new) - $margin['bottom'],
0, 0, imagesx($stamp_new), imagesy($stamp_new));
$imageName = 'newimage.jpg';
$dirName = 'test_folder';
if(!file_exists($dirName)){
mkdir($dirName, 0755, true);
imagepng($image, $dirName.'/'.$imageName);
} else {
imagepng($image, $dirName.'/'.$imageName);
}

ImageDestroy($image);
ImageDestroy($stamp);

关于php - 如何使用 PHP 调整图像上的水印大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com