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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.