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

Tutoriel PHP de base

Tutoriel PHP avancé

PHP & MySQL

Manuel de référence PHP

Utilisation et exemple de la fonction php getimagesize () pour obtenir des informations sur l'image

Traitement des images PHP

La fonction getimagesize() est utilisée pour obtenir la taille de l'image et d'autres informations. En cas de succès, elle retourne un tableau, en cas d'échec, elle retourne FALSE et génère un message d'erreur de niveau E_WARNING.

Format de syntaxe :

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

La fonction getimagesize() détermine toute image GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2JPX, JB2La taille des fichiers d'images JPC, XBM ou WBMP est déterminée et les dimensions de l'image, le type de fichier et la hauteur et la largeur de l'image sont retournés.

示例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://fr.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");

Traitement des images PHP