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

Monday, October 10, 2022

[FIXED] How to check if and image is mono color or empty?

 October 10, 2022     gd, image, php     No comments   

Issue

I need to prevent users from uploading blank profile images. For blank images I mean a full white or any other full color image. I think the best approach is to check if the images contains just one color but i don't know how to do it.

I'm trying to check this after the image is uploaded, just to simplify things.

Could anyone help me on this?


Solution

Check whole image

1) Resize your image to smaller one (eg resize it originalSize/10 or sth like this...) 2) Use this function to get image size 3) Loop throught pixels, save the color of first one and continue till the color of pixels on position x,y is different or until there are no more pixels unchecked.

    <?php

function isColorSame($imgname){
$img = imagecreatefrompng($imgname);
list($width, $height, $type, $attr) = getimagesize($imgname);

$x = 0;
$y = 0;

$starterColor = imagecolorat(0, 0, $img);

while($x < $width){
    while($y < $height) {
        if (imagecolorat($x, $y, $img) !== $starterColor){
            return false;
        }
        $y++;
    }
    $x++;
}
return true;
}

Guess

Check just few pixels (eg. 100 pixels) by generating random x,y and checking color at this position. This is not 100% sure, but after this, you can pass the image to the function, which checks all pixels.



Answered By - Eakethet
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • 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