2015-12-09 91 views
2

嗨,我想調整上傳的圖像上的水印PNG,但只是無法弄清楚,我做錯了什麼。如何使用PHP調整圖像大小的水印?

這裏是我的代碼:

// 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 

回答

2

用途:imagecopyresizedimagecopyresampled

$watermark = imagecreatefrompng ('watermark.png'); 
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark); 

// this is an example to resized your watermark to 0.5% from their original size. 
// You can change this with your specific new sizes. 
$percent = 0.5; 
$newwidth = $watermark_width * $percent; 
$newheight = $watermark_height * $percent; 

// create a new image with the new dimension. 
$new_watermark = imagecreatetruecolor($newwidth, $newheight); 

// Retain image transparency for your watermark, if any. 
imagealphablending($new_watermark, false); 
imagesavealpha($new_watermark, true); 

// copy $watermark, and resized, into $new_watermark 
// change to `imagecopyresampled` for better quality 
imagecopyresized($new_watermark, $watermark, 0, 0, 0, 0, $newwidth, $newheight, $watermark_width, $watermark_height); 

$_Dim[x] = imageSX($destImage); 
$_Dim[y] = imageSY($destImage); 
$logo_Dim[x] = imageSX($new_watermark); 
$logo_Dim[y] = imageSY($new_watermark); 
$x = $_Dim[x] - $logo_Dim[x]; 
$y = $_Dim[y] - $logo_Dim[y]; 
imagecopy ($destImage, $new_watermark, $x, $y, 0, 0, $newwidth, $newheight); 
imagedestroy ($new_watermark); 

注意:我假設你有另一個imageSX & imageSY功能,由於內置在功能上都是小寫:imagesx & imagesy

編輯1:php中的函數名不區分大小寫,但它是調用函數的良好實踐,因爲它們出現在聲明中。

編輯2:添加更多代碼以保留透明度。

+0

在PHP中的一切都是不區分大小寫。悲傷但真實。 – miken32

+0

它就像一個魅力。你救了我的傍晚,謝謝你。 – AlesTom

+1

@ miken32至少它是一致的:-) –

1

您可以使用此功能調整郵票大小。請勿使用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);