IHYPRESS PROGRAMMING
PHP programs with code and output for beginners
php logo
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
PHP ❯ Colour Analysis
<15>
<?php /* show image */ echo "<img src='https://i.imgur.com/dSPQrMN.jpg' style='height:200px; width:auto;'><br>"; /* create an image resource based on an existing image file */ /* use the imagecreatefrompng function for a png image */ $img = imagecreatefromjpeg('https://i.imgur.com/dSPQrMN.jpg'); /* get the image resource width and height (resolution) */ $width = imagesx($img); $height = imagesy($img); $npixels = $width * $height; /* loop through every pixel of the image */ $r=0; $g=0; $b=0; for ($col = 0; $col <= $width; $col++) { for ($row = 0; $row <= $height; $row++) { $colourn = imagecolorat($img, $col, $row); //gets the pixel colour as an integer /* get the number of red, green and blue pixels using bitwise operators */ $red = (($colourn >> 16) & 255); $green = (($colourn >> 8) & 255); $blue = ($colourn & 255); if ($red > $green && $red > $blue) {$r = $r + 1;} else if ($green > $red && $green > $blue) {$g = $g + 1;} else if ($blue > $green && $blue > $red) $b = $b + 1; } } /* get the percentages of each primary colour formatted one decimal */ $rp = $r * 100 / $npixels; $rp = number_format($rp,1); $gp = $g * 100 / $npixels; $gp = number_format($gp,1); $bp = $b * 100 / $npixels; $bp = number_format($bp,1); /* the report */ echo "<div>Resolution: $width x $height pixels</div>"; echo "<div>Total number of pixels = $npixels</div>"; echo "<div>Red pixels: $r ($rp%)<br>Green pixels: $g ($gp%)<br>Blue pixels: $b ($bp%)</div>"; ?>
Hergestellt in Deutschland / Made in Germany

Resolution: 1920 x 1080 pixels
Total number of pixels = 2073600
Red pixels: 390450 (18.8%)
Green pixels: 209034 (10.1%)
Blue pixels: 1446413 (69.8%)
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland