2016-12-07 162 views
2

我在使用PHP的圖像上添加透明徽標作爲水印。但是,結果是徽標質量差(其下的圖像質量很高,所以它只是水印)。這是我使用的代碼(其對最後3行):將徽標添加爲水印,水印質量差

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); 
+0

你真正的問題是什麼? – Blueblazer172

+0

爲什麼使用imagecopyresized的水印質量很差 –

回答

0

imagecopyresized將複製和規模和形象。這使用一個相當原始的算法,往往會產生更多的像素化結果。

一個更好的質量一個簡單的例子是:

<?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); 
?> 

你應該在這個職位從1-100看看圖像的here

0

使用質量。

imagejpeg($image, $new_image_name, 99);