百科狗-知识改变命运!
--

imagecolormatch() - gd函数(图像处理)

乐乐11个月前 (11-21)阅读数 9#技术干货
文章标签版本

imagecolormatch()

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

使一个图像中调色板版本的颜色与真彩色版本更能匹配

说明

imagecolormatch(resource $image1,resource $image2): bool

使一个图像中调色板版本的颜色与真彩色版本更能匹配。

参数

$image1

真彩色图像连接资源。

$image2

必须是调色板图像,而且和$image1的大小必须相同。

返回值

imagecolormatch() - gd函数(图像处理)

成功时返回TRUE,或者在失败时返回FALSE

范例

imagecolormatch()例子

注释

Note:此函数仅在与 GD 库捆绑编译的 PHP 版本中可用。

Note:此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

参见

  • imagecreatetruecolor()新建一个真彩色图像
Those that have Ubuntu servers note, that this function is added in PHP's GD library fork and is not available by default in Ubuntu php5-gd package.
Here's how-to install the PHP GD version: http://preview.tinyurl.com/yel4r7t
This function appears to work by changing the values of the colors of the paletted image -- no good if you're trying to force the resultant image to stick with certain pre-defined color values.
This function is a godsend! It works exactly as documented.
I'm working on an application where I need to take a transparent GIF, matte the GIF on a user defined background color, and finally scale the GIF based on a user defined %.
The only way I could get this to work so that the final image was high quality, ie: no jagged edges, and a smooth scale, was to convert the GIF to a JPG, and then copy the JPG into a new GIF image like this:
// open transparent gif
$GIFimg = imagecreatefromgif($file_path);
// create jpg image
$JPGimg = imagecreatetruecolor($width, $height);
// copy GIF to JPG
imagecopy($JPGimg, $GIFimg, 0, 0, 0, 0, $width, $height); 
// create a true color image
$JPGscaled = imagecreatetruecolor($n_width, $n_height);
// scale the new JPG using the truecolor image
imagecopyresampled($JPGscaled, $JPGimg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// create final GIF image
$GIFfinal = imagecreate($n_width, $n_height);
// copy the scaled JPG back to a GIF
imagecopymerge($GIFfinal, $JPGscaled, 0, 0, 0, 0, $n_width, $n_height, 100);
This worked great except the final step, copying the JPG to a GIF. If the JPG had too many colors, the function would index the colors to make it a palette image. So what would end up happening is the final image contained INCORRECT colors.
Adding this one line at the bottom of the code fixed everything.
imagecolormatch($JPGscaled, $GIFfinal);
I hope this helps anyone who is converting images back and forth and dealing with palette issues and color correction. Also, be aware that the above code is a sample and will not work by copying and pasting.
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)