|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License |
| 12 | + * |
| 13 | + * This code was originally taken from: |
| 14 | + * https://github.com/ktomk/Miscellaneous/blob/master/get_png_imageinfo/get_png_imageinfo.php |
| 15 | + * It has been modified to fix bugs and improve code formatting |
| 16 | + * |
| 17 | + * Get image-information from PNG file |
| 18 | + * |
| 19 | + * php's getimagesize does not support additional image information |
| 20 | + * from PNG files like channels or bits. |
| 21 | + * |
| 22 | + * get_png_imageinfo() can be used to obtain this information |
| 23 | + * from PNG files. |
| 24 | + * |
| 25 | + * @author Tom Klingenberg <lastflood.net> |
| 26 | + * @license Apache 2.0 |
| 27 | + * @link https://github.com/ktomk/Miscellaneous/blob/master/get_png_imageinfo/get_png_imageinfo.php |
| 28 | + * @link http://www.libpng.org/pub/png/spec/iso/index-object.html#11IHDR |
| 29 | + * |
| 30 | + * @param string $file filename |
| 31 | + * @return array|bool image information, FALSE on error |
| 32 | + */ |
| 33 | +function get_png_imageinfo($file) { |
| 34 | + if (! is_file($file)) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + $info = unpack( |
| 39 | + 'a8sig/Nchunksize/A4chunktype/Nwidth/Nheight/Cbit-depth/Ccolor/Ccompression/Cfilter/Cinterface', |
| 40 | + file_get_contents($file, 0, null, 0, 29) |
| 41 | + ); |
| 42 | + |
| 43 | + if (empty($info)) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + if ("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" != array_shift($info)) { |
| 47 | + return false; // no PNG signature |
| 48 | + } |
| 49 | + if (13 != array_shift($info)) { |
| 50 | + return false; // wrong length for IHDR chunk |
| 51 | + } |
| 52 | + if ('IHDR'!==array_shift($info)) { |
| 53 | + return false; // a non-IHDR chunk singals invalid data |
| 54 | + } |
| 55 | + |
| 56 | + $color = $info['color']; |
| 57 | + $type = array( |
| 58 | + 0 => 'Greyscale', |
| 59 | + 2 => 'Truecolour', |
| 60 | + 3 => 'Indexed-colour', |
| 61 | + 4 => 'Greyscale with alpha', |
| 62 | + 6 => 'Truecolour with alpha' |
| 63 | + ); |
| 64 | + |
| 65 | + if (empty($type[$color])) { |
| 66 | + return false; // invalid color value |
| 67 | + } |
| 68 | + |
| 69 | + $info['color-type'] = $type[$color]; |
| 70 | + $samples = ((($color % 4) % 3) ? 3 : 1) + ($color > 3 ? 1 : 0); |
| 71 | + $info['channels'] = $samples; |
| 72 | + $info['bits'] = $info['bit-depth']; |
| 73 | + |
| 74 | + return $info; |
| 75 | +} |
0 commit comments