Computer >> Computer tutorials >  >> Programming >> PHP

imagecolormatch() function in PHP


The imagecolormatch() function forms the colors of the palette version of an image more closely match the true color version

Syntax

bool imagecolormatch ( img1, img2 )

Parameters

  • img1: Create image with imagecreatetruecolor() function.

  • img2: A palette image link resource pointing to an image. This image has the same size as img1.

Return

The imagecolormatch() function returns TRUE on success or FALSE on failure.

Example

The following is an example

<?php
   $img1 = imagecreatefrompng('https://www.tutorialspoint.com/images/Swift.png');
   $img2 = imagecreate(imagesx($img1), imagesy($img1));
   $color = Array();
   $color[] = imagecolorallocate($img2, 110, 40, 180);
   $color[] = imagecolorallocate($img2, 90, 10, 90);
   $color[] = imagecolorallocate($img2, 66, 170, 110);
   $color[] = imagecolorallocate($img2, 130,90, 70);
   echo imagecolormatch($img1, $img2);
   imagedestroy($img1);
   imagedestroy($img2);
?>

Output

The following is the output:

1

Example

Let us see another example

<?php
   $img1 = imagecreatefrompng('https://www.tutorialspoint.com/images/tp-logo-diamond.png');
   $img2 = imagecreate(imagesx($img1), imagesy($img1));
   $color = Array();
   $color[] = imagecolorallocate($img2, 10, 1, 20);
   $color[] = imagecolorallocate($img2, 40, 30, 10);
   $color[] = imagecolorallocate($img2, 15, 100, 50);
   $color[] = imagecolorallocate($img2, 70, 20, 30);
   echo imagecolormatch($img1, $img2);
   imagedestroy($img1);
   imagedestroy($img2);
?>

Output

The following is the output:

1