php根据背景图片深浅加水印
生活随笔
收集整理的這篇文章主要介紹了
php根据背景图片深浅加水印
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近做到一個(gè)項(xiàng)目要實(shí)現(xiàn)這樣一個(gè)功能, 根據(jù)上傳圖片的主色調(diào), 在圖片上加水印或文字.
比如上傳的圖片顏色較深,文字的顏色用淺色標(biāo)識(shí),如果圖片顏色較淺, 則文字顏色用深色標(biāo)識(shí).
解決方法是:
先確定要加文字的圖片區(qū)域,遍歷每一個(gè)像素點(diǎn), 取得該區(qū)域像素點(diǎn)的平均亮度, 根據(jù)平均亮度決定文字的顏色. 如果該圖片區(qū)域的顏色不復(fù)雜,只是一個(gè)純色塊,就不需要遍歷了,只需要獲取色塊中某個(gè)像素點(diǎn)的亮度就可以了.
不建議遍歷整張圖, 而只是遍歷需要加水印的圖片文字的區(qū)域, 否則會(huì)有性能問(wèn)題.
/*** 獲取圖片某個(gè)像素的顏色及亮度* @author church <church_qi@aliyun.com>* @date 2017-4-5*/ class ImgColor {/*** RGB格式*/const RGB = 1;/*** 6位十六進(jìn)制格式*/const HEX_6_BIT = 2;/*** 8位十六進(jìn)制格式*/const HEX_8_BIT = 3;/*** 打開(kāi)圖片* @param string $img_path 圖片路徑* @return resource */public static function openImg($img_path){if(!is_file($img_path)) {throw new Exception("不存在的圖片");}$info = getimagesize($img_path);if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){throw new Exception("非法的圖片");}$img_info = array('width' => $info[0],'height' => $info[1],'type' => image_type_to_extension($info[2], false),'mime' => $info['mime'],);$fun = "imagecreatefrom{$img_info['type']}";$img = $fun($img_path);return $img;}/*** 獲取某個(gè)像素的顏色* @param string $img_path 圖片路徑* @param integer $x 橫坐標(biāo)* @param integer $y 縱坐標(biāo)* @param integer $format 格式* @return mixed*/public static function getColor($img_path = '', $x, $y, $format = self::HEX_8_BIT){static $img;$getRgb = function($img) use ($x, $y) {$rgb = imagecolorat($img, $x, $y);$r = ($rgb >> 16) & 0xFF;$g = ($rgb >> 8) & 0xFF;$b = $rgb & 0xFF;return [$r, $g, $b];};if (!(isset($img) && is_resource($img))) {$img = self::openImg($img_path);}return self::rgbToFormat($getRgb($img), $format);}/*** 獲取某個(gè)像素的亮度 (范圍為0~255, 越小越深)* 算法參考: https://www.w3.org/TR/AERT#color-contrast* * @param string $img_path 圖片路徑* @param integer $x 橫坐標(biāo)* @param integer $y 縱坐標(biāo)* @return double */public static function getBrightnessOfPixel($img_path = '', $x, $y){list($r, $g, $b) = self::getColor($img_path, $x, $y, self::RGB);$g = $r * 0.299 + $g * 0.587 + $b * 0.114;return $g;}/*** 把rgb格式轉(zhuǎn)換成其它格式* @param array $rgb * @param integer $format 格式* @return string */public static function rgbToFormat($rgb, $format){list($r, $g, $b) = $rgb;$result = '#';switch ($format) {case self::RGB:return $rgb;break;case self::HEX_6_BIT:$result .= str_pad(dechex($r), 2, '0', STR_PAD_LEFT);$result .= str_pad(dechex($g), 2, '0', STR_PAD_LEFT);$result .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);return $result;break;case self::HEX_8_BIT:$result .= 'ff';$result .= str_pad(dechex($r), 2, '0', STR_PAD_LEFT);$result .= str_pad(dechex($g), 2, '0', STR_PAD_LEFT);$result .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);return $result;break;}}}亮度的范圍為0~255, 越小越深, 越大越淺; 判斷時(shí)可以取中位, 大于125就算淺,小于就算深.
require './ImgColor.php';$img_path = './test.png';$brightness = 0; for ($i = 0; $i < 100; $i++) {for ($j = 0; $j < 100; $j++) {$brightness += ImgColor::getBrightnessOfPixel($img_path, $i, $j);} }$brightness /= 10000;$font_color = '';$brightness = $brightness > 125 ? '較淺' : '較深';//若背景色較淺, 水印文字顏色設(shè)置為黑色, 反之則設(shè)置為白色 $font_color = $brightness == '較淺' ? '#00000000' : '#ffffffff';總結(jié)
以上是生活随笔為你收集整理的php根据背景图片深浅加水印的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Elastic search 入门
- 下一篇: 动态规划算法php,php算法学习之动态