[英]PHP watermark proportional resizing
:)我實際上是在制作在線相冊,並且要完成它,我需要創建一個水印腳本。 但這絕對行不通! 我真的不明白為什么。 我需要按比例調整水印的大小; 我已經完成了所有比率的計算,一切似乎都很清楚,但是沒有任何效果,請幫幫我!
這是代碼:
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('531a1251532b0.jpg');
// get the height/width of the stamp image
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$syimg = imagesy($im);
//percentage of the size(4%)
$percent = 4/$syimg;
$sx2 = $sx * $percent;
$sy2 = $sy * $percent;
$posx = (imagesx($im) / 2) - $sx2;
$posy = (imagesy($im) / 2) - $sy2;
//checking (everything is ok!)
//echo "per: ". $percent;
//echo "<br>\n sx: ". $sx2;
//echo "<br>\n posx: ". $posx;
//echo "<br>\n sy: ". $sy2;
//echo "<br>\n posy: ". $posy;
//echo "<br>\n syimg : ". $syimg;
//echo "<br>\n sximg : ". imagesx($im);
// Copy the stamp image onto the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, round($posx), round($posy), 0, 0, round($sx2), round($sy2));
// Output and free memory
header('Content-type: image/jpg');
imagepng($im);
imagedestroy($im);
?>
感謝您日后的回覆!
感謝Ryan Vincent和simON的幫助! :)我終於(經過許多小時和許多頭痛)找到了解決方案,正如您所看到的,我發現我的解決方案更加簡單:
<?php
if(empty($_GET['i'])){
//displays a error image if GET['i'] is empty
$im = imagecreatefrompng("scr/m/e.png");
//keeps the transparency of the picture
imagealphablending( $im, false );
imagesavealpha( $im, true );
// Output and free memory
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}elseif(!file_exists('img/'.$_GET['i'])){
//displays an error image if the file doesn't exists in the img folder
$im = imagecreatefrompng("scr/m/e.png");
//keeps the transparency of the picture
imagealphablending( $im, false );
imagesavealpha( $im, true );
// Output and free memory
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}else{
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('scr/m/w.png');
$get = $_GET['i'];
$im = imagecreatefromjpeg('img/'.$get);
// get the height/width of the stamp image
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$sximg = imagesx($im);
//percentage of the size(5%)
$percent = $sximg * 0.15;
//positionnig the stamp to the center
$posx = round(imagesx($im) / 2) - round($percent / 2);
$posy = round(imagesy($im) / 2) - round($percent / 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, $stamp, 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);
}
?>
再次感謝您的答復。 我希望我的腳本能在與我相同的情況下對所有人有所幫助! :)晚安(我是法國人,這是夜晚:3)!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.