English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriais Básicos PHP

Tutoriais Avançados PHP

PHP & MySQL

Manual de Referência PHP

Uso e exemplo da função php getimagesize () para obter informações de imagem

Processamento de Imagem PHP

A função getimagesize() é usada para obter o tamanho da imagem e informações relacionadas, retornando um array com sucesso e FALSE em caso de falha, gerando uma mensagem de erro de nível E_WARNING.

Formato de sintaxe:

array getimagesize ( string $filename [, array &$imageinfo ] )

A função getimagesize() determina qualquer GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2JPX, JB2Tamanho do arquivo de imagem JPC, XBM ou WBMP e retorno das dimensões da imagem, tipo de arquivo e altura e largura da imagem.

示例1:本地图片文件

<?php
list($width, $height, $type, $attr) = getimagesize("w3codebox-logo.png");
echo "宽度为:" . $width;
echo "高度为:" . $height;
echo "类型为:" . $attr;
?>

以上示例输出结果为:

宽度为:290
高度为:69
类型为:3
属性:width="290" height="69"

示例2:远程图片文件

<?php
$remote_png_url = 'http://pt.oldtoolbag.com/wp-content/themes/oldtoolbag.com/assets/img/logo-domain-green2.png';
$img_data = getimagesize($remote_png_url);
print_r($img_data );
?>

以上示例输出结果为:

Array
(
    [0] => 290
    [1] => 69
    [2] => 3
    [3] => width="290" height="69"
    [bits] => 8
    [mime] => image/png
)

返回结果说明

  • 索引 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
  • 索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签
  • 索引 bits 给出的是图像的每种颜色的位数,二进制格式
  • 索引 channels 给出的是图像的通道值,RGB 图像默认是 3
  • 索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如: header("Content-type: image/jpeg");

Processamento de Imagem PHP