如何给图片加水印?

发布于 2024-11-01 00:43:49 字数 1052 浏览 4 评论 0原文

如何在不影响源图像的情况下在图像上添加水印。

$SourceFile = '/uploadedimage/gallery/thumble/';
$DestinationFile = '/uploadedimage/gallery/thumble/image-watermark.jpg';
$WaterMarkText = 'Copyright appsbee.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);



function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'arial.ttf';
   $font_size = 10;
   imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image);
   imagedestroy($image_p);
};

如果我这样做,缩略图文件夹中存在的图像就会受到影响。

How to put water mark on image without affecting the source image.

$SourceFile = '/uploadedimage/gallery/thumble/';
$DestinationFile = '/uploadedimage/gallery/thumble/image-watermark.jpg';
$WaterMarkText = 'Copyright appsbee.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);



function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
   list($width, $height) = getimagesize($SourceFile);
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($SourceFile);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
   $black = imagecolorallocate($image_p, 0, 0, 0);
   $font = 'arial.ttf';
   $font_size = 10;
   imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
   if ($DestinationFile<>'') {
      imagejpeg ($image_p, $DestinationFile, 100);
   } else {
      header('Content-Type: image/jpeg');
      imagejpeg($image_p, null, 100);
   };
   imagedestroy($image);
   imagedestroy($image_p);
};

If I do this the image which are present on thumble folder are effected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

此刻的回忆 2024-11-08 00:43:49

使用这个类,在添加水印之前只需复制原始图像即可。

<?php
class Watermark{
    # given two images, return a blended watermarked image
    # given two images, return a blended watermarked image
    function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
        $alpha_level /= 100; # convert 0-100 (%) alpha to decimal
        # calculate our images dimensions
        $main_img_obj_w = imagesx( $main_img_obj );
        $main_img_obj_h = imagesy( $main_img_obj );
        $watermark_img_obj_w = imagesx( $watermark_img_obj );
        $watermark_img_obj_h = imagesy( $watermark_img_obj );
        # determine center position coordinates
        $main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) );
        $main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
        $main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) );
        $main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) );
        # create new image to hold merged changes
        $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );
        # walk through main image
        for( $y = 0; $y < $main_img_obj_h; $y++ ) {
            for( $x = 0; $x < $main_img_obj_w; $x++ ) {
                $return_color     = NULL;

                # determine the correct pixel location within our watermark
                $watermark_x      = $x - $main_img_obj_min_x;
                $watermark_y      = $y - $main_img_obj_min_y;

                # fetch color information for both of our images
                $main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );

                # if our watermark has a non-transparent value at this pixel intersection
                # and we're still within the bounds of the watermark image
                if (  $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
                $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
                    $watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );

                    # using image alpha, and user specified alpha, calculate average
                    $watermark_alpha  = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );
                    $watermark_alpha  = $watermark_alpha * $alpha_level;

                    # calculate the color 'average' between the two - taking into account the specified alpha level
                    $avg_red          = $this->_get_ave_color( $main_rgb['red'],            $watermark_rbg['red'],        $watermark_alpha );
                    $avg_green  = $this->_get_ave_color( $main_rgb['green'],      $watermark_rbg['green'],      $watermark_alpha );
                    $avg_blue         = $this->_get_ave_color( $main_rgb['blue'],      $watermark_rbg['blue'],       $watermark_alpha );

                    # calculate a color index value using the average RGB values we've determined
                    $return_color     = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );

                    # if we're not dealing with an average color here, then let's just copy over the main color
                } else {
                    $return_color     = imagecolorat( $main_img_obj, $x, $y );

                } # END if watermark

                # draw the appropriate color onto the return image
                imagesetpixel( $return_img, $x, $y, $return_color );

            } # END for each X pixel
        } # END for each Y pixel
        # return the resulting, watermarked image for display
        return $return_img;
    } # END create_watermark()

    # average two colors given an alpha
    function _get_ave_color( $color_a, $color_b, $alpha_level ) {
        return round( ( ( $color_a * ( 1 - $alpha_level ) ) + ( $color_b  * $alpha_level ) ) );
    } # END _get_ave_color()

    # return closest pallette-color match for RGB values
    function _get_image_color($im, $r, $g, $b) {
        $c=imagecolorexact($im, $r, $g, $b);
        if ($c!=-1) return $c;
        $c=imagecolorallocate($im, $r, $g, $b);
        if ($c!=-1) return $c;
        return imagecolorclosest($im, $r, $g, $b);
    } # EBD _get_image_color()
} # END watermark API
?>

这就是如何使用它

$watermark = new Watermark();
        $actual_image = imagecreatefromjpeg("images/$image_file");
        $watermark_image = imagecreatefrompng("images/water_mark.png");
        //create water marked image with 66% transparency
        $return_img_obj = $watermark->create_watermark( $actual_image, $watermark_image, 66 );

        //now save the updated image
        imagejpeg($return_img_obj,"images/$image_file1");

        //clear the memory
        imagedestroy($actual_image);
        imagedestroy($watermark_image);
        imagedestroy($return_img_obj);

Use this class, before putting water mark just make the copy of original image.

