首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP中取图片色系并生成跟图片反色差的字体

PHP中取图片色系并生成跟图片反色差的字体

原创
作者头像
V站CEO-西顾
修改2018-06-10 10:50:45
1.6K0
修改2018-06-10 10:50:45
举报
文章被收录于专栏:V站V站

V站笔记

原取图片色系:

function imgColor($imgUrl){	$imageInfo = getimagesize($imgUrl);	$imgType   = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));	$imageFun  = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);	$i         = $imageFun($imgUrl);	$rColorNum = $gColorNum = $bColorNum = $total = 0;	for ($x = 0; $x < imagesx($i); $x++) {		for ($y = 0; $y < imagesy($i); $y++) {			$rgb = imagecolorat($i, $x, $y);			$r   = ($rgb >> 16) & 0xFF;			$g   = ($rgb >> 8) & 0xFF;			$b   = $rgb & 0xFF;			$rColorNum += $r;			$gColorNum += $g;			$bColorNum += $b;			$total++;		}	}	$rgb      = array();	$rgb['r'] = round($rColorNum / $total);	$rgb['g'] = round($gColorNum / $total);	$rgb['b'] = round($bColorNum / $total);	return $rgb;} $rgb = imgColor("图片地址");print_r($rgb); //将打印出一个数组

根据函数我们可以理解到,此函数运行时会依次遍历所有的像素点。然后取出现最多的像素点。

由此产生了一个问题。如果遇到这种图片:

就会发现取出了 255,255,255的全白色。

于是:我们改进一下函数:

function imgColor($imgUrl){	$imageInfo = getimagesize($imgUrl);	$imgType   = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));	$imageFun  = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);	$i         = $imageFun($imgUrl);	$rColorNum = $gColorNum = $bColorNum = $total = 0;	for ($x = 50; $x < imagesx($i) - 50; $x++) {		for ($y = 50; $y < imagesy($i) - 50; $y++) {			$rgb = imagecolorat($i, $x, $y);			$r   = ($rgb >> 16) & 0xFF;			$g   = ($rgb >> 8) & 0xFF;			$b   = $rgb & 0xFF;			$rColorNum += $r;			$gColorNum += $g;			$bColorNum += $b;			$total++;		}	}	$rgb      = array();	$rgb['r'] = round($rColorNum / $total);	$rgb['g'] = round($gColorNum / $total);	$rgb['b'] = round($bColorNum / $total);	return $rgb;}$rgb = imgColor("图片地址");print_r($rgb); //将打印出一个数组  /*** x=50 * imagesx($i) - 50* y=50* imagesy($i) - 50* 相当于 margin 属性 **/

我们发现取色正常了。如果不是很正常依然可以调大数值。

根据图片色系来生成一个对应的字体:

$gray      = '255,255,255';$grayLevel = $img['r'] * 0.299 + $img['g'] * 0.587 + $img['b'] * 0.114;if ($grayLevel >= 150) {	$gray = '0,0,0';}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档