[英]Add Watermark to JPG or PNG proportionally using PHP
我现在遇到的问题是“水印”图像/文件的比例不正确。 高度比预期的要高一些。
正如您从下面的预览中看到的那样,“水印”字/图像被拉伸了。
<?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);
为了将调整大小的水印的纵横比保持在背景图像大小的 5%,您需要知道宽度与高度的比率,并在生成调整大小的图像时使用它。 根据@CBroe 的评论,对两者使用单个$percent
值将产生错误的纵横比。 水印的定位也需要新的水印尺寸能够计算出合适的图像中心
两个初始用途:
imagealphablending( $watermark, false );
imagesavealpha( $watermark, true );
不是必需的,可以删除。
最后的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.