note 122015 added to function.getimagesize

From: Date: Tue, 12 Dec 2017 19:24:16 +0000
Subject: note 122015 added to function.getimagesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-209357@lists.php.net to get a copy of this message
There's a code snippet for getting JPEG image dimensions by getting only first few bytes of the file, but it doesn't work for PNG files, so I wrote one. It will download only the first 24 bytes instead of the whole image, and thus being much faster than getimagesize() and it will save bandwidth at the same time: <?php // Retrieve PNG width and height without downloading/reading entire image. function getpngsize( $img_loc ) { $handle = fopen( $img_loc, "rb" ) or die( "Invalid file stream." ); if ( ! feof( $handle ) ) { $new_block = fread( $handle, 24 ); if ( $new_block[0] == "\x89" && $new_block[1] == "\x50" && $new_block[2] == "\x4E" && $new_block[3] == "\x47" && $new_block[4] == "\x0D" && $new_block[5] == "\x0A" && $new_block[6] == "\x1A" && $new_block[7] == "\x0A" ) { if ( $new_block[12] . $new_block[13] . $new_block[14] . $new_block[15] === "\x49\x48\x44\x52" ) { $width = unpack( 'H*', $new_block[16] . $new_block[17] . $new_block[18] . $new_block[19] ); $width = hexdec( $width[1] ); $height = unpack( 'H*', $new_block[20] . $new_block[21] . $new_block[22] . $new_block[23] ); $height = hexdec( $height[1] ); return array( $width, $height ); } } } return false; } ?> ---- Server IP: 208.43.231.9 Probable Submitter: 109.92.53.240 ---- Manual Page -- http://php.net/manual/en/function.getimagesize.php Edit -- https://master.php.net/note/edit/122015 Del: integrated -- https://master.php.net/note/delete/122015/integrated Del: useless -- https://master.php.net/note/delete/122015/useless Del: bad code -- https://master.php.net/note/delete/122015/bad+code Del: spam -- https://master.php.net/note/delete/122015/spam Del: non-english -- https://master.php.net/note/delete/122015/non-english Del: in docs -- https://master.php.net/note/delete/122015/in+docs Del: other reasons-- https://master.php.net/note/delete/122015 Reject -- https://master.php.net/note/reject/122015 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209357) next »