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

PHP Grundanleitung

PHP Fortgeschrittene Anleitung

PHP & MySQL

PHP Referenzhandbuch

Verwendung und Beispiel der Funktion php getimagesize () zur Erteilung von Bildinformationen

PHP-Bildverarbeitung

Die Funktion getimagesize() wird verwendet, um die Größe des Bildes und weitere Informationen zu erhalten. Bei Erfolg wird ein Array zurückgegeben, bei Misserfolg wird FALSE zurückgegeben und eine E_WARNING-Stufe Fehlermeldung erzeugt.

Syntax-Format:

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

Die Funktion getimagesize() bestimmt die Größe und weitere Informationen für alle GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2JPX, JB2Größe der JPC, XBM oder WBMP Bilddatei bestimmen und die Abmessungen des Bildes sowie den Dateityp und die Höhe und Breite der Bildgröße zurückgeben.

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

PHP-Bildverarbeitung