getimagesize() - gd函数(图像处理)
getimagesize()
(PHP 4, PHP 5, PHP 7)
取得图像大小
说明
getimagesize(string $filename[,array &$imageinfo]): arraygetimagesize()函数将测定任何GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM或WBMP图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通HTML文件中IMG
标记中的 height/width 文本字符串。
如果不能访问$filename指定的图像或者其不是有效的图像,getimagesize()将返回FALSE
并产生一条E_WARNING级的错误。
对JPC,JP2,JPX,JB2,XBM和WBMP的支持自 PHP 4.3.2 起可用。对SWC的支持自 PHP 4.3.0 起可用。对TIFF的支持是 PHP 4.2.0 添加的。Note: JPEG 2000 支持是 PHP 4.3.2 添加的。注意 JPC 和 JP2 可以有不同的色彩深度的成分。此情况下,“bits”的值是碰到的最高的位深度。此外,JP2 文件可能包含有多个 JPEG 2000 代码流,此情况下,getimagesize()返回此文件顶层中碰到的第一个代码流的值。
Note:本函数不需要 GD 图像库。
返回一个具有四个单元的数组。索引 0 包含图像宽度的像素值,索引 1 包含图像高度的像素值。索引 2 是图像类型的标记: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。这些标记与 PHP 4.3.0 新加的 IMAGETYPE 常量对应。索引 3 是文本字符串,内容为“height="yyy" width="xxx"”,可直接用于 IMG 标记。
getimagesize(文件)
URL 支持是 PHP 4.0.5 添加的。
getimagesize(URL)
对于JPG图像,还会多返回两个索引:channels和bits。channels对于 RGB 图像其值为 3,对于 CMYK 图像其值为 4。bits是每种颜色的位数。
自 PHP 4.3.0 起,bits和channels对于其它图像类型也存在。但是这些值可能会把人搞糊涂。例如,GIF总是对每个像素使用 3 个 channel,但是对于动画GIF来说每个像素的位数无法通过全局颜色表计算出来。
某些格式可能不包含图像或者包含多个图像。此种情况下,getimagesize()可能不能用来准确测定图像的大小。此时getimagesize()将返回零作为宽度和高度。
自 PHP 4.3.0 起,getimagesize()还会返回额外的参数mime,符合该图像的 MIME 类型。此信息可以用来在 HTTP Content-type 头信息中发送正确的信息:
getimagesize()和 MIME 类型
Note that, if you're going to be a good programmer and use named constatnts (IMAGETYPE_JPEG) rather than their values (2), you want to use the IMAGETYPE variants - IMAGETYPE_JPEG, IMAGETYPE GIF, IMAGETYPE_PNG, etc. For some reason, somebody made a horrible decision, and IMG_PNG is actually 4 in my version of PHP, while IMAGETYPE_PNG is 3. It took me a while to figure out why comparing the type against IMG_PNG was failing...
It's always good to check out an image's dimensions while attempting to upload to your server or database...especially if it's going to be displayed on a page that doesn't accomodate images beyond a particular size.
I wanted to use getimagesize() on .SWF files stored in the database as blob data and couldn't find a simple solution, so I created my own. I am releasing this code under the MIT license to save everyone some time:
Could be useful (didn´t know where to post it): function getImageErrors( $filename, $type = "", $minWidth = 0, $minHeight = 0, $maxWidth = 0, $maxHeight = 0, $maxFileSize = 0 ) { $errors = array(); if ( file_exists( $filename ) ) { $ending = substr( $filename, strpos( $filename, "." ) ); if ( is_array( $type ) ) { $isTypeOf = false; foreach( $type as $eachtype ) { if ( $ending == $eachtype ) { $isTypeOf = true; } } if ( ! $isTypeOf ) { $errors[ 'type' ] = $ending; } } elseif ( $type != "" ) { if ( $ending != $type ) { $errors[ 'type' ] = $ending; } } $size = getimagesize( $filename ); if ( $size[ 0 ] $minWidth ) && ( $size[ 0 ] > $maxWidth ) ) { $errors[ 'maxWidth' ] = $size[ 0 ]; } if ( ( $maxHeight > $minHeight ) && ( $size[ 1 ] > $maxHeight ) ) { $errors[ 'maxHeight' ] = $size[ 1 ]; } if ( ( $maxFileSize > 0 ) && ( filesize( $filename ) > $maxFileSize ) ) { $errors[ 'maxFileSize' ] = filesize( $filename ); } } else { $errors[ 'filename' ] = "not existing"; } return ( count( $errors ) > 0 ? $errors : null ); }
This function returns the width and height of a JPEG image from a string, allowing the dimensions of images stored in a database to be retrieved without writing them to the disk first, or using "imagecreatefromstring" which is very slow in comparison. Doing this 10,000 times takes 0.43 seconds, compared with using imagecreatefromstring/imagesx/imagesy which takes around 1.52 seconds to do the same. Do not use this instead of getimagesize when dealing with files, getimagesize is much faster coming in at 0.15 seconds.
i made function img_resize($path,$tmp_name,$new_name,$new_width) this could be useful.
Here is the function which determines whether the PNG image contains alpha or not: The color type of PNG image is stored at byte offset 25. Possible values of that 25'th byte is: * 0 - greyscale * 2 - RGB * 3 - RGB with palette * 4 - greyscale + alpha * 6 - RGB + alpha
Returns a array with 4 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: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. The 3 index contains ' height="yyy" width="xxx" '
Rather than making a lengthy function that essentially runs twice (once as width, once as height) I came up with a helpful function that uses variable variables to set a maximum height/width. Hope someone finds this helpful. function scaleimage($location, $maxw=NULL, $maxh=NULL){ $img = @getimagesize($location); if($img){ $w = $img[0]; $h = $img[1]; $dim = array('w','h'); foreach($dim AS $val){ $max = "max{$val}"; if(${$val} > ${$max} && ${$max}){ $alt = ($val == 'w') ? 'h' : 'w'; $ratio = ${$alt} / ${$val}; ${$val} = ${$max}; ${$alt} = ${$val} * $ratio; } } return(""); } }
There's a code snippet for getting JPEG image dimensions by getting only first few bytes of the file, but it doesn't work for PNG files, so I wrote one. It will download only the first 24 bytes instead of the whole image, and thus being much faster than getimagesize() and it will save bandwidth at the same time:
For some images, using getimagesize() without the second parameter will return the correct info, but when you add the second parameter it will return false. This is most likely a bug (and it has been reported as such), but meanwhile, if you encounter this problem, a workaround is to use exif_read_data().
When validating images, allways check both, image type *AND* file extension! Because most image types allow sections for comments or other irrelevant data. Those section can be used to infiltrate php code onto the server. If these files are stored as sent by the client, files with a ".php" extension can be executed and do tremendous harm.
A simple piece of code i wrote to proportionally resize an image to a max height and width then display it
Heres a easy way to scale images to thethat they are in *this is broken up so anyone can understand it :) I'm sorry for they other scripts, but I made one mistake about the image resizing... here is a working script !
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!