簡體   English   中英

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

[英]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);

Output 圖片: 在此處輸入圖像描述

水印圖片: 在此處輸入圖像描述

為了將調整大小的水印的縱橫比保持在背景圖像大小的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM