search
Homephp教程php手册PHP中如何用 getimagesize 函数获取图像信息

getimagesize() 函数用于获取图像尺寸,类型等信息。imagesx() 函数用于获取图像的宽度。imagesy() 函数用于获取图像的高度。

getimagesize() 函数用于获取图像大小及相关信息,成功返回一个数组,失败则返回 FALSE 并产生一条 E_WARNING 级的错误信息。

语法:

array getimagesize( string filename )

例子:

<?php
$array = getimagesize("images/flower_1.jpg");
print_r($array);
?>

浏览器显示如下:

Array
(
    [0] => 350
    [1] => 318
    [2] => 2
    [3] => width="350" height="318"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)

返回结果说明

1、索引 0 给出的是图像宽度的像素值

2、索引 1 给出的是图像高度的像素值

3、索引 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

4、索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 dc0870658837139040642baa5555a380 标签

5、索引 bits 给出的是图像的每种颜色的位数,二进制格式

6、索引 channels 给出的是图像的通道值,RGB 图像默认是 3

7、索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如:

header("Content-type: image/jpeg");

提示

如您要在自己的电脑上运行本教程中图像处理的例子,请将教程中用到的图片下载到本地 images 文件夹下备用:

imagesx() 函数用于获取图像的宽度,单位为像素,返回值为整型。

语法:

int imagesx( resource image )

参数为如 imagecreatetruecolor()、imagecreatefromjpeg() 等函数返回的图像资源。

imagesy() 函数用于获取图像的高度,语法及用法同 imagesx() 。

语法:

int imagesy( resource image )

例子:

<?php
$img = imagecreatefromjpeg("images/flower_1.jpg");
echo "图像宽度:",imagesx( $img ),"<br />";
echo "图像高度:",imagesy( $img );
?>

浏览器输出:

图像宽度:350
图像高度:318

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools