How is it possible to find out the color of the picture, more precisely the background of this picture, or just what color is on the edges?

2 answers 2

You will need a library that can do this (gd, imagemagic ...) http://php.net/manual/en/book.image.php http://php.net/manual/en/book.imagick.php

GD

http://php.net/manual/en/function.imagecolorat.php

<?php $path = realpath('../path/to/./your/folder/testimage.jpg'); if ($path) { $im = imagecreatefrompng($path); $rgb = imagecolorat($im, 10, 15); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; var_dump($r, $g, $b); $colors = imagecolorsforindex($im, $rgb); var_dump($colors); // rgba } else { echo "Invalid file path"; } 

Imagemagick

http://php.net/manual/en/imagick.getimagepixelcolor.php

 <?php $path = realpath('../path/to/./your/folder/testimage.jpg'); if ($path) { $image = new Imagick($path); $x = 1; $y = 1; $pixel = $image->getImagePixelColor($x, $y); $colors = $pixel->getColor(); var_dump($colors); // produces Array([r]=>255,[g]=>255,[b]=>255,[a]=>1); var_dump($pixel->getColorAsString()); // produces rgb(255,255,255); } else { echo "Invalid file path"; } 

Only tested.

1) Need the full path to the file. Relative did not work correctly. http://php.net/manual/en/function.realpath.php . realpath will return false if the path is incorrect.

2) GD for working with transparency should be used. http://php.net/manual/en/function.imagecolorsforindex.php (only there, the transparency otherwise says 0 - non-transparent 127 - transparent)

Updated code.

  • The first one gives the answer: int (0) int (0) int (0) The second one does not give an answer at all. - Sasha Osipov
  • @ Sasha Osipov do you have both extensions installed? - E_p
  • Yes, connected. The reason is that the crypt file must be in the same folder with the image ... How to fix it? Because all images are stored in a separate folder. The extension is correct. - Sasha Osipov
  • @ SashaOsipov Updated code / response. - E_p
  • I found the problem myself, very interesting of course ... prntscr.com/dx3q50 - page prntscr.com/dx3qau - script Reason: You need to install another extension in php .... - Sasha Osipov

Make a print screen and open it in Photoshop using the eyedropper tool - the i button on the keyboard, if there is no Photoshop, then in the paint. If you do not want this way. There is a plugin - it shows chrome in the browser.

  • You did not understand at all ... It is necessary to learn RGB images by means of php - Sasha Osipov