imagecolormatch

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

imagecolormatch β€” ΠŸΡ€ΠΈΠ±Π»ΠΈΠΆΠ°Π΅Ρ‚ Ρ†Π²Π΅Ρ‚Π° ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ³ΠΎ изобраТСния ΠΊ truecolor-вСрсии

ОписаниС

function imagecolormatch(GdImage $image1, GdImage $image2): true

Ѐункция подгоняСт Ρ†Π²Π΅Ρ‚Π° ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ³ΠΎ изобраТСния ΠΊ truecolor-вСрсии

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image1

ΠžΠ±ΡŠΠ΅ΠΊΡ‚ truecolor-изобраТСния.

image2

ΠžΠ±ΡŠΠ΅ΠΊΡ‚ ΠΏΠ°Π»ΠΈΡ‚Ρ€ΠΎΠ²ΠΎΠ³ΠΎ изобраТСния с Ρ‚Π΅ΠΌ ΠΆΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ, Ρ‡Ρ‚ΠΎ ΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ image1.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ image1 ΠΈ image2 Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΎΠΆΠΈΠ΄Π°ΡŽΡ‚ экзСмпляр класса GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ оТидался Ρ‚ΠΈΠΏ resource.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΏΠΎΠ΄Π³ΠΎΠ½ΠΊΠΈ Ρ†Π²Π΅Ρ‚ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagecolormatch()

<?php

// Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
$im1 = imagecreatefrompng('./gdlogo.png');
$im2 = imagecreate(imagesx($im1), imagesy($im1));

// ДобавляСм ряд Ρ†Π²Π΅Ρ‚ΠΎΠ² Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $im2
$colors = array();
$colors[] = imagecolorallocate($im2, 255, 36, 74);
$colors[] = imagecolorallocate($im2, 40, 0, 240);
$colors[] = imagecolorallocate($im2, 82, 100, 255);
$colors[] = imagecolorallocate($im2, 84, 63, 44);

// Π—Π°Π΄Π°Ρ‘ΠΌ соотвСтствия Ρ†Π²Π΅Ρ‚ΠΎΠ² truecolor-Ρ†Π²Π΅Ρ‚Π°ΠΌ изобраТСния
imagecolormatch($im1, $im2);

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagecreatetruecolor() - Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ truecolor-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
2
gk at karko dot net ΒΆ
15 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 ΒΆ
20 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
0
Samantha ΒΆ
22 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.