PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

[FIXED] How to know howmany colors are exist in the image?

 September 19, 2022     gd, imagick, jquery, php     No comments   

Issue

<?php
$img=imagecreatefrompng('dense.png');
list($width, $height)=getimagesize('dense.png'); 
$t=0;
for( $i=0 ; $i<$height ; $i++ )
{
    for( $j=0 ; $j<$width ; $j++ )
    {
        $pix  = imagecolorat($img, $i, $j);
        $cols = imagecolorsforindex($img, $pix);
        $r = $cols['red'];
        $g = $cols['green'];
        $b = $cols['blue'];
        $pixel[$i][$j][0]=$r;
        $pixel[$i][$j][1]=$g;
        $pixel[$i][$j][2]=$b;       
        }
}

for( $i=0 ; $i<$height ; $i++ )
{
    for( $j=0 ; $j<$width ; $j++ )
    {
        echo "(".$i.",".$j.") color of that pixel is (".$pixelcolor[$i][$j][0].",".$pixelcolor[$i][$j][1].",".$pixelcolor[$i][$j][2].").</p>";
    }
    echo"<br/>";
}
?>

This is the my code but when i run this code its give me blank webpage.

I want to make one array which store the rgb value of each pixel and it also print on the webpage and reduce those value which are repeating in the array.

so i want to know that how many colors are exist in the images?


Solution

Your code is probably dying from a memory exhaustion error. Creating a million or so arrays can cause that. Here's a script that counts the number of unique colors in an image in a fairly efficient manner (for PHP):

<?php

$path = "test.jpg";

$img = imagecreatefromjpeg($path);
$w = imagesx($img);
$h = imagesy($img);

// capture the raw data of the image
ob_start();
imagegd2($img, null, $w);
$data = ob_get_clean();
$totalLength = strlen($data);

// calculate the length of the actual pixel data
// from that we can derive the header size
$pixelDataLength = $w * $h * 4;
$headerLength = $totalLength - $pixelDataLength;

// use each four-byte segment as the key to a hash table
$counts = array();
for($i = $headerLength; $i < $totalLength; $i += 4) {
    $pixel = substr($data, $i, 4);
    $count =& $counts[$pixel];
    $count += 1;
}
$colorCount = count($counts);
echo $colorCount;

?>

$colorCount is the number of unique colors in the image. $counts gives you the number of times each color occurs. Each $key is a 4-byte string. The first byte is transparency. A value of zero means opaque. The second, third, and fourth bytes are R, G, and B respectively. You'll need to call ord() to get the value.



Answered By - cleong
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing