Get image dimensions [duplicate]
Asked Answered
F

3

55

I have an url to the image, like $var = http://example.com/image.png

How do I get its dimensions to an array, like array([h]=> 200, [w]=>100) (height=200, width=100)?

Fennie answered 8/10, 2010 at 12:44 Comment(0)
L
143

You can use the getimagesize function like this:

list($width, $height) = getimagesize('path to image');
echo "width: " . $width . "<br />";
echo "height: " .  $height;
Layamon answered 8/10, 2010 at 12:46 Comment(6)
OP did ask "How do I get its dimensions to an array" so Im not sure why this is the fully accepted answer... GJ though.Roomful
@Dutchie432: He mainly meant how to get image dimensions and creating an array once you have width and height is/should be easy :)Layamon
I think it's safe to say that's your (likely correct) assumption, and not addressing the specifics of the question.Roomful
This option does not support the https:// protocol. Anyone know of any good options for secure image size detection?Lickspittle
@JustinKaz It supports https, but your server needs to have openSSL. https://mcmap.net/q/1175835/-getimagesize-and-https https is working well for me, for example.Sander
@Roomful getimagesize returns an array... list lists the array items out into variables.Sander
U
45

Using getimagesize function, we can also get these properties of that specific image-

<?php

list($width, $height, $type, $attr) = getimagesize("image_name.jpg");

echo "Width: " .$width. "<br />";
echo "Height: " .$height. "<br />";
echo "Type: " .$type. "<br />";
echo "Attribute: " .$attr. "<br />";

//Using array
$arr = array('h' => $height, 'w' => $width, 't' => $type);
?>

**Result like this -**

Width: 200
Height: 100
Type: 2
Attribute: width='200' height='100'


Type of image consider like -

1 = GIF
2 = JPG
3 = PNG
4 = SWF
5 = PSD
6 = BMP
7 = TIFF(intel byte order)
8 = TIFF(motorola byte order)
9 = JPC
10 = JP2
11 = JPX
12 = JB2
13 = SWC
14 = IFF
15 = WBMP
16 = XBM

Uncrowned answered 24/8, 2013 at 8:54 Comment(0)
R
20
<?php 
    list($width, $height) = getimagesize("http://site.com/image.png"); 
    $arr = array('h' => $height, 'w' => $width );
?>
Roomful answered 8/10, 2010 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.