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.
1. Cách là m
Tạo file index.php trong thư mục images của bạn với nội dung
$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
Bây giá», bạn thay bằng
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 xẻ cho. Chúc bạn ngà y cà ng tiến bá»™