Get An Image Size With PHP

PHP has a function called getimagesize() for getting the width and height from an image. I’m always forgetting the name of the getimagesize() function so I decided to write a post about it and showing some examples and that way maybe I’ll remember in the future…
getimagesize() is one of those classic PHP functions which illustrates how bad the naming convention for PHP functions is. Usually words if function names in PHP are separate by and underscore (e.g. it would be get_image_size()) but this is one of those functions that doesn’t. Anyway, I digress…

Pass getimagesize() a filename and it will return an array containing the image’s width, height, what type of image it is, a text value with the width and height which can be placed directly into an <img> tag and some other stuff detailed below.

For example, if we had a JPEG image called “my-great-photo.jpg” and it was 300 pixels width and 200 pixels high, you could get the image information from it like so:

$info = getimagesize("my-great-photo.jpg");

Doing print_r($info) would return this, for the above example:

Array
(
    [0] => 300
    [1] => 200
    [2] => 2
    [3] => width="300" height="200"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

The value in index 0 is the width.

The value in index 1 is the height.

The value in index 2 is one of the IMAGETYPE_XXX constants, such as IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG and IMAGETYPE_BMP.

The value in index 3 is the text string that can be add into an <img> tag.

The ‘bits’ value indicates the number of bits for each color.

The ‘channels’ value is either 3 for RGB or 4 for CMYK.

The ‘mime’ value is the image’s mime type and can be used in an HTTP Content-type header.

If the file cannot be opened or parsed, the getimagesize() function will return false. If accessing the filename  image is impossible, or if it isn’t a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE.

Use GetImageSize function in ordert to get image size in php.

Syntax: array getimagesize ( string filename [, array &imageinfo] )

It returns

  • image width
  • image height
  • image type; 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
  • image size string to use in html <img> tag (sample: height=”100″ width=”50″)
  • some other image parameters, starting PHP 4.3.0

Cut from the php manual:
The optional imageinfo parameter allows you to extract some extended information from the image file. Currently, this will return the different JPG APP markers as an associative array. Some programs use these APP markers to embed text information in images. A very common one is to embed IPTC  http://www.iptc.org/ information in the APP13 marker. You can use the iptcparse()  function to parse the binary APP13 marker into something readable.

Code:
<?php
  $size = GetImageSize("my-picture.jpg", $info);
  if (isset($info["APP13"])) {
    $iptc = iptcparse($info["APP13"]);
    var_dump($iptc);
  }
  $img_x = $size[0];
  $img_y = $size[1];
  $img_type  = $size[2];
  $img_sizes = $size[3];
  $channels = $size['channels'];
  $bits = $size['bits'];
  $mime = $size['mime']; // delivers correct HTTP Content-type to be user in headers
  // sample:  header("Content-type: {$size['mime']}");

?>

Related posts:

Advertisements
By Rz Rasel Posted in Php

3 comments on “Get An Image Size With PHP

  1. Hello! My spouse and I often publish guest articles or blog posts for other website owners to help increase exposure to our work, as well as provide superb articles to site owners. It’s a win win situation! If you happen to be interested feel free to e-mail me at: Bottella2334@gmail.com so we can communicate further. Thanks alot :)!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s