<?php
class Watermark{
    # given two images, return a blended watermarked image
    # given two images, return a blended watermarked image
    function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
        $alpha_level /= 100; # convert 0-100 (%) alpha to decimal
        # calculate our images dimensions
        $main_img_obj_w = imagesx( $main_img_obj );
        $main_img_obj_h = imagesy( $main_img_obj );
        $watermark_img_obj_w = imagesx( $watermark_img_obj );
        $watermark_img_obj_h = imagesy( $watermark_img_obj );
        # determine center position coordinates
        $main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) );
        $main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
        $main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) );
        $main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) );
        # create new image to hold merged changes
        $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );
        # walk through main image
        for( $y = 0; $y < $main_img_obj_h; $y++ ) {
            for( $x = 0; $x < $main_img_obj_w; $x++ ) {
                $return_color     = NULL;

                # determine the correct pixel location within our watermark
                $watermark_x      = $x - $main_img_obj_min_x;
                $watermark_y      = $y - $main_img_obj_min_y;

                # fetch color information for both of our images
                $main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );

                # if our watermark has a non-transparent value at this pixel intersection
                # and we're still within the bounds of the watermark image
                if (  $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
                $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
                    $watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );

                    # using image alpha, and user specified alpha, calculate average
                    $watermark_alpha  = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );
                    $watermark_alpha  = $watermark_alpha * $alpha_level;

                    # calculate the color 'average' between the two - taking into account the specified alpha level
                    $avg_red          = $this->_get_ave_color( $main_rgb['red'],            $watermark_rbg['red'],        $watermark_alpha );
                    $avg_green  = $this->_get_ave_color( $main_rgb['green'],      $watermark_rbg['green'],      $watermark_alpha );
                    $avg_blue         = $this->_get_ave_color( $main_rgb['blue'],      $watermark_rbg['blue'],       $watermark_alpha );

                    # calculate a color index value using the average RGB values we've determined
                    $return_color     = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );

                    # if we're not dealing with an average color here, then let's just copy over the main color
                } else {
                    $return_color     = imagecolorat( $main_img_obj, $x, $y );

                } # END if watermark

                # draw the appropriate color onto the return image
                imagesetpixel( $return_img, $x, $y, $return_color );

            } # END for each X pixel
        } # END for each Y pixel
        # return the resulting, watermarked image for display
        return $return_img;
    } # END create_watermark()

    # average two colors given an alpha
    function _get_ave_color( $color_a, $color_b, $alpha_level ) {
        return round( ( ( $color_a * ( 1 - $alpha_level ) ) + ( $color_b  * $alpha_level ) ) );
    } # END _get_ave_color()

    # return closest pallette-color match for RGB values
    function _get_image_color($im, $r, $g, $b) {
        $c=imagecolorexact($im, $r, $g, $b);
        if ($c!=-1) return $c;
        $c=imagecolorallocate($im, $r, $g, $b);
        if ($c!=-1) return $c;
        return imagecolorclosest($im, $r, $g, $b);
    } # EBD _get_image_color()
} # END watermark API
?>

and this is how to use it

$watermark = new Watermark();
        $actual_image = imagecreatefromjpeg("images/$image_file");
        $watermark_image = imagecreatefrompng("images/water_mark.png");
        //create water marked image with 66% transparency
        $return_img_obj = $watermark->create_watermark( $actual_image, $watermark_image, 66 );

        //now save the updated image
        imagejpeg($return_img_obj,"images/$image_file1");

        //clear the memory
        imagedestroy($actual_image);
        imagedestroy($watermark_image);
        imagedestroy($return_img_obj);
原谅我要高飞 2024-11-08 00:43:49

不影响源图像。

要在不影响实际图像的情况下执行此操作,只需添加包含透明图像的 HTML 层即可。但这作为一种保护手段毫无用处,因为每个拥有一点点技术知识的人都可以在几秒钟内获取原始图像。

without affecting the source image.

Doing this without affecting the actual image is possible only by adding a HTML layer containing a transparent image. That is pretty useless as a means of protection though, because everyone with a tiny bit of technical knowledge can fetch the original image in seconds.

时间你老了 2024-11-08 00:43:49

如果您不想影响源图像,那么我建议您将透明图像放在 HTML 中图像上方的绝对定位 div 中。这意味着您不必在每次页面加载时处理图像(因为您想保持原始图像完整),并且不必费力存储额外的图像。

If you don't want to affect the source image, then I'd recommend putting a transparent image in an absolutely positioned div over the image in your HTML. This means you don't have to process an image on each page load (because you want to keep the original intact), and you don't have to mess about with storing an extra image.

东风软 2024-11-08 00:43:49

使用GD图像库: http://php.net/manual/en/book.image .php

这样我就可以创建图像的网络水印版本并存储源图像。

除非您打算即时添加水印,在这种情况下,我不确定您是否可以轻松做到这一点。

Using the GD image library: http://php.net/manual/en/book.image.php

This way I would create a web watermark version of the image and also store the source image.

Unless you mean to add a watermark on the fly in which case I'm not sure you can that easily.

镜花水月 2024-11-08 00:43:49

从第一眼看来,我认为如果您没有向函数提供目标文件名,则这段代码不会影响源文件。

From the first sight, I think this code will not affect the source file if u didn't provide a destination file name to the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文