imagecolormatch

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

imagecolormatchパレットイメージの色を True カラーイメージに近づける

説明

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

パレットイメージの色を True カラーイメージに近づけます。

パラメータ

image1

truecolor イメージリンクリソース。

image2

パレットイメージリンクリソース。 image1 と同じ大きさの画像をさします。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 imagecolormatch() の例

<?php
// true color 画像とパレット画像を用意します
$im1 imagecreatefrompng('./gdlogo.png');
$im2 imagecreate(imagesx($im1), imagesy($im1));

// いくつかの色を $im2 に追加します
$colors   = Array();
$colors[] = imagecolorallocate($im22553674);
$colors[] = imagecolorallocate($im2400240);
$colors[] = imagecolorallocate($im282100255);
$colors[] = imagecolorallocate($im2846344);

// これらの色を true color 画像にマッチさせます
imagecolormatch($im1$im2);

// メモリから開放します
imagedestroy($im1);
imagedestroy($im2);
?>

参考

add a note add a note

User Contributed Notes 3 notes

up
1
gk at karko dot net
10 years ago
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
up
1
albriNOght at anSPAMre dot net
14 years ago
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.
up
-1
Samantha
16 years ago
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.
To Top