<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Get the size of an image</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.gd-info.html">gd_info</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getimagesizefromstring.html">getimagesizefromstring</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.getimagesize" class="refentry"> <div class="refnamediv"> <h1 class="refname">getimagesize</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">getimagesize</span> — <span class="dc-title">Get the size of an image</span></p> </div> <div class="refsect1 description" id="refsect1-function.getimagesize-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>getimagesize</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$imageinfo</code></span> ] )</div> <p class="para rdfs-comment"> The <span class="function"><strong>getimagesize()</strong></span> function will determine the size of any given image file and return the dimensions along with the file type and a <em>height/width</em> text string to be used inside a normal <acronym title="Hyper Text Markup Language">HTML</acronym> <code class="tag">IMG</code> tag and the correspondant <acronym title="Hypertext Transfer Protocol">HTTP</acronym> content type. </p> <p class="para"> <span class="function"><strong>getimagesize()</strong></span> can also return some more information in <em><code class="parameter">imageinfo</code></em> parameter. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Note that JPC and JP2 are capable of having components with different bit depths. In this case, the value for "bits" is the highest bit depth encountered. Also, JP2 files may contain <em>multiple JPEG 2000 codestreams</em>. In this case, <span class="function"><strong>getimagesize()</strong></span> returns the values for the first codestream it encounters in the root of the file. </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> The information about icons are retrieved from the icon with the highest bitrate. </span> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.getimagesize-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">filename</code></em></span> <dd> <p class="para"> This parameter specifies the file you wish to retrieve information about. It can reference a local file or (configuration permitting) a remote file using one of the supported streams. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">imageinfo</code></em></span> <dd> <p class="para"> This optional parameter allows you to extract some extended information from the image file. Currently, this will return the different <acronym>JPG</acronym> 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 <a href="http://www.iptc.org/" class="link external">» IPTC</a> information in the APP13 marker. You can use the <span class="function"><a href="function.iptcparse.html" class="function">iptcparse()</a></span> function to parse the binary APP13 marker into something readable. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.getimagesize-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array with up to 7 elements. Not all image types will include the <em>channels</em> and <em>bits</em> elements. </p> <p class="para"> Index 0 and 1 contains respectively the width and the height of the image. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Some formats may contain no image or may contain multiple images. In these cases, <span class="function"><strong>getimagesize()</strong></span> might not be able to properly determine the image size. <span class="function"><strong>getimagesize()</strong></span> will return zero for width and height in these cases. </p> </p></blockquote> <p class="para"> Index 2 is one of the <em>IMAGETYPE_XXX</em> constants indicating the type of the image. </p> <p class="para"> Index 3 is a text string with the correct <em>height="yyy" width="xxx"</em> string that can be used directly in an <acronym>IMG</acronym> tag. </p> <p class="para"> <em>mime</em> is the correspondant MIME type of the image. This information can be used to deliver images with the correct HTTP <em>Content-type</em> header: <div class="example" id="example-3170"> <p><strong>Example #1 <span class="function"><strong>getimagesize()</strong></span> and MIME types</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"rb"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$size </span><span style="color: #007700">&& </span><span style="color: #0000BB">$fp</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-type: </span><span style="color: #007700">{</span><span style="color: #0000BB">$size</span><span style="color: #007700">[</span><span style="color: #DD0000">'mime'</span><span style="color: #007700">]}</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">fpassthru</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /> exit;<br />} else {<br /> </span><span style="color: #FF8000">// error<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <em>channels</em> will be 3 for RGB pictures and 4 for CMYK pictures. </p> <p class="para"> <em>bits</em> is the number of bits for each color. </p> <p class="para"> For some image types, the presence of <em>channels</em> and <em>bits</em> values can be a bit confusing. As an example, <acronym title="Graphic Interchange Format">GIF</acronym> always uses 3 channels per pixel, but the number of bits per pixel cannot be calculated for an animated <acronym title="Graphic Interchange Format">GIF</acronym> with a global color table. </p> <p class="para"> On failure, <strong><code>FALSE</code></strong> is returned. </p> </div> <div class="refsect1 errors" id="refsect1-function.getimagesize-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If accessing the <em><code class="parameter">filename</code></em> image is impossible <span class="function"><strong>getimagesize()</strong></span> will generate an error of level <strong><code>E_WARNING</code></strong>. On read error, <span class="function"><strong>getimagesize()</strong></span> will generate an error of level <strong><code>E_NOTICE</code></strong>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.getimagesize-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> Added icon support. </td> </tr> <tr> <td>5.2.3</td> <td> Read errors generated by this function downgraded to <strong><code>E_NOTICE</code></strong> from <strong><code>E_WARNING</code></strong>. </td> </tr> <tr> <td>4.3.2</td> <td> Support for <acronym title="Japan Picture">JPC</acronym>, <acronym title="JPEG-2000">JP2</acronym>, <acronym>JPX</acronym>, <acronym title="Joint Bilevel Image Experts Group">JB2</acronym>, <acronym title="X Bitmap">XBM</acronym>, and <acronym title="Wireless Bitmap">WBMP</acronym> became available. </td> </tr> <tr> <td>4.3.2</td> <td> JPEG 2000 support was added for the <em><code class="parameter">imageinfo</code></em> parameter. </td> </tr> <tr> <td>4.3.0</td> <td> <em>bits</em> and <em>channels</em> are present for other image types, too. </td> </tr> <tr> <td>4.3.0</td> <td> <em>mime</em> was added. </td> </tr> <tr> <td>4.3.0</td> <td> Support for <acronym>SWC</acronym> and <acronym title="Interchange File Format">IFF</acronym> was added. </td> </tr> <tr> <td>4.2.0</td> <td> Support for <acronym title="Tagged Image File Format">TIFF</acronym> was added. </td> </tr> <tr> <td>4.0.6</td> <td> Support for <acronym title="Bitmap">BMP</acronym> and <acronym title="Photoshop Document">PSD</acronym> was added. </td> </tr> <tr> <td>4.0.5</td> <td> URL support was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.getimagesize-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3171"> <p><strong>Example #2 <span class="function"><strong>getimagesize()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">list(</span><span style="color: #0000BB">$width</span><span style="color: #007700">, </span><span style="color: #0000BB">$height</span><span style="color: #007700">, </span><span style="color: #0000BB">$type</span><span style="color: #007700">, </span><span style="color: #0000BB">$attr</span><span style="color: #007700">) = </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"img/flag.jpg"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"<img src=\"img/flag.jpg\" </span><span style="color: #0000BB">$attr</span><span style="color: #DD0000"> alt=\"getimagesize() example\" />"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3172"> <p><strong>Example #3 getimagesize (URL)</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/gifs/logo.gif"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// if the file name has space in it, encode it properly<br /></span><span style="color: #0000BB">$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/gifs/lo%20go.gif"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-3173"> <p><strong>Example #4 getimagesize() returning IPTC</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$size </span><span style="color: #007700">= </span><span style="color: #0000BB">getimagesize</span><span style="color: #007700">(</span><span style="color: #DD0000">"testimg.jpg"</span><span style="color: #007700">, </span><span style="color: #0000BB">$info</span><span style="color: #007700">);<br />if (isset(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">"APP13"</span><span style="color: #007700">])) {<br /> </span><span style="color: #0000BB">$iptc </span><span style="color: #007700">= </span><span style="color: #0000BB">iptcparse</span><span style="color: #007700">(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">"APP13"</span><span style="color: #007700">]);<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$iptc</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.getimagesize-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">This function does not require the GD image library.</p></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.getimagesize-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.image-type-to-mime-type.html" class="function" rel="rdfs-seeAlso">image_type_to_mime_type()</a> - Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype</span></li> <li class="member"> <span class="function"><a href="function.exif-imagetype.html" class="function" rel="rdfs-seeAlso">exif_imagetype()</a> - Determine the type of an image</span></li> <li class="member"> <span class="function"><a href="function.exif-read-data.html" class="function" rel="rdfs-seeAlso">exif_read_data()</a> - Reads the EXIF headers from JPEG or TIFF</span></li> <li class="member"> <span class="function"><a href="function.exif-thumbnail.html" class="function" rel="rdfs-seeAlso">exif_thumbnail()</a> - Retrieve the embedded thumbnail of a TIFF or JPEG image</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.gd-info.html">gd_info</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getimagesizefromstring.html">getimagesizefromstring</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>