简体   繁体   English

使用 PHP 按比例将水印添加到 JPG 或 PNG

[英]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: Output 图片: 在此处输入图像描述

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.为了将调整大小的水印的纵横比保持在背景图像大小的 5%,您需要知道宽度与高度的比率,并在生成调整大小的图像时使用它。 As per the comment by @CBroe, using a single $percent value for both will yield the wrong aspect ratio.根据@CBroe 的评论,对两者使用单个$percent值将产生错误的纵横比。 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.最后的header调用应设置为image/png ,然后使用imagepng将 output 合成图像发送到浏览器。


<?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 );
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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