This question was already answered for other languages,
but I wonder - is there a PHP solution how to get image's dimensions from image URL without
downloading the whole file or without downloading it at all?
Asked
Active
Viewed 3,655 times
1

jave.web
- 13,880
- 12
- 91
- 125
-
1Hope this post will help you [Super fast getimagesize in php][1] [1]: http://stackoverflow.com/questions/4635936/super-fast-getimagesize-in-php – Vijayakumar Selvaraj Jan 22 '14 at 13:00
-
@VijayakumarSelvaraj yes that really answers my question correctly :) – jave.web Jan 22 '14 at 13:16
2 Answers
2
You can use getimagesize(), but this requires you to download the entire file before retrieving information about it.
There's a nice function from a user on the PHP site which retrieves just enough bytes so that it can get the image dimensions, though I'm not sure if it will work for anything other than JPEG.
<?php
// Retrieve JPEG width and height without downloading/reading entire image.
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("Invalid file stream.");
$new_block = NULL;
if(!feof($handle)) {
$new_block = fread($handle, 32);
$i = 0;
if($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
$i += 4;
if($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
// Read block size and skip ahead to begin cycling through blocks in search of SOF marker
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
while(!feof($handle)) {
$i += $block_size;
$new_block .= fread($handle, $block_size);
if($new_block[$i]=="\xFF") {
// New block detected, check for SOF marker
$sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
if(in_array($new_block[$i+1], $sof_marker)) {
// SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.
$size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
$unpacked = unpack("H*", $size_data);
$unpacked = $unpacked[1];
$height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
$width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
return array($width, $height);
} else {
// Skip block marker and read block size
$i += 2;
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return FALSE;
}
}
}
}
}
return FALSE;
}
?>

Ben Fortune
- 31,623
- 10
- 79
- 80
-
-
1Its quite unreadable :) But I think I know what it does..., anyway it uses fopen - I need to retrieve the dimensions from url - what if allow_url_fopen is disabled? – jave.web Jan 22 '14 at 13:12
-
1I think this question has been really answered - **as @Vijayakumar Selvaraj said** - in here: http://stackoverflow.com/questions/4635936/super-fast-getimagesize-in-php – jave.web Jan 22 '14 at 13:15
0
$size=getimagesize("$pathToYourPicture");
$width=$size[0];
$height=$size[1];
How about this?

Top Questions
- 1,862
- 4
- 26
- 38
-
1Is `getimagesize` good for retrieving from URL ? and please note - I would like to download as little as possible – jave.web Jan 22 '14 at 13:04
-
this is not the answer you are looking for , yes it does the job but it downloads the entire image first which is useless in your case, for you other question yes you can use it for remote images with urls – Display name Jun 09 '16 at 11:36