我有一个用户可以上传图片的网站......
我需要在图片上传后添加我的徽标(水印)。
我该怎么办?
重要的是,水印位于可见的角落,例如,我已经看到了在飞行中生成水印的网站,并将标记放在主图像的背景为“相同颜色的任何地方” “所以如果你知道我的意思,那么水印就会突然出现。
有人有一个很好的教程或文章吗? 或者知道php中的任何函数我需要找到水印的位置?
答案 0 :(得分:49)
PHP手册中的good example:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
答案 1 :(得分:14)
使用此功能
水印图像的类型必须是" png"
function watermark_image($target, $wtrmrk_file, $newcopy) {
$watermark = imagecreatefrompng($wtrmrk_file);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$img = imagecreatefromjpeg($target);
$img_w = imagesx($img);
$img_h = imagesy($img);
$wtrmrk_w = imagesx($watermark);
$wtrmrk_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
imagejpeg($img, $newcopy, 100);
imagedestroy($img);
imagedestroy($watermark);
}
watermark_image('image_name.jpg','watermark.png', 'new_image_name.jpg');
答案 2 :(得分:12)
水印图像的良好示例,位于中心
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
答案 3 :(得分:3)
我找到了一个更好的解决方案,通过.htaccess在dinamically添加水印,你可以在这里找到教程:
Add watermark to images through htaccess
上传自定义.htaccess文件,watermark.php scrypt和watermark.png图片后,文件夹及其子文件夹中的所有图片都会显示水印,但是,您仍会将原始文件保留在服务器中
希望能帮助某人帮助我。
答案 4 :(得分:2)
这可以使用GD或ImageMagick等图像处理库来完成。这是一个教程,解释了使用GD的方法:
答案 5 :(得分:2)
ImageMagick效果很好。我以前做过。不过,整个业务有点痛苦。特别是如果你想要花哨的混合模式等。
答案 6 :(得分:2)
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpg');
$save_watermark_photo_address = 'watermark_photo.jpg';
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
// header('Content-type: image/png');
imagejpeg($im, $save_watermark_photo_address, 80);
imagedestroy($im);