i am trying to add a watermark to an image with opacity i have used the answer given here Watermarking an image with php it does roughly what i want but im not sure how to get it to where i want it.
what i want to do is add the image watermark to an image but add opacity to it, i have looked about to see how i would go about it but am unable to work out the code to make it work.
this is what im trying to do. take the watermark and add the image to watermark background as the watermarks background then merge it with the image with opacity. so that they can see the image behide the watermark slightly
code
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);
}
there were a few errors in this answer but i managed to correct them, it does what its ment to do but the opacity and position of watermark, can anyone help me out with this please.
this is a function that i have done but doesn't work correctly,
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;
}
this outputs the image to watermark but does not seam to actually watermark the image.