getimagesize

getimagesize(string $filename, array &$image_info = null): array|false

图片尺寸探测:读文件头(如 PNG 的 IHDR 块)即得宽高/MIME,无需解码整图、不依赖 GD——手写头解析是十语言共同的灵魂演示。

C C23

C23
#include <stdint.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    /* 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序 */
    unsigned char png[33] = {0}; /* 全量零初始化:部分写入后读取,未初始化栈内存是 UB */
    memcpy(png, "\x89PNG\r\n\x1a\n", 8);
    memcpy(png + 8, "\x00\x00\x00\x0dIHDR", 8);
    png[19] = 0x02; // 宽 2(偏移 16..19,大端序)
    png[23] = 0x03; // 高 3(偏移 20..23,大端序)
    memcpy(png + 24, "\x08\x06\x00\x00\x00\x00\x00\x00\x00", 9);
    uint32_t w = (uint32_t)png[16] << 24 | (uint32_t)png[17] << 16
               | (uint32_t)png[18] << 8 | png[19];
    uint32_t h = (uint32_t)png[20] << 24 | (uint32_t)png[21] << 16
               | (uint32_t)png[22] << 8 | png[23];
    printf("%ux%u\n", w, h);
    printf("%s\n", (w == 2 && h == 3) ? "parsed ok" : "parsed wrong");
    return 0;
}
输出
2x3
parsed ok

C++ C++26

C++26
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>

int main() {
    // 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
    std::array<unsigned char, 33> png = {};
    const unsigned char head[8] = {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a};
    std::copy(head, head + 8, png.begin());
    const char *ihdr = "\x00\x00\x00\x0dIHDR";
    std::copy(ihdr, ihdr + 8, png.begin() + 8);
    png[19] = 0x02; // 宽 2(偏移 16..19,大端序)
    png[23] = 0x03; // 高 3(偏移 20..23,大端序)
    uint32_t w = (uint32_t)png[16] << 24 | (uint32_t)png[17] << 16
               | (uint32_t)png[18] << 8 | png[19];
    uint32_t h = (uint32_t)png[20] << 24 | (uint32_t)png[21] << 16
               | (uint32_t)png[22] << 8 | png[23];
    std::printf("%ux%u\n", w, h);
    std::printf("%s\n", (w == 2 && h == 3) ? "parsed ok" : "parsed wrong");
    return 0;
}
输出
2x3
parsed ok

C# v11.0.100

v11.0.100
// 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
byte[] head = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52 };
byte[] png = new byte[33];
Array.Copy(head, png, head.Length);
png[19] = 0x02; // 宽 2(偏移 16..19,大端序)
png[23] = 0x03; // 高 3(偏移 20..23,大端序)
uint width = System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(png.AsSpan(16));
uint height = System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(png.AsSpan(20));
Console.WriteLine($"{width}x{height}");
Console.WriteLine((width == 2 && height == 3) ? "parsed ok" : "parsed wrong");
输出
2x3
parsed ok

Go v1.27.4

v1.27.4
package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	// 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
	png := append([]byte("\x89PNG\r\n\x1a\n"), []byte("\x00\x00\x00\x0dIHDR")...)
	png = binary.BigEndian.AppendUint32(png, 2) // 宽
	png = binary.BigEndian.AppendUint32(png, 3) // 高
	png = append(png, "\x08\x06\x00\x00\x00"...)
	png = append(png, "\x00\x00\x00\x00"...) // CRC 占位
	width := binary.BigEndian.Uint32(png[16:20])
	height := binary.BigEndian.Uint32(png[20:24])
	fmt.Printf("%dx%d\n", width, height)
	if width == 2 && height == 3 {
		fmt.Println("parsed ok")
	} else {
		fmt.Println("parsed wrong")
	}
}
输出
2x3
parsed ok

Java v27.0.1

v27.0.1
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static void main(String[] args) {
        // 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
        byte[] png = new byte[33];
        byte[] head = {(byte) 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
        System.arraycopy(head, 0, png, 0, 8);
        byte[] ihdr = {0, 0, 0, 0x0d, 0x49, 0x48, 0x44, 0x52};
        System.arraycopy(ihdr, 0, png, 8, 8);
        ByteBuffer.wrap(png).order(ByteOrder.BIG_ENDIAN).putInt(16, 2).putInt(20, 3);
        int width = ByteBuffer.wrap(png).order(ByteOrder.BIG_ENDIAN).getInt(16);
        int height = ByteBuffer.wrap(png).order(ByteOrder.BIG_ENDIAN).getInt(20);
        System.out.println(width + "x" + height);
        System.out.println((width == 2 && height == 3) ? "parsed ok" : "parsed wrong");
    }
}
输出
2x3
parsed ok

JavaScript v26.13.1

