onlyican Posted July 16, 2012 Share Posted July 16, 2012 Hi Background: We have a large MySQL table which stores data and images as BLOBS (do not ask) My Task: To extract the images from the SQL and save them as image files Problem: to Calculate the image type from the blob itself. (if its jpg, png ect) I have heard that I can use a regex to work out if the image is jpg / png ect, but not sure what it is, could anyone advice?? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/ Share on other sites More sharing options...
lordshoa Posted July 16, 2012 Share Posted July 16, 2012 Could you not just use the header ? Or does this not work on blobs ? if(header("Content-type: image/jpg")){ print $row['image']; }else if (header("Content-type: image/gif"); ){ print $row['image']; }else if(header("Content-type: image/png") ){ print $row['image']; } Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361852 Share on other sites More sharing options...
onlyican Posted July 16, 2012 Author Share Posted July 16, 2012 header does not work on strings. Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361853 Share on other sites More sharing options...
ignace Posted July 16, 2012 Share Posted July 16, 2012 You can try using http://www.php.net/manual/en/function.exif-imagetype.php: file_put_contents($row['id'], $row['bdata']); switch (exif_imagetype($row['id'])) { case IMAGETYPE_GIF: rename($row['id'], $row['id'] . '.gif'); break; .. } Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361856 Share on other sites More sharing options...
onlyican Posted July 16, 2012 Author Share Posted July 16, 2012 Cheers ignance I was going down that avenue. Forgot about the "rename()" function Was going to create a tmp file, use exif_imagetype then save image, delete tmp but your method seems, shall we say, less messy Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361857 Share on other sites More sharing options...
Adam Posted July 16, 2012 Share Posted July 16, 2012 Alternatively.. Each image type has it's own unique 'signature' within the first few bytes of the image. That means you can just run through an array of pre-defined signatures and check which matches, then write to a file: function getImageTypeFromBlob($imageData) { $signatures = array( 'jpg' => "\xFF\xD8\xFF", 'gif' => "GIF", 'png' => "\x89PNG", 'bmp' => "BM", 'swf' => "CWS" ); $first4Bytes = substr($imageData, 0, 4); foreach ($signatures as $imageType => $signature) { if (strpos($first4Bytes, $signature) === 0) { return $imageType; } } return false; } Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361860 Share on other sites More sharing options...
Adam Posted July 16, 2012 Share Posted July 16, 2012 I would recommend using ignace's method by the way, given you're already writing to a file and it's only what exif_imagetype() does internally. Just posted the alternative method so you're a bit clearer on how it works. Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361861 Share on other sites More sharing options...
onlyican Posted July 16, 2012 Author Share Posted July 16, 2012 Cheers Adam. I have to do this for thousands of image entries in the db We have approx 175000 entries per DAY. this is over at least 3 years of data. So I might use your option Adam simply to save one process on the rename (normally wouldn't bother but at this scale, i think I might) Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361864 Share on other sites More sharing options...
Adam Posted July 16, 2012 Share Posted July 16, 2012 Hmm I would definitely recommend benchmarking the two methods. Both have positives and negatives that stretched over 175000 repetitions could be quite a substantial difference. I would still lean towards the exif_imagetype() method though to be honest, just because it's a native function and more robust. I don't think the additional file read and rename would cause much of a difference. Benchmark them and find out though.. Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361876 Share on other sites More sharing options...
ignace Posted July 16, 2012 Share Posted July 16, 2012 The rename is not required per se, I added it as convenience. As programs will always read the first few bytes to find the image mime type, only Windows uses file extensions... Quote Link to comment https://forums.phpfreaks.com/topic/265747-regex-to-get-file-type-from-blob/#findComment-1361879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.