hn4u @ Last updated 21/11/04 22:42
Go to my homepage at http://4u.jcisio.com
Full version available at http://4u.jcisio.com/r/article617.htm

Hải Nam

Giữ bản quyền ảnh bằng watermark

Watermark là hình, chữ được in chìm trên tấm ảnh. Bài viết này sẽ hướng dẫn bạn tạo watermark một cách tự động từ ảnh gốc của mình. Bạn cần có PHP và GD.

Cách làm

Tạo file index.php trong thư mục images của bạn với nội dung

CODE (PHP)
$direction = 'H';
$dim = true;
$text = '4u.jcisio.com';
$font = 2;

error_reporting(0);
$filename = $_SERVER['QUERY_STRING'];

$size = GetImageSize ($filename);
switch (
$size[2])
{
    case
2:
       
Header('Content-type: image/jpeg');
       
$funcCreate = 'ImageCreateFromJpeg';
       
$funcOutput = 'ImageJpeg';
        break;
    case
3:
       
Header('Content-type: image/png');
       
$funcCreate = 'ImageCreateFromPng';
       
$funcOutput = 'ImagePng';
        break;
    default:
       
Header ('Content-type: image/gif');
        if (! @
readfile($filename))

        {
           
$error = ImageCreate(100,40);
           
$c_grey = ImageColorAllocate($error, 128, 128, 128);
           
$c_white = ImageColorAllocate($error, 255, 255, 255);
           
ImageFill($error, 0, 0, $c_grey);
           
Imagestring($error, 5, 1, 12, 'Image error', $c_white);
            echo
ImagePng($error);
           
ImageDestroy($error);
            return;
        }
}

$width = ImageFontWidth($font)*strlen($text);
$height = ImageFontHeight($font);

$image = $funcCreate($filename);
if (
$direction=='H')
{
   
get_color($size[0]-$width-5, $size[1]-$height-5, $size[0]-5, $size[1]-5);
   
ImageString($image, $font, $size[0]-$width-4, $size[1]-$height-4, $text, $color_bg);
   
ImageString($image, $font, $size[0]-$width-5, $size[1]-$height-5, $text, $color);
}
else
{
   
get_color($size[0]-$height-5, $size[1]-$width-5, $size[0]-5, $size[1]-5);
   
ImageStringUp($image, $font, $size[0]-$height-4, $size[1]-6, $text, $color_bg);
   
ImageStringUp($image, $font, $size[0]-$height-5, $size[1]-5, $text, $color);
}
echo
$funcOutput($image);
ImageDestroy($image);

function
get_color($x1, $y1, $x2, $y2)
{
    global
$image, $dim, $color, $color_bg;
    if (!
$dim)
    {
       
$color = ImageColorAllocate($image, 0, 255, 255);
       
$color_bg = ImageColorAllocate($image, 255, 0, 255);
        return;
    }

    for(
$x = $x1; $x <= $x2; $x++)
    {
        for (
$y = $y1; $y <= $y2; $y++)
        {
           
$colors = ImageColorsForIndex($image, ImageColorAt($image, $x, $y));
           
$text_color['r'] += $colors['red'];
           
$text_color['r'] /= 2;
           
$text_color['g'] += $colors['green'];
           
$text_color['g'] /= 2;
           
$text_color['b'] += $colors['blue'];
           
$text_color['b'] /= 2;
        }
    }
   
$text_color['r'] = $text_color['r'] < 128 ? $text_color['r'] + 128 : $text_color['r'] - 128;
   
$text_color['g'] = $text_color['g'] < 128 ? $text_color['g'] + 128 : $text_color['g'] - 128;
   
$text_color['r'] = $text_color['r'] < 128 ? $text_color['r'] + 128 : $text_color['r'] - 128;

   
$color = ImageColorAllocate($image, $text_color['r'], $text_color['g'], $text_color['b']);
   
$color_bg = ImageColorAllocate($image,
                        (
$text_color['r'] > 128 ? 10 : 240),
                        (
$text_color['g'] > 128 ? 10 : 240),
                        (
$text_color['b'] > 128 ? 10 : 240));
}

Thông thường, bạn gọi một file ảnh bằng

HTML
<img src="images/star.jpg">

Bây giờ, bạn thay bằng

HTML
<img src="images/?star.jpg">

Trông cũng giống nhau, nhưng bản chất thay đổi. Trong cách gọi thứ 2, bạn mở file images/, đây là tên thư mục nên nó lấy file mặc định là index.php. Tập tin index.php sẽ xử lí tham số của nó và xuất ra ảnh có watermark. Bạn có thể ghi đầy đủ là  images/index.php?star.jpg, nhưng như thế không mang vẻ thần bí lắm.

Giải thích

Lời kết

Bạn đã biết được cách làm watermark, nhưng không nên lạm dụng nó. Hãy nhớ rằng đại đa số những cái bạn có là do người khác chia xẻ cho. Chúc bạn ngày càng tiến bộ


hainam4u @ Last updated 21/11/04 22:42
Go to my homepage at http://4u.jcisio.com