v26.13.1
// 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
const png = Buffer.concat([
  Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
  Buffer.from([0x00, 0x00, 0x00, 0x0d]),
  Buffer.from("IHDR", "latin1"),
  Buffer.from([0x00, 0x00, 0x00, 0x02]), // 宽 2
  Buffer.from([0x00, 0x00, 0x00, 0x03]), // 高 3
  Buffer.from([0x08, 0x06, 0x00, 0x00, 0x00]),
  Buffer.from([0x00, 0x00, 0x00, 0x00]), // CRC 占位
]);
const width = png.readUInt32BE(16);
const height = png.readUInt32BE(20);
console.log(`${width}x${height}`);
console.log(width === 2 && height === 3 ? "parsed ok" : "parsed wrong");
输出
2x3
parsed ok

PHP v8.6.0

v8.6.0
// getimagesize 读取 IHDR 块;宽高在固定偏移处、大端序
$png = "\x89PNG\r\n\x1a\n"
    . "\x00\x00\x00\x0dIHDR"
    . pack("N", 2) . pack("N", 3) // 宽 2、高 3
    . "\x08\x06\x00\x00\x00"
    . "\x00\x00\x00\x00";         // CRC 占位:读取头部时不校验它
[$width, $height, $type, $attr] = getimagesizefromstring($png);
echo "{$width}x{$height}\n";
echo "type const: $type\n";
echo $attr, "\n";
输出
2x3
type const: 3
width="2" height="3"

Python v3.15.2

v3.15.2
# 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
import struct

png = (
    b"\x89PNG\r\n\x1a\n"
    + b"\x00\x00\x00\x0dIHDR"
    + struct.pack(">II", 2, 3)  # 宽 2、高 3
    + b"\x08\x06\x00\x00\x00"
    + b"\x00\x00\x00\x00"  # CRC 占位:解析头部时不校验它
)
width, height = struct.unpack(">II", png[16:24])
print(f"{width}x{height}")
print("parsed ok" if (width, height) == (2, 3) else "parsed wrong")
输出
2x3
parsed ok

Rust v1.99.1

v1.99.1
fn main() {
    // 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
    let mut png: Vec<u8> = vec![0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
    png.extend_from_slice(&[0x00, 0x00, 0x00, 0x0d]);
    png.extend_from_slice(b"IHDR");
    png.extend_from_slice(&2u32.to_be_bytes()); // 宽
    png.extend_from_slice(&3u32.to_be_bytes()); // 高
    png.extend_from_slice(&[0x08, 0x06, 0x00, 0x00, 0x00]);
    png.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // CRC 占位
    let width = u32::from(png[16]) << 24 | u32::from(png[17]) << 16
        | u32::from(png[18]) << 8 | u32::from(png[19]);
    let height = u32::from(png[20]) << 24 | u32::from(png[21]) << 16
        | u32::from(png[22]) << 8 | u32::from(png[23]);
    println!("{}x{}", width, height);
    println!("{}", if (width, height) == (2, 3) { "parsed ok" } else { "parsed wrong" });
}
输出
2x3
parsed ok

TypeScript v26.13.1

v26.13.1
// 手写 PNG 头解析:IHDR 宽高在固定偏移处、大端序
const png: Buffer = Buffer.concat([
  Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
  Buffer.from([0x00, 0x00, 0x00, 0x0d]),
  Buffer.from("IHDR", "latin1"),
  Buffer.from([0x00, 0x00, 0x00, 0x02]), // 宽 2
  Buffer.from([0x00, 0x00, 0x00, 0x03]), // 高 3
  Buffer.from([0x08, 0x06, 0x00, 0x00, 0x00]),
  Buffer.from([0x00, 0x00, 0x00, 0x00]), // CRC 占位
]);
const width: number = png.readUInt32BE(16);
const height: number = png.readUInt32BE(20);
console.log(`${width}x${height}`);
console.log(width === 2 && height === 3 ? "parsed ok" : "parsed wrong");
输出
2x3
parsed ok

语言差异说明

PHP:getimagesize/getimagesizefromstring 属 ext/standard(手册明示不需要 GD),读 IHDR 等头块即返回宽高/IMAGETYPE/MIME/IMG 属性串;探测不等于校验——手册 Caution 明令不要用 getimagesize 验证图片合法性(用途应为 Fileinfo)。字符串版 getimagesizefromstring 免写临时文件,示例直接内嵌构造 PNG 头(偏移 16/20、大端)。

Python:struct 手写解析(对应!)+ PIL/Pillow 对照(Image.open(...).size)。

Go:标准库 image.DecodeConfig 只读头即得尺寸不解整图(image/png 注册后);本示例保留手写教学版。

JavaScript/TypeScript:Node Buffer.readUInt32BE 手写偏移(浏览器侧则用 Image/naturalWidth)。

Rust:手写 + image crate(png::Decoder 读 header);Java:ImageIO.read(...).getWidth() 或手写 ByteBuffer;C#:System.Drawing.Image.FromFile 或 BinaryPrimitives 手写。

C/C++:无标准库图片支持——24 字节头手写解析是本条灵魂(签名 8 字节 + 长度 4 + 类型 4 + 宽高各 4)。