function.getimagesize.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 328 行
HTML
328 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Get the size of an image</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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.image-type-to-extension.html">image_type_to_extension</a></div> <div class="up"><a href="ref.image.html">GD 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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><b><b>getimagesize</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span> [, <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$imageinfo</tt></span> ] )</div> <p class="para rdfs-comment"> The <b>getimagesize()</b> function will determine the size of any given image file and return the dimensions along with the file type and a height/width 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"> <b>getimagesize()</b> can also return some more information in <i><tt class="parameter">imageinfo</tt></i> parameter. </p> <blockquote><p><b class="note">Note</b>: <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 multiple JPEG 2000 codestreams. In this case, <b>getimagesize()</b> returns the values for the first codestream it encounters in the root of the file. </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> The information about icons are retreived from the icon with the highest bitrate. </span> </p></blockquote> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">filename</tt></i></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"><i><tt class="parameter">imageinfo</tt></i></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 <a href="function.iptcparse.html" class="function">iptcparse()</a> function to parse the binary APP13 marker into something readable. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an array with 7 elements. </p> <p class="para"> Index 0 and 1 contains respectively the width and the height of the image. </p> <blockquote><p><b class="note">Note</b>: Some formats may contain no image or may contain multiple images. In these cases, <b>getimagesize()</b> might not be able to properly determine the image size. <b>getimagesize()</b> will return zero for width and height in these cases. <br /> </p></blockquote> <p class="para"> Index 2 is one of the <i>IMAGETYPE_XXX</i> constants indicating the type of the image. </p> <p class="para"> Index 3 is a text string with the correct <i>height="yyy" width="xxx"</i> string that can be used directly in an IMG tag. </p> <p class="para"> <i>mime</i> is the correspondant MIME type of the image. This information can be used to deliver images with correct the HTTP <i>Content-type</i> header: <div class="example"> <p><b>Example #1 getimagesize() and MIME types</b></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: {$size['mime']}"</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"> <i>channels</i> will be 3 for RGB pictures and 4 for CMYK pictures. </p> <p class="para"> <i>bits</i> is the number of bits for each color. </p> <p class="para"> For some image types, the presence of <i>channels</i> and <i>bits</i> 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, <b><tt>FALSE</tt></b> is returned. </p> </div> <div class="refsect1 errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> If accessing the <i><tt class="parameter">filename</tt></i> image is impossible, or if it isn't a valid picture, <b>getimagesize()</b> will generate an error of level <b><tt>E_WARNING</tt></b>. On read error, <b>getimagesize()</b> will generate an error of level <b><tt>E_NOTICE</tt></b>. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.3.0</td> <td colspan="1" rowspan="1" align="left"> Added icon support. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.2.3</td> <td colspan="1" rowspan="1" align="left"> Read errors generated by this function downgraded to <b><tt>E_NOTICE</tt></b> from <b><tt>E_WARNING</tt></b>. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.2</td> <td colspan="1" rowspan="1" align="left"> 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 valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.2</td> <td colspan="1" rowspan="1" align="left"> JPEG 2000 support was added for the <i><tt class="parameter">imageinfo</tt></i> parameter. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> <i>bits</i> and <i>channels</i> are present for other image types, too. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> <i>mime</i> was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> Support for <acronym>SWC</acronym> was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.2.0</td> <td colspan="1" rowspan="1" align="left"> Support for <acronym title="Tagged Image File Format">TIFF</acronym> was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.5</td> <td colspan="1" rowspan="1" align="left"> URL support was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #2 getimagesize (file)</b></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\" $attr alt=\"getimagesize() example\" />"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <div class="example"> <p><b>Example #3 getimagesize (URL)</b></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"> <p><b>Example #4 getimagesize() returning IPTC</b></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"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: The <b>getimagesize()</b> function does not require the GD image library. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.image-type-to-mime-type.html" class="function" rel="rdfs-seeAlso">image_type_to_mime_type()</a></li> <li class="member"><a href="function.exif-imagetype.html" class="function" rel="rdfs-seeAlso">exif_imagetype()</a></li> <li class="member"><a href="function.exif-read-data.html" class="function" rel="rdfs-seeAlso">exif_read_data()</a></li> <li class="member"><a href="function.exif-thumbnail.html" class="function" rel="rdfs-seeAlso">exif_thumbnail()</a></li> </ul> </p> </div></div><hr /><div 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.image-type-to-extension.html">image_type_to_extension</a></div> <div class="up"><a href="ref.image.html">GD Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?