Add 'Watermark' to images with php
A good example in the PHP manual:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
add watermark image to uploaded image
I tested this using the code below and this worked ok. Obviously I have used paths relevant to my test system but hopefully it should help.
One of the most important and often forgotten things when uploading and dealing with uploaded files is the enctype
of the form - so I included my test form as an example.
If you want to save the image as well as display it with the watermark use the imagepng function twice, once with a filename and the other without.
<form method='post' action='/test/so/wtrmarkimg.php' enctype='multipart/form-data'>
<h1>Image uploader - Watermark</h1>
<input type='file' name='image' />
<input type='submit' value='Submit' />
</form>
<?php
#$path = "../large/";
$path='c:/temp/';/* output path for images generated */
$watermarksrc=realpath( 'c:/wwwroot/images/watermark.png' );
if( isset( $_FILES['image'] ) ){
$img_tmpname=$_FILES['image']['tmp_name'];
$num = substr( md5( mt_rand( 1,9999999999 ) ),0,9);
$new_name = $path.$num.".jpg";
$image = $num.".jpg";
if( move_uploaded_file( $img_tmpname, $new_name ) ){
$image = imagecreatefromjpeg( $new_name );
$logoImage = imagecreatefrompng( $watermarksrc );
imagealphablending( $logoImage, true );
$imageWidth=imagesx($image);
$imageHeight=imagesy($image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);
imagecopy(
$image,
$logoImage,
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
0, 0,
$logoWidth, $logoHeight );
// Set type of image and send the output
header("Content-type: image/png");
imagepng( $image );/*display image with watermark */
@imagepng( $image, $new_name );/* save image with watermark */
// Release memory
imagedestroy( $image );
imagedestroy( $imageLogo );
}
} else {
echo "ERROR";
print_r($_FILES);
}
?>
add watermark to the image and save it as image not php file
Instead of this:
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Write:
$filename = '/foo/bar.png';
imagepng($im, $filename);
imagedestroy($im);
How do I add the watermark image with different positions in PHP Laravel?
Hey Dude try by replace this code with imagecopyresized
function.
I hope it can work for you.
Thanks.
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// top-left
imagecopy($im, $stamp, -45, -5, 0, 0, imagesx($stamp), imagesy($stamp));
// top-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, -5, 0, 0, imagesx($stamp), imagesy($stamp));
// bottom-left
imagecopy($im, $stamp, -45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));
// bottom-right
imagecopy($im, $stamp, imagesx($im) - $sx + 45, imagesy($im) - $sy + 5, 0, 0, imagesx($stamp), imagesy($stamp));
// center
imagecopy($im, $stamp, (imagesx($im) - $sx)/2, (imagesy($im) - $sy)/2, 0, 0, imagesx($stamp), imagesy($stamp));
Watermark an Image using Text in PHP
One problem I can see straight away is that the $imagetobewatermark
variable starts off as a string, then becomes a new blank image object (not an existing image), and when you subsequently create the mark
image object, it's not going to work because $imagetobewatermark
is no longer a string.
Try:
$imagetobewatermark=imagecreatefrompng("images/muggu.png");
$watermarktext="Muggu";
$font="../font/century gothic.ttf";
$fontsize="15";
$white = imagecolorallocate($imagetobewatermark, 255, 255, 255);
imagettftext($imagetobewatermark, $fontsize, 0, 20, 10, $white, $font, $watermarktext);
header("Content-type:image/png");
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
EDIT:
I failed to notice a typo in your text variable $wartermarktext
, which should be $watermarktext
.
Correct this and it should work.
How to add watermark to an image at the bottom right in PHP?
You are placing the watermark image in the wrong location. It needs to be relative to the width and height of the source image, as well as the dimensions of the watermark image. This solves your problem:
imagecopy($img, $watermark, $img_w - $wtrmrk_w, $img_h - $wtrmrk_h, 0, 0, $wtrmrk_w, $wtrmrk_h);
Related Topics
Laravel 5 Failed Opening Required Bootstrap/../Vendor/Autoload.PHP
Remove Warning Messages in PHP
How to Get First 5 Characters from String
Redirecting to Previous Page After Login - PHP
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
Pdo Prepared Statements With Wildcards
Sha1 VS Md5 VS Sha256: Which to Use For a PHP Login
Resetting Array Pointer in Pdo Results
I Cannot Get My Login Form to Connect Interact Properly With MySQL Database
Insert - Number of Bind Variables Doesn't Match Number of Fields in Prepared Statement
Laravel Certificate Verification Errors When Sending Tls Email
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'
How to Change Envelope from Address Using PHP Mail