JeremyCanada26 Posted May 23, 2011 Share Posted May 23, 2011 I have two questions regarding $byte_array. 1. Is there a general way to check if a $byte_array contains any data? 2. Is there a way to confirm if the $byte_array contains an actual PNG image? Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/ Share on other sites More sharing options...
fugix Posted May 23, 2011 Share Posted May 23, 2011 to check if a variable contains data you can use the empty() function Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/#findComment-1218899 Share on other sites More sharing options...
JeremyCanada26 Posted May 23, 2011 Author Share Posted May 23, 2011 to check if a variable contains data you can use the empty() function Thanks fugix, yes that empty() function is awesome. I wonder if it's possible to check if the $byte_array contains valid PNG data? Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/#findComment-1218903 Share on other sites More sharing options...
fugix Posted May 23, 2011 Share Posted May 23, 2011 yeah you can check if the variable contains the mime type "image/png" Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/#findComment-1218906 Share on other sites More sharing options...
JeremyCanada26 Posted May 23, 2011 Author Share Posted May 23, 2011 function checkIfSuppliedImageDataIsValid() { //first, check that the byte_array isset, then make sure it's not empty, then make sure it contains valid image data if(isset($this->byte_array) && !empty($this->byte_array) && imagecreatefromstring($this->byte_array)) { //passed in byte array is good return true; } else { //missing or invalid return false; } } The above function is what i'm settling for, although I also see there might be another way to do it manually but I'm not technical enough to know how. The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10 Taken from the png RFC page, http://www.faqs.org/rfcs/rfc2083.html, section, 3.1. PNG file signature Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/#findComment-1218910 Share on other sites More sharing options...
fugix Posted May 23, 2011 Share Posted May 23, 2011 okay Link to comment https://forums.phpfreaks.com/topic/237170-how-to-check-if-a-byte_array-contains-data/#findComment-1218913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.