getimagesize
getimagesize(string $filename, array &$image_info = null): array|false
Image size detection: reads the file header (e.g. the PNG IHDR chunk) for width/height/MIME without decoding the whole image and without requiring GD — hand-rolled header parsing is the shared soul of the ten-language demo.
C C23
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main(void) {
/* Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets */
unsigned char png[33] = {0}; /* zero-init: partial writes + reads, uninitialized stack is UB */
memcpy(png, "\x89PNG\r\n\x1a\n", 8);
memcpy(png + 8, "\x00\x00\x00\x0dIHDR", 8);
png[19] = 0x02; // width 2 (offset 16..19, big-endian)
png[23] = 0x03; // height 3 (offset 20..23, big-endian)
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
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
int main() {
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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; // width 2 (offset 16..19, big-endian)
png[23] = 0x03; // height 3 (offset 20..23, big-endian)
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
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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; // width 2 (offset 16..19, big-endian)
png[23] = 0x03; // height 3 (offset 20..23, big-endian)
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
package main
import (
"encoding/binary"
"fmt"
)
func main() {
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
png := append([]byte("\x89PNG\r\n\x1a\n"), []byte("\x00\x00\x00\x0dIHDR")...)
png = binary.BigEndian.AppendUint32(png, 2) // width
png = binary.BigEndian.AppendUint32(png, 3) // height
png = append(png, "\x08\x06\x00\x00\x00"...)
png = append(png, "\x00\x00\x00\x00"...) // CRC placeholder
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
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Main {
public static void main(String[] args) {
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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]), // width 2
Buffer.from([0x00, 0x00, 0x00, 0x03]), // height 3
Buffer.from([0x08, 0x06, 0x00, 0x00, 0x00]),
Buffer.from([0x00, 0x00, 0x00, 0x00]), // CRC placeholder
]);
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
// getimagesize reads the IHDR chunk; width/height are big-endian at fixed offsets
$png = "\x89PNG\r\n\x1a\n"
. "\x00\x00\x00\x0dIHDR"
. pack("N", 2) . pack("N", 3) // width 2, height 3
. "\x08\x06\x00\x00\x00"
. "\x00\x00\x00\x00"; // CRC placeholder: the header read does not verify it
[$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
# Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
import struct
png = (
b"\x89PNG\r\n\x1a\n"
+ b"\x00\x00\x00\x0dIHDR"
+ struct.pack(">II", 2, 3) # width 2, height 3
+ b"\x08\x06\x00\x00\x00"
+ b"\x00\x00\x00\x00" # CRC placeholder: header parsing does not verify it
)
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
fn main() {
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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()); // width
png.extend_from_slice(&3u32.to_be_bytes()); // height
png.extend_from_slice(&[0x08, 0x06, 0x00, 0x00, 0x00]);
png.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // CRC placeholder
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
// Hand-rolled PNG header parse: IHDR width/height sit at fixed big-endian offsets
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]), // width 2
Buffer.from([0x00, 0x00, 0x00, 0x03]), // height 3
Buffer.from([0x08, 0x06, 0x00, 0x00, 0x00]),
Buffer.from([0x00, 0x00, 0x00, 0x00]), // CRC placeholder
]);
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
Language differences
PHP: getimagesize/getimagesizefromstring live in ext/standard (the manual states GD is not required) and return width/height/IMAGETYPE/MIME/IMG attribute string by reading header chunks such as IHDR; detection is not validation — the manual Caution explicitly forbids using getimagesize to verify that a file is a valid image (use Fileinfo for that). The string variant getimagesizefromstring avoids temp files: the demo builds the PNG header inline (offsets 16/20, big-endian).
Python: struct hand-parsing (direct counterpart!) plus PIL/Pillow (Image.open(...).size).
Go: the standard library image.DecodeConfig reads only the header without decoding the whole image (after image/png registration); the demo keeps the hand-rolled teaching version.
JavaScript/TypeScript: Node Buffer.readUInt32BE with manual offsets (in browsers use Image/naturalWidth).
Rust: hand-rolled plus the image crate (png::Decoder reads the header); Java: ImageIO.read(...).getWidth() or a hand-rolled ByteBuffer; C#: System.Drawing.Image.FromFile or BinaryPrimitives by hand.
C/C++: no standard-library image support — the 24-byte hand-rolled header parse is the soul of this entry (8-byte signature + 4-byte length + 4-byte type + 4-byte width + 4-byte height).