Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to check if a PNG image has transparent background?

I'm using a crop & resize function for images, but I need to let it crop/resize ONLY png files WITH transparent backgrounds, at least 1 pixel in the image should be transparent for the image to be accepted.

Is possible to check if a PNG image has transparent background/pixels?

I'm using PHP and GD libraries.

EDIT: Ok, I've figured out how to do this on PHP with GD libraries. Look how clean it looks! :)

<?php

$im = imagecreatefrompng("php.png");
$rgba = imagecolorat($im,1,1);
$alpha = ($rgba & 0x7F000000) >> 24;

var_dump($alpha);
?>

Any ideas how to do an array for the x/y coordenates to check all the image pixels looking for at least 1 pixel = $alpha = 127?

like image 927
user1143241 Avatar asked Nov 05 '22 08:11

user1143241


1 Answers

You can call out to imagemagick for it:

https://www.imagemagick.org/discourse-server/viewtopic.php?t=18596

convert my_image.png -format "%[opaque]" info:

False

like image 140
rogerdpack Avatar answered Nov 09 '22 06:11

rogerdpack