简体   繁体   中英

Add Watermark to JPG or PNG proportionally using PHP

The problem I encountered right now is that the "watermark" image/file isn't proportionally correct. The height is a bit taller than expected.

As you can see from the preview below the "watermark" word/image was stretched.

<?php
//Source folder where all images are placed
$source="source";

//Destination folder where all images with watermark will be copied
$destination="output";

$watermark =imagecreatefrompng("watermark-sample.png");

//keeps the transparency of the picture
imagealphablending( $watermark, false );
imagesavealpha( $watermark, true );

//keeps the transparency of the picture
imagealphablending( $watermark, false );
imagesavealpha( $watermark, true );

// assign image file name
$image = $source.'/large-pic.jpg';

$im = imagecreatefromjpeg($image);

// get the height/width of the stamp image
$sx = imagesx($watermark);
$sy = imagesy($watermark);
$sximg = imagesx($im);

//percentage of the size(5%)
$percent = $sximg * 0.12;

//position the stamp to the center
$posx = round(imagesx($im) / 2) - round($sx / 2);
$posy = round(imagesy($im) / 2) - round($sy / 2);

//Create the final resized watermark stamp
$dest_image = imagecreatetruecolor($percent, $percent);

//keeps the transparency of the picture
imagealphablending( $dest_image, false );
imagesavealpha( $dest_image, true );

//resizes the stamp
imagecopyresampled($dest_image, $watermark, 0, 0, 0, 0, $percent, $percent, $sx, $sy);

// Copy the resized stamp image onto the photo
imagecopy($im, $dest_image, round($posx), round($posy), 0, 0, $percent, $percent);

// Output and free memory
header('Content-type: image/jpg');
imagepng($im);
imagedestroy($im);

Output Image: 在此处输入图像描述

Watermark Image: 在此处输入图像描述

In order to maintain the aspect ratio of a resized watermark at 5% of the background image size you need to know the ratio of width to height and use that when generating the resized image. As per the comment by @CBroe, using a single $percent value for both will yield the wrong aspect ratio. The positioning of the watermark also requires the new watermark sizes to be able to calculate the appropriate image centre

The two initial uses of:

imagealphablending( $watermark, false );
imagesavealpha( $watermark, true );

were not required and can be removed.

The header call at the end should be set to image/png as you then use imagepng to output the composite image to the browser.


<?php

# Source folder where all images are placed
$source=__DIR__ . '/source';

# As the $destination folder/variable was unused it is gone.


$watermark = imagecreatefrompng( $source . '/watermark-sample.png' );
$im = imagecreatefromjpeg( $source . '/large-pic.jpg' );



# get the height/width of the watermark image
$sx = imagesx( $watermark );
$sy = imagesy( $watermark );
$sximg = imagesx( $im );


# very important - aspect ratio of the watermark!!!
$ratio=$sx / $sy;

# percentage of the size( 5% )
$percent = 5 / 100; # 5% as per question



# Determine new sizes for the watermark.
# The width will be 5% of the background width
# but the height will be the respective proportion
# of the background height based upon the aspect
# ratio of the watermark!

$width = $sximg * $percent;
$height = $width / $ratio;



# Determine the position the watermark on the background image and
# use the new sizes of watermark as part of the centre calculations!
$posx = round( imagesx( $im ) / 2 ) - round( $width / 2 );
$posy = round( imagesy( $im ) / 2 ) - round( $height / 2 );





# Create the final resized watermark stamp
$dest_image = imagecreatetruecolor( $width, $height );

# keeps the transparency of the picture
imagealphablending( $dest_image, false );
imagesavealpha( $dest_image, true );


# resizes the stamp
imagecopyresampled( $dest_image, $watermark, 0, 0, 0, 0, $width, $height, $sx, $sy );

# Copy the resized stamp image onto the photo
imagecopy( $im, $dest_image, round( $posx ), round( $posy ), 0, 0, $width, $height );



# Output and free memory - use correct header however!
header('Content-type: image/png');
imagepng( $im );

imagedestroy( $im );
imagedestroy( $watermark );
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM