Get the Width and Height of an Image Using a function in PHP


PHP provides a function to get the width and height of an image. The getimagesize() function will determine the size of image file including flash file(swf).

This is a very useful function in PHP, I found it on php.net PHP provides function to get the width and height of an image. The getimagesize() function will determine the size of image file including flash file(swf).

Code:

<?php

  //renumber

  $my_image = array_values(getimagesize('your-image.jpg'));

  //use list on new array

  list($width, $height, $type, $attr, , , $imgtype) = $my_image;

  //view new array

  echo "<pre>";

  print_r($my_image);

  echo "</pre>";

  echo "<br/>";

  //spit out content

  echo 'Attribute: '.$attr.'<br />';

  echo 'Width: '.$width.'<br />';

  echo 'Height: '.$height.'<br />';

  echo 'Type: '.$imgtype.'<br />';

?>


When you run this script you will see the result like this:

Array

(

    [0] => xxx

    [1] => xxx

    [2] => 2

    [3] => width="xxx" height="xxx"

    [4] => 8

    [5] => 3

    [6] => image/jpeg

)





Attribute: width="xxx" height="xxx"

Width: xxx

Height: xxx

Type: image/jpeg

You will get the width, height, type of an image and also attribute of an image, I use this function in my image upload form.

You can read a full manual here

getimagesize() Returns an array with up to 7 elements.

The 0 index is the width of the image in pixels.
The 1 index is the height of the image in pixels.
The 2 index is a flag for the image type:

Type of Image

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

The 3 index contains ' height="yyy" width="xxx" '

The 6 index contains image MIME type 'Ex. image/jpeg'

Post a Comment

0 Comments