首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加logo作为水印,水印质量差

添加logo作为水印,水印质量差
EN

Stack Overflow用户
提问于 2016-12-07 18:54:44
回答 2查看 326关注 0票数 2

我添加一个透明的标志作为水印的图像使用PHP。然而,在结果中,徽标的质量很差(它下面的图像质量很高,所以它只是一个水印)。这是我使用的代码(大约是最后3行):

代码语言:javascript
复制
header("Content-Type: image/png");

$photo = imagecreatefromjpeg('photos/'.$photo['image']);
$height = imagesx($photo);
$width = imagesx($photo);
if ($width > $_POST['width']) {
    $r = $width / $_POST['width'];

    $newwidth = $width / $r;
    $newheight = $height / $r;
}
$image = imagecreatetruecolor($width, $height);

$image2 = imagecopyresampled($image, $photo, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

$position = explode(" ", $_POST['background']);

$image3 = imagecrop($image, [
    'x' => str_replace(array('-', 'px'), array('', ''), $position[0]),
    'y' => str_replace(array('-', 'px'), array('', ''), $position[1]),
    'width' => $_POST['width'],
    'height' => $_POST['height']
]);
$stamp = imagecreatefrompng('img/logo.png');
imagecopyresized($image3, $stamp, 0, 0, 0, 0, 147, 50, imagesx($stamp), imagesy($stamp));
imagepng($image3, "created/".time().".png", 9);
EN

回答 2

Stack Overflow用户

发布于 2016-12-07 19:03:09

imagecopyresized将进行复制、缩放和图像处理。这使用了一种相当原始的算法,往往会产生更多像素化的结果。

为了获得更好的质量,一个简单的例子是:

代码语言:javascript
复制
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

你应该看看这篇文章here

票数 0
EN

Stack Overflow用户

发布于 2017-01-05 20:37:02

使用1-100之间的图像质量。

代码语言:javascript
复制
imagejpeg($image, $new_image_name, 99);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41015567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档