添加“水印”的图像与PHP

我有一个网站,用户可以上传图片…

一旦上传,我需要将图像(水印)添加到图像中。

我怎么能这样做?

重要的是水印在可见的angular落,例如我看到网站在飞行中产生水印,并且在主要图像的背景是“相同颜色”的地方放置标记,因此如果你知道我的意思,水印就伸出来了。

有人有一个很好的教程或文章? 或者知道在PHP中的任何function,我需要find水印的位置?

PHP手册中的一个很好的例子 :

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

水印图像的好例子,位于中心

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

使用这个function
水印图像的types必须是“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'); 

我发现了一个更好的解决scheme,通过.htaccess通过dinamically添加水印,你可以在这里find教程:

通过htaccess为图像添加水印

在上传自定义的.htaccess文件,watermark.php scrypt和你的watermark.png图片之后,文件夹及其子文件夹中的所有图像都会显示水印,但是您仍然会将原始文件保存在服务器中。

希望能帮助那些对我有帮助的人。

这可以使用图像操作库(如GD或ImageMagick)来完成 。 这里是一个教程,解释了一种使用GD的方法:

http://articles.sitepoint.com/article/watermark-images-php

ImageMagick对此很有效。 我以前做过。 尽pipe如此,整个业务仍然有点痛苦。 特别是如果你想花式混合模式等。

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