相对于父图像调整水印大小并保持抗锯齿
Resize watermark relative to parent image and keep antialiasing
我看过其他答案,但 none 解决了这个问题,我有这个代码
$stamp = imagecreatefrompng('w.png');
$im = imagecreatefromjpeg('image.jpg');
$stw = imagesx($im)/4;
$marge_bottom = 10;
$marge_right = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopyresized($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
header("Content-Type: image/jpeg");
imagejpeg($im,NULL,100);
这有效,但水印(图章)在调整大小后出现锯齿状
水印应该是这样的:
编辑:
这是针对遇到此问题的任何人的解决方案,将 imagecopyresized()
函数替换为 imagecopyresampled()
imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
我不知道你这里的代码的所有来龙去脉。但是,我确实知道任何按比例缩小的图像都必须保持其纵横比,以便在较小的尺寸下显示相同。如果在缩小时使用绝对值来定义图像的宽度和高度,则图像中出现锯齿状边缘的可能性会更高,因为图像在较小尺寸时没有保持其纵横比。因此,不要定义绝对值,而应使用动态值。因此,例如,不要使用 10px 或转换为百分比 (%)。
尝试 imagecopyresampled
而不是 imagecopyresized
:
imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
来自手册:
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.
我看过其他答案,但 none 解决了这个问题,我有这个代码
$stamp = imagecreatefrompng('w.png');
$im = imagecreatefromjpeg('image.jpg');
$stw = imagesx($im)/4;
$marge_bottom = 10;
$marge_right = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopyresized($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
header("Content-Type: image/jpeg");
imagejpeg($im,NULL,100);
这有效,但水印(图章)在调整大小后出现锯齿状
水印应该是这样的:
编辑:
这是针对遇到此问题的任何人的解决方案,将 imagecopyresized()
函数替换为 imagecopyresampled()
imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
我不知道你这里的代码的所有来龙去脉。但是,我确实知道任何按比例缩小的图像都必须保持其纵横比,以便在较小的尺寸下显示相同。如果在缩小时使用绝对值来定义图像的宽度和高度,则图像中出现锯齿状边缘的可能性会更高,因为图像在较小尺寸时没有保持其纵横比。因此,不要定义绝对值,而应使用动态值。因此,例如,不要使用 10px 或转换为百分比 (%)。
尝试 imagecopyresampled
而不是 imagecopyresized
:
imagecopyresampled($im, $stamp, $marge_right, $marge_bottom, 0, 0, $stw, $stw, imagesx($stamp), imagesy($stamp));
来自手册:
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.