[英]php watermark an image with opacity
我正在嘗試向具有不透明度的圖像添加水印,我已使用此處給出的答案使用php對圖像加水印,它大致可以實現我想要的功能,但是我不確定如何將其到達我想要的位置。
我想做的是將圖像水印添加到圖像中,但給圖像添加不透明度,我一直在尋找如何處理它,但無法制定代碼使其工作。
這就是我想做的。 拍攝水印並將圖像添加到水印背景作為水印背景,然后將其與具有不透明度的圖像合並。 這樣他們就可以看到隱藏在水印后面的圖像
function Watermark ($image,$output,$overlay,$opacity=20){
if (!file_exists($image)) {
die("Image does not exist.");
}
// Set offset from bottom-right corner
$w_offset = 0;
$h_offset = 100;
$extension = strtolower(substr($image, strrpos($image, ".") + 1));
// Load image from file
switch ($extension){
case 'jpg':
$background = imagecreatefromjpeg($image);
break;
case 'jpeg':
$background = imagecreatefromjpeg($image);
break;
case 'png':
$background = imagecreatefrompng($image);
break;
case 'gif':
$background = imagecreatefromgif($image);
break;
default:
die("Image is of unsupported type.");
}
// Find base image size
$swidth = imagesx($background);
$sheight = imagesy($background);
// Turn on alpha blending
imagealphablending($background, true);
// Create overlay image
//$overlay = imagecreatefrompng($overlay);
$photo = imagecreatefromjpeg($image);
$watermark = imagecreatefrompng($overlay);
// Get the size of overlay
$owidth = imagesx($watermark);
$oheight = imagesy($watermark);
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
imagejpeg($photo,$output);
// Overlay watermark
// Destroy the images
imagedestroy($background);
imagedestroy($watermark);
}
這個答案有一些錯誤,但是我設法糾正了它,它的工作是做什么的,但是水印的不透明度和位置,任何人都可以幫我解決這個問題。
這是我完成的功能,但是無法正常工作,
function _Watermark($input, $output, $watermark, $opacity=50){
$im = $this->imagecreatefrom($input);
$stamp = $this->imagecreatefrom($watermark);
// First we create our stamp image manually from GD
//$stamp = imagecreatetruecolor(100, 70);
//imagefilledrectangle($stamp, 0, 0, 99, 69, 0x0000FF);
//imagefilledrectangle($stamp, 9, 9, 90, 60, 0xFFFFFF);
//imagestring($stamp, 5, 20, 20, 'libGD', 0x0000FF);
//imagestring($stamp, 3, 20, 40, '(c) 2007-9', 0x0000FF);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$ix = imagesx($im);
$iy = imagesy($im);
$sx = imagesx($stamp);
$sy = imagesy($stamp);
list($iw, $ih, $type, $attr) = getimagesize($input);
list($sw, $sh, $type, $attr) = getimagesize($watermark);
//imagecopy($im, $stamp, $ix - $sx - $marge_right, $ix - $sy - $marge_bottom, 0, 0, $sx, $sy);
// copying relevant section from background to the cut resource
$cut = imagecreatetruecolor($sw, $sh);
echo ($ix - $sx - $marge_right)." x ".($iy - $sy - $marge_bottom)." | $sx x $sy<br/>";
imagecopy($cut, $im, 0, 0, $ix - $sx - $marge_right, $iy - $sy - $marge_bottom, $sx, $sy) ;
// copying relevant section from watermark to the cut resource
echo $sx." x ".$sy." | $sw x $sy<br/>";
imagecopy($cut, $stamp, 0, 0, $sx, $sy, $sw, $sh);
// insert cut resource to destination image
imagecopymerge($im, $cut, $ix - $sx - $marge_right, $iy - $sy - $marge_right, 0, 0, $sw, $sw, $opacity);
//imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), $opacity);
// Save the image to file and free memory
imagejpeg($im, $output);
imagedestroy($im);
imagedestroy($stamp);
return true;
}
private function imagecreatefrom($input){
$size = getimagesize($input);
if($size){
switch($size['mime']){
case 'image/jpeg':
$base = imagecreatefromjpeg($input);
imagealphablending( $base, false );
imagesavealpha( $base, true );
return $base;
break;
case 'image/png':
$base = imagecreatefrompng($input);
imagealphablending( $base, false );
imagesavealpha( $base, true );
return $base;
break;
case 'image/gif':
$base = imagecreatefromgif($input);
imagealphablending( $base, false );
imagesavealpha( $base, true );
return $base;
break;
case 'image/vnd.wap.wbmp':
$base = imagecreatefromwbmp($input);
imagealphablending( $base, false );
imagesavealpha( $base, true );
return $base;
break;
case '': default: return false; break;
}
}
return false;
}
這會將圖像輸出到水印,但不會接縫以對圖像實際加水印。
組合透明圖像使用PHP的GD庫
<?php
//define the width and height of our images
define("WIDTH", 200);
define("HEIGHT", 200);
$dest_image = imagecreatetruecolor(WIDTH, HEIGHT);
//make sure the transparency information is saved
imagesavealpha($dest_image, true);
//create a fully transparent background (127 means fully transparent)
$trans_background = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
//fill the image with a transparent background
imagefill($dest_image, 0, 0, $trans_background);
//take create image resources out of the 3 pngs we want to merge into destination image
$a = imagecreatefrompng('1.png');
$b = imagecreatefrompng('2.png');
$c = imagecreatefrompng('3.png');
//copy each png file on top of the destination (result) png
imagecopy($dest_image, $a, 0, 0, 0, 0, WIDTH, HEIGHT);
imagecopy($dest_image, $b, 0, 0, 0, 0, WIDTH, HEIGHT);
imagecopy($dest_image, $c, 0, 0, 0, 0, WIDTH, HEIGHT);
//send the appropriate headers and output the image in the browser
header('Content-Type: image/png');
imagepng($dest_image);
//destroy all the image resources to free up memory
imagedestroy($a);
imagedestroy($b);
imagedestroy($c);
imagedestroy($dest_image);
?>
新圖像看起來像透明背景徽標(.png格式)
使用透明的背景徽標(.png格式),該徽標會生成類似以下內容的圖像。 透明文字圖片
PHPWatermark庫支持設置不透明度的圖像水印。 例如 -
$watermark = new Watermark('/path/to/source.jpg');
$watermark->setOpacity(.4);
// Many other options to customize
$watermark->withImage('path/to/logo.png', 'path/to/output.jpg');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.