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

Newsun

Believe in Good
Thành viên thân thiết
Tham gia
20/4/2008
Bài viết
9.433
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.

1. Cách làm

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


https://forum.aloxovn.com/showthread.php?t=333#
PHP:
<?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


<img src="/@forum/images/star.jpg"/>


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

<img src="/@forum/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.

2. Giải thích

Câu lệnh switch xác định kiểu ảnh để xuất header thích hợp. Do GD 1.6.2 trở về sau không hỗ trợ .gif (các phiên bản 2.x bundled hỗ trợ gif readonly), nên nếu không phải ảnh .jpg hoặc .png, nó sẽ xuất thẳng mà không xử lí gì cả.
Các dòng sau đó tìm hiểu về màu sắc của khu vực sẽ viết chữ, để định màu phù hợp cho watermark. Để đơn giản, bạn có thể xoá luôn hàm get_color() và đặt thẳng giá trị cho $color và $color_bg.
3. 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 sẻ cho. Chúc bạn ngày càng tiến bộ

Sưu tầm
 
×
Quay lại
Top