getimagesize

(PHP 4, PHP 5, PHP 7, PHP 8)

getimagesize β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ Ρ€Π°Π·ΠΌΠ΅Ρ€ изобраТСния

ОписаниС

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

Ѐункция getimagesize() опрСдСляСт Ρ€Π°Π·ΠΌΠ΅Ρ€ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΠΎΠ³ΠΎ графичСского Ρ„Π°ΠΉΠ»Π° ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ изобраТСния, Ρ‚ΠΈΠΏ Ρ„Π°ΠΉΠ»Π° ΠΈ Ρ‚Π΅ΠΊΡΡ‚ΠΎΠ²ΡƒΡŽ строку height/width, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ Π² Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Π°Ρ… HTML-Ρ‚Π΅Π³Π° IMG ΠΈ Π² HTTP-Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ°Ρ… Ρ‚ΠΈΠΏΠ° содСрТимого.

Ѐункция getimagesize() Ρ‚Π°ΠΊΠΆΠ΅ ΡƒΠΌΠ΅Π΅Ρ‚ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Ρ‚ΡŒ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ свСдСния ΠΎΠ± ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ image_info.

ΠŸΡ€Π΅Π΄ΠΎΡΡ‚Π΅Ρ€Π΅ΠΆΠ΅Π½ΠΈΠ΅

Ѐункция ΠΎΠΆΠΈΠ΄Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ filename ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ Π½Π° ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ» изобраТСния. ΠŸΡ€ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡Π΅ ΠΏΡƒΡ‚ΠΈ Π½Π΅ Π½Π° Ρ„Π°ΠΉΠ» изобраТСния функция ΠΈΠ½ΠΎΠ³Π΄Π° Π½Π΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ опрСдСляСт Ρ„Π°ΠΉΠ» ΠΊΠ°ΠΊ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π½ΠΎ Π½ΠΎ массив содСрТит бСссмыслСнныС значСния.

Π€ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize() Π½Π΅ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΡΡŽΡ‚, прСдставляСт Π»ΠΈ собой Ρ„Π°ΠΉΠ» допустимоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. Для ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ графичСских Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‚ΡΡ Ρ€Π΅ΡˆΠ΅Π½ΠΈΡΠΌΠΈ Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ модуля Fileinfo, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Π°Π»ΠΈ для Ρ‚Π°ΠΊΠΈΡ… Ρ†Π΅Π»Π΅ΠΉ.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Π΅ части JPC- ΠΈ JP2-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ часто содСрТат ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ с Ρ€Π°Π·Π½ΠΎΠΉ Π³Π»ΡƒΠ±ΠΈΠ½ΠΎΠΉ Ρ†Π²Π΅Ρ‚Π°, Ρ‚ΠΎΠ³Π΄Π° элСмСнт "bits" содСрТит максимальноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠΈΠ»Π° функция. Π€Π°ΠΉΠ»Ρ‹ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ JP2 Ρ‚Π°ΠΊΠΆΠ΅ ΠΈΠ½ΠΎΠ³Π΄Π° содСрТат мноТСствСнныС ΠΊΠΎΠ΄ΠΎΠ²Ρ‹Π΅ ΠΏΠΎΡ‚ΠΎΠΊΠΈ multiple JPEG 2000 codestreams, Ρ‚ΠΎΠ³Π΄Π° функция getimagesize() Π²Π΅Ρ€Π½Ρ‘Ρ‚ значСния ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ° ΠΊΠΎΠ΄Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΎΠ±Π½Π°Ρ€ΡƒΠΆΠΈΡ‚ Π² ΠΊΠΎΡ€Π½Π΅ Ρ„Π°ΠΉΠ»Π°.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: БвСдСния ΠΎΠ± ΠΈΠΊΠΎΠ½ΠΊΠ°Ρ… ΠΈΠ·Π²Π»Π΅ΠΊΠ°ΡŽΡ‚ΡΡ ΠΈΠ· ΠΈΠΊΠΎΠ½ΠΊΠΈ с наибольшим Π±ΠΈΡ‚Ρ€Π΅ΠΉΡ‚ΠΎΠΌ.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π˜Π·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ GIF состоят ΠΈΠ· ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ»ΠΈ Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… ΠΊΠ°Π΄Ρ€ΠΎΠ², Π³Π΄Π΅ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΊΠ°Π΄Ρ€ ΠΈΠ½ΠΎΠ³Π΄Π° Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‡Π°ΡΡ‚ΡŒ изобраТСния. Π Π°Π·ΠΌΠ΅Ρ€ изобраТСния, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ возвращаСтся Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize(), ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΎΠ±Ρ‰ΠΈΠΉ Ρ€Π°Π·ΠΌΠ΅Ρ€, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ функция считала ΠΈΠ· дСскриптора логичСского экрана.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

filename

АргумСнт Π·Π°Π΄Π°Ρ‘Ρ‚ Ρ„Π°ΠΉΠ», свСдСния ΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ трСбуСтся ΠΈΠ·Π²Π»Π΅Ρ‡ΡŒ. Π’ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΠ΅Ρ€Π΅Π΄Π°ΡŽΡ‚ ΠΏΡƒΡ‚ΡŒ Π½Π° Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΈΠ»ΠΈ, Ссли ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ конфигурация, ΡƒΠ΄Π°Π»Ρ‘Π½Π½Ρ‹ΠΉ Ρ„Π°ΠΉΠ». ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‡Ρƒ Π΄Π°Π½Π½Ρ‹Ρ… ΡƒΠ΄Π°Π»Ρ‘Π½Π½ΠΎΠ³ΠΎ Ρ„Π°ΠΉΠ»Π° Π²Ρ‹ΠΏΠΎΠ»Π½ΡΡŽΡ‚ Ρ‡Π΅Ρ€Π΅Π· ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΡ‹Π΅ ΠΏΠΎΡ‚ΠΎΠΊΠΈ.

image_info

ΠΠ΅ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ заполняСтся Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠ΅ΠΉ, ΠΊΠΎΡ‚ΠΎΡ€ΡƒΡŽ ΠΌΠΎΠ΄ΡƒΠ»ΡŒ ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅Ρ‚ ΠΈΠ· Ρ„Π°ΠΉΠ»Π° изобраТСния. Π’ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ доступно ΠΈΠ·Π²Π»Π΅Ρ‡Π΅Π½ΠΈΠ΅ APP-ΠΌΠ°Ρ€ΠΊΠ΅Ρ€ΠΎΠ² JPG-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ Π² Π²ΠΈΠ΄Π΅ ассоциативного массива. ΠžΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Π΅ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ Ρ‡Π΅Ρ€Π΅Π· эти APP-ΠΌΠ°Ρ€ΠΊΠ΅Ρ€Ρ‹ Π²ΡΡ‚Ρ€Π°ΠΈΠ²Π°ΡŽΡ‚ тСкст Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. Π§Π°Ρ‰Π΅ ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Ρ… Π²ΡΡ‚Ρ€Π°ΠΈΠ²Π°ΡŽΡ‚ » IPTC-свСдСния Π² APP13-ΠΌΠ°Ρ€ΠΊΠ΅Ρ€. Для прСобразования Π΄Π²ΠΎΠΈΡ‡Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… APP13-ΠΌΠ°Ρ€ΠΊΠ΅Ρ€Π° Π²ΠΎ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ ΡƒΠ΄ΠΎΠ±Π½ΠΎΠ΅ для чтСния Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ iptcparse().

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image_info ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ„Π°ΠΉΠ»Ρ‹ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ JFIF.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив ΠΈΠ· 7 элСмСнтов. НС ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ‚ΠΈΠΏ изобраТСния содСрТит элСмСнты с ΠΊΠ°Π½Π°Π»Π°ΠΌΠΈ channels ΠΈ Π±ΠΈΡ‚Ρ€Π΅ΠΉΡ‚ΠΎΠΌ bits.

Π˜Π½Π΄Π΅ΠΊΡΡ‹ 0 ΠΈ 1 содСрТат ΡˆΠΈΡ€ΠΈΠ½Ρƒ ΠΈ высоту изобраТСния.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

ΠžΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Π΅ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Ρ‹ Ρ„Π°ΠΉΠ»ΠΎΠ² хранят Π½Π°Π±ΠΎΡ€ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ ΠΈΠ»ΠΈ Π²ΠΎΠΎΠ±Ρ‰Π΅ Π½Π΅ содСрТат ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ. Для Ρ‚Π°ΠΊΠΈΡ… Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize() Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Ρ€Π°Π·ΠΌΠ΅Ρ€ изобраТСния. Ѐункция getimagesize() Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π½ΡƒΠ»ΠΈ ΠΊΠ°ΠΊ значСния высоты ΠΈ ΡˆΠΈΡ€ΠΈΠ½Ρ‹.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Ѐункция getimagesize() Π½Π΅ ΡƒΡ‡ΠΈΡ‚Ρ‹Π²Π°Π΅Ρ‚ ΠΌΠ΅Ρ‚Π°Π΄Π°Π½Π½Ρ‹Π΅ изобраТСния, поэтому ΠΊΠΎΠ³Π΄Π°, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, значСния Exif-Ρ„Π»Π°Π³Π° Orientation ΠΏΠΎΠ²ΠΎΡ€Π°Ρ‡ΠΈΠ²Π°ΡŽΡ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° 90 ΠΈΠ»ΠΈ 270 градусов, индСксы 0 ΠΈ 1 ΠΌΠ΅Π½ΡΡŽΡ‚ΡΡ мСстами: индСкс 0 содСрТит высоту, Π° индСкс 1 β€” ΡˆΠΈΡ€ΠΈΠ½Ρƒ.

ИндСкс 2 содСрТит константу сСмСйства IMAGETYPE_*, которая ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Ρ‚ΠΈΠΏ изобраТСния.

ИндСкс 3 содСрТит строку со значСниями ΡˆΠΈΡ€ΠΈΠ½Ρ‹ ΠΈ высоты изобраТСния height="yyy" width="xxx", ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΡƒΠΊΠ°Π·Ρ‹Π²Π°ΡŽΡ‚ Π² Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Π°Ρ… Ρ‚Π΅Π³Π° IMG.

mime β€” MIME-Ρ‚ΠΈΠΏ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ соотвСтствуСт ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ. Π­Ρ‚ΠΈ свСдСния ΠΏΠΎΠΌΠΎΠ³Π°ΡŽΡ‚ ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Ρ‚ΡŒ изобраТСния Π½Π° основании Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ° Content-Type:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 Ѐункция getimagesize() ΠΈ MIME-Ρ‚ΠΈΠΏΡ‹

<?php

$size
= getimagesize($filename);
$fp = fopen($filename, "rb");

if (
$size && $fp) {
header("Content-Type: {$size['mime']}");
fpassthru($fp);
exit;
} else {
// Ошибка
}

Π­Π»Π΅ΠΌΠ΅Π½Ρ‚ channels ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 3 для RGB-ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΎΠΊ ΠΈ 4 для Ρ†Π²Π΅Ρ‚ΠΎΠ²ΠΎΠΉ ΠΌΠΎΠ΄Π΅Π»ΠΈ CMYK.

bits β€” количСство Π±ΠΈΡ‚ΠΎΠ² для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Ρ†Π²Π΅Ρ‚Π°.

Для ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ‚ΠΈΠΏΠΎΠ² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ присутствиС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ channels ΠΈ bits ΠΈΠ½ΠΎΠ³Π΄Π° сбиваСт с Ρ‚ΠΎΠ»ΠΊΡƒ. Π€ΠΎΡ€ΠΌΠ°Ρ‚ GIF, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€, прСдставляСт ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ пиксСль 3-ΠΊΠ°Π½Π°Π»ΡŒΠ½Ρ‹ΠΌ Ρ†Π²Π΅Ρ‚ΠΎΠΌ, Π½ΠΎ количСство Π±ΠΈΡ‚ΠΎΠ² для хранСния ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ пиксСля Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ΄Π½ΠΎΠ·Π½Π°Ρ‡Π½ΠΎ Π²Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚ΡŒ для Π°Π½ΠΈΠΌΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹Ρ… GIF-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ, Ссли ΠΎΠΏΠΈΡ€Π°Ρ‚ΡŒΡΡ Π½Π° Π³Π»ΠΎΠ±Π°Π»ΡŒΠ½ΡƒΡŽ Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ Ρ†Π²Π΅Ρ‚ΠΎΠ², ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΊΠ°Π΄Ρ€Ρ‹ Π°Π½ΠΈΠΌΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ изобраТСния часто содСрТит Π»ΠΎΠΊΠ°Π»ΡŒΠ½ΡƒΡŽ ΠΏΠ°Π»ΠΈΡ‚Ρ€Ρƒ Ρ†Π²Π΅Ρ‚ΠΎΠ².

Ѐункция Π²Π΅Ρ€Π½Ρ‘Ρ‚ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ½Π΅Ρ‚ ошибка.

Ошибки

ΠŸΡ€ΠΈ нСдоступности Ρ„Π°ΠΉΠ»Π° ΠΏΠΎ ΠΏΡƒΡ‚ΠΈ filename функция getimagesize() Π²Ρ‹Π΄Π°Ρ‘Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ уровня E_WARNING. ΠŸΡ€ΠΈ ошибкС чтСния Ρ„Π°ΠΉΠ»Π° функция getimagesize() Π²Ρ‹Π΄Π°Ρ‘Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ уровня E_NOTICE.

Π‘ PHP 8.0.0 ΠΏΡ€ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡Π΅ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ filename пустой строки выбрасываСтся ошибка ValueError.

Бписок измСнСний

ВСрсия ОписаниС
8.2.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ фактичСскиС Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹, Π±ΠΈΡ‚Ρ‹ ΠΈ ΠΊΠ°Π½Π°Π»Ρ‹ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ AVIF; Ρ€Π°Π½ΡŒΡˆΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΡΠΎΠΎΠ±Ρ‰Π°Π»ΠΈΡΡŒ ΠΊΠ°ΠΊ 0x0, Π° Π±ΠΈΡ‚Ρ‹ ΠΈ ΠΊΠ°Π½Π°Π»Ρ‹ Π½Π΅ ΡΠΎΠΎΠ±Ρ‰Π°Π»ΠΈΡΡŒ Π²ΠΎΠΎΠ±Ρ‰Π΅.
8.0.0 Ѐункция Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ выбрасываСт ΠΎΡˆΠΈΠ±ΠΊΡƒ ValueError, Ссли Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ filename ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ ΠΏΡƒΡΡ‚ΡƒΡŽ строку; Ρ€Π°Π½ΡŒΡˆΠ΅ Π²Ρ‹Π΄Π°Π²Π°Π»Π°ΡΡŒ ошибка уровня E_WARNING, ΠΈ функция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π»Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false.
7.1.0 Π”ΠΎΠ±Π°Π²ΠΈΠ»ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° WebP.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ получСния Ρ€Π°Π·ΠΌΠ΅Ρ€Π° изобраТСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize()

<?php

list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo
"<img src=\"img/flag.jpg\" $attr alt=\"ΠΏΡ€ΠΈΠΌΠ΅Ρ€ getimagesize()\" />";

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #3 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ получСния Ρ€Π°Π·ΠΌΠ΅Ρ€Π° изобраТСния ΠΏΠΎ URL-адрСсу Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize

<?php

$size
= getimagesize("http://www.example.com/gifs/logo.gif");

// Названия Ρ„Π°ΠΉΠ»ΠΎΠ² с ΠΏΡ€ΠΎΠ±Π΅Π»Π°ΠΌΠΈ трСбуСтся ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
$size = getimagesize("http://www.example.com/gifs/lo%20go.gif");

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #4 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° IPTC-ΠΌΠ΅Ρ‚Π°Π΄Π°Π½Π½Ρ‹Ρ… Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getimagesize()

<?php

$size
= getimagesize("testimg.jpg", $info);
if (isset(
$info["APP13"])) {
$iptc = iptcparse($info["APP13"]);
var_dump($iptc);
}

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π­Ρ‚ΠΎΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ трСбуСтся Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° GD.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • image_type_to_mime_type() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ Mime-Ρ‚ΠΈΠΏΠ° для Ρ‚ΠΈΠΏΠ° изобраТСния, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ³ΠΎ функциями getimagesize, exif_read_data, exif_thumbnail, exif_imagetype
  • exif_imagetype() - Determine the type of an image
  • exif_read_data() - Π§ΠΈΡ‚Π°Π΅Ρ‚ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ EXIF ΠΈΠ· Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ
  • exif_thumbnail() - ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ встроСнноС ΠΏΡ€Π΅Π²ΡŒΡŽ изобраТСния
  • imagesx() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΡˆΠΈΡ€ΠΈΠ½Ρ‹ изобраТСния
  • imagesy() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ высоты изобраТСния
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 25 notes

up
95
james dot relyea at zifiniti dot com ΒΆ
17 years ago
As noted below, getimagesize will download the entire image before it checks for the requested information. This is extremely slow on large images that are accessed remotely. Since the width/height is in the first few bytes of the file, there is no need to download the entire file. I wrote a function to get the size of a JPEG by streaming bytes until the proper data is found to report the width and height:

<?php
// Retrieve JPEG width and height without downloading/reading entire image.
function getjpegsize($img_loc) {
    $handle = fopen($img_loc, "rb") or die("Invalid file stream.");
    $new_block = NULL;
    if(!feof($handle)) {
        $new_block = fread($handle, 32);
        $i = 0;
        if($new_block[$i]=="\xFF" && $new_block[$i+1]=="\xD8" && $new_block[$i+2]=="\xFF" && $new_block[$i+3]=="\xE0") {
            $i += 4;
            if($new_block[$i+2]=="\x4A" && $new_block[$i+3]=="\x46" && $new_block[$i+4]=="\x49" && $new_block[$i+5]=="\x46" && $new_block[$i+6]=="\x00") {
                // Read block size and skip ahead to begin cycling through blocks in search of SOF marker
                $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                $block_size = hexdec($block_size[1]);
                while(!feof($handle)) {
                    $i += $block_size;
                    $new_block .= fread($handle, $block_size);
                    if($new_block[$i]=="\xFF") {
                        // New block detected, check for SOF marker
                        $sof_marker = array("\xC0", "\xC1", "\xC2", "\xC3", "\xC5", "\xC6", "\xC7", "\xC8", "\xC9", "\xCA", "\xCB", "\xCD", "\xCE", "\xCF");
                        if(in_array($new_block[$i+1], $sof_marker)) {
                            // SOF marker detected. Width and height information is contained in bytes 4-7 after this byte.
                            $size_data = $new_block[$i+2] . $new_block[$i+3] . $new_block[$i+4] . $new_block[$i+5] . $new_block[$i+6] . $new_block[$i+7] . $new_block[$i+8];
                            $unpacked = unpack("H*", $size_data);
                            $unpacked = $unpacked[1];
                            $height = hexdec($unpacked[6] . $unpacked[7] . $unpacked[8] . $unpacked[9]);
                            $width = hexdec($unpacked[10] . $unpacked[11] . $unpacked[12] . $unpacked[13]);
                            return array($width, $height);
                        } else {
                            // Skip block marker and read block size
                            $i += 2;
                            $block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
                            $block_size = hexdec($block_size[1]);
                        }
                    } else {
                        return FALSE;
                    }
                }
            }
        }
    }
    return FALSE;
}
?>
up
16
nikolam3244 at gmail dot com ΒΆ
8 years ago
There's a code snippet for getting JPEG image dimensions by getting only first few bytes of the file, but it doesn't work for PNG files, so I wrote one. It will download only the first 24 bytes instead of the whole image, and thus being much faster than getimagesize() and it will save bandwidth at the same time:

<?php
// Retrieve PNG width and height without downloading/reading entire image.
function getpngsize( $img_loc ) {
    $handle = fopen( $img_loc, "rb" ) or die( "Invalid file stream." );

    if ( ! feof( $handle ) ) {
        $new_block = fread( $handle, 24 );
        if ( $new_block[0] == "\x89" &&
            $new_block[1] == "\x50" &&
            $new_block[2] == "\x4E" &&
            $new_block[3] == "\x47" &&
            $new_block[4] == "\x0D" &&
            $new_block[5] == "\x0A" &&
            $new_block[6] == "\x1A" &&
            $new_block[7] == "\x0A" ) {
                if ( $new_block[12] . $new_block[13] . $new_block[14] . $new_block[15] === "\x49\x48\x44\x52" ) {
                    $width  = unpack( 'H*', $new_block[16] . $new_block[17] . $new_block[18] . $new_block[19] );
                    $width  = hexdec( $width[1] );
                    $height = unpack( 'H*', $new_block[20] . $new_block[21] . $new_block[22] . $new_block[23] );
                    $height  = hexdec( $height[1] );

                    return array( $width, $height );
                }
            }
        }

    return false;
}
?>
up
17
tomasz at trejderowski dot pl ΒΆ
12 years ago
If you want to "convert" value returned by "getimagesize()" as index "2" into something more human-readable, you may consider using a function like this one:

    $imageTypeArray = array
    (
        0=>'UNKNOWN',
        1=>'GIF',
        2=>'JPEG',
        3=>'PNG',
        4=>'SWF',
        5=>'PSD',
        6=>'BMP',
        7=>'TIFF_II',
        8=>'TIFF_MM',
        9=>'JPC',
        10=>'JP2',
        11=>'JPX',
        12=>'JB2',
        13=>'SWC',
        14=>'IFF',
        15=>'WBMP',
        16=>'XBM',
        17=>'ICO',
        18=>'COUNT'  
    );
    
    $size = getimagesize($filename);
    
    $size[2] = $imageTypeArray[$size[2]];

Or something similar.
up
19
php dot net at dannysauer dot com ΒΆ
21 years ago
Note that, if you're going to be a good programmer and use named constatnts (IMAGETYPE_JPEG) rather than their values (2), you want to use the IMAGETYPE variants - IMAGETYPE_JPEG, IMAGETYPE GIF, IMAGETYPE_PNG, etc.  For some reason, somebody made a horrible decision, and IMG_PNG is actually 4 in my version of PHP, while IMAGETYPE_PNG is 3.  It took me a while to figure out why comparing the type against IMG_PNG was failing...
up
5
simon dot waters at surevine dot com ΒΆ
11 years ago
Note: getimage size doesn't attempt to validate image file formats

It is possible for malformed GIF images to contain PHP and still have valid dimensions.

Programmers need to ensure such images are validated by other tools, or never treated as PHP or other executable types (enforcing appropriate extensions, avoiding user controlled renaming, restricting uploaded images to areas of the website where PHP is not enabled).

http://ha.ckers.org/blog/20070604/passing-malicious-php-through-getimagesize/
up
4
redcore at gmail dot com ΒΆ
18 years ago
It's always good to check out an image's dimensions while attempting to upload to your server or database...especially if it's going to be displayed on a page that doesn't accomodate images beyond a particular size.

<?php

$tmpName = $_FILES['userfile']['tmp_name'];
        
list($width, $height, $type, $attr) = getimagesize($tmpName);

if($width>275 || $height>275)
{
die("exceeded image dimension limits.");
}

?>
up
3
utilmind ΒΆ
14 years ago
Here is the function which determines whether the PNG image contains alpha or not:

<?php
function is_alpha_png($fn){
  return (ord(@file_get_contents($fn, NULL, NULL, 25, 1)) == 6);
}
?>

The color type of PNG image is stored at byte offset 25. Possible values of that 25'th byte is:
 * 0 - greyscale
 * 2 - RGB
 * 3 - RGB with palette
 * 4 - greyscale + alpha
 * 6 - RGB + alpha
up
3
info at personalmis dot com ΒΆ
18 years ago
Seems the various ways people are trying to proportionaly scale an image, up or down, could be more straight forward if one remembers ones algebra.

The formula is, y = mx, where m is the slope of the line. This is the ratio of y:x or m = y/x.

So if...

// max values for x and y
$y_max = 600;
$x_max = 800;

// image size
$y1 = 2000;
$x1 = 3000;

// use width for scaling
if ($x1 > $x_max)
{
    // find slope 
    $m = $y1/$x1;
    // set x side to max
    $x2 = $x_max;
    // set y side to a proportional size
    $y2 = $m * $x1;
}

The new image proportionally scaled will be x2 = 800, y2 = 533 (rounded).

To do it from the y side, simply reverse the x's and y's.
up
4
Steve ΒΆ
15 years ago
The list of defined IMAGETYPE_ constants is on the manual page for exif_imagetype:

http://www.php.net/manual/en/function.exif-imagetype.php
up
5
info at alex-lawrence dot com ΒΆ
18 years ago
Could be useful (didnΒ΄t know where to post it):

function getImageErrors( $filename, $type = "", $minWidth = 0, $minHeight = 0, $maxWidth = 0, $maxHeight = 0, $maxFileSize = 0 )
{
    $errors = array();
    if ( file_exists( $filename ) )
    {
        $ending = substr( $filename, strpos( $filename, "." ) );
        if ( is_array( $type ) )
        {
            $isTypeOf = false;
            foreach( $type as $eachtype )
            {
                if ( $ending == $eachtype )
                {
                    $isTypeOf = true;
                }
            }
            if ( ! $isTypeOf )
            {
                $errors[ 'type' ] = $ending;
            }
        }
        elseif ( $type != "" )
        {
            if ( $ending != $type )
            {
                $errors[ 'type' ] = $ending;
            }
        }
        $size = getimagesize( $filename );
        if ( $size[ 0 ] < $minWidth )
        {
            $errors[ 'minWidth' ] = $size[ 0 ];
        }
        if ( $size[ 1 ] < $minHeight )
        {
            $errors[ 'minHeight' ] = $size[ 1 ];
        }
        if ( ( $maxWidth > $minWidth ) && ( $size[ 0 ] > $maxWidth ) )
        {
            $errors[ 'maxWidth' ] = $size[ 0 ];
        }
        if ( ( $maxHeight > $minHeight ) && ( $size[ 1 ] > $maxHeight ) )
        {
            $errors[ 'maxHeight' ] = $size[ 1 ];
        }
        if ( ( $maxFileSize > 0 ) && ( filesize( $filename ) > $maxFileSize ) )
        {
            $errors[ 'maxFileSize' ] = filesize( $filename );
        }
    }
    else
    {
        $errors[ 'filename' ] = "not existing";
    }
    return ( count( $errors ) > 0 ? $errors : null );
}
up
3
cloned at clonedmadman dot com ΒΆ
18 years ago
Well, I am making a script which will resize the image when uploaded, however, i am making a multi-uploader, so i came across with a problem: an efficient way of getting a pictures height and width and storing them in an array to resize later. This is what i came up with:

<?php
$links = array("test1.jpg", "test2.png");
$sizearray = array();
$count = count($links);
for($i = 0; $i < $count; $i++) {
    $size = getimagesize($links[$i]);
    list($width, $height) = $size;
    $sizearray[$links[$i]] = array("width" => $width, "height" => $height);
}
print_r($sizearray);
// which will print out: Array ( [test1.jpg] => Array ( [width] => 300 [height] => 400 ) [test2.png] => Array ( [width] => 680 [height] => 100 ) )
?>
up
2
diablx at hotmail dot com ΒΆ
22 years ago
I'm sorry for they other scripts, but I made one mistake about the image resizing... here is a working script !
<?
    // Some configuration variables !
    $maxWidth = 90;
    $maxHeight = 90;
    $maxCols = 8;
    $webDir = "https://localhost/images/";
    $localDir = $_SERVER['DOCUMENT_ROOT']."/images/";

    $AutorisedImageType = array ("jpg", "jpeg", "gif", "png");
?>

<center>
<table border='1' cellspacing='5' cellpadding='5' style="border-collapse:collapse; border-style: dotted">
<tr>
   <?
   // Open localDir
   $dh = opendir($localDir);
   while (false !== ($filename = readdir($dh))) {
       $filesArray[] = $filename;
   }

   // Display and resize
   foreach ($filesArray as $images) {
   
       $ext = substr($images, strpos($images, ".")+1, strlen($images));
       
       if( in_array($ext, $AutorisedImageType) ) {

           list($width, $height, $type, $attr) = @getimagesize( $localDir.$images );

            $xRatio = $maxWidth / $width; 
            $yRatio = $maxHeight / $height; 
            
            if ( ($width <= $maxWidth) && ($height <= $maxHeight) ) { 
              $newWidth = $width; 
              $newHeight = $height; 
            } 
            else if (($xRatio * $height) < $maxHeight) { 
              $newHeight = ceil($xRatio * $height); 
              $newWidth = $maxWidth; 
            } 
            else { 
              $newWidth = ceil($yRatio * $width); 
              $newHeight = $maxHeight; 
            } 
           
           if($i == $maxCols) {
               echo "</tr><tr>";
               $i = 0;
           }
           echo "<td align='center' valign='middle' width='$maxWidth' height='$maxHeight'><img src='".$webDir.$images."' width='$newWidth' height='$newHeight'></td>";
           $i++;
       }
   }
?>
</tr>
</table>
</center>
up
1
kazuya ΒΆ
12 years ago
i made function img_resize($path,$tmp_name,$new_name,$new_width)
this could be useful.

<?php

$new_file = img_resize("./img/", "test.jpg","copy_test.jpg",300);
echo "<IMG src = '$new_file'>";

function img_resize($path,$tmp_name,$new_name,$new_width){
    if (!file_exists($path.$filename)){
        echo "file not found!";
        exit;
    }
    if (!is_writable($path)){
        echo "error:permission denied!";
        exit;
    }
    list($width, $height) = getimagesize($path . $tmp_name);
    $new_height = abs($new_width * $height / $width); 
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($path . $tmp_name); 
    imagecopyresampled($image_p, $image, 0, 0, 0, 0,
                        $new_width, $new_height, $width, $height); 
    imagejpeg($image_p, $path . $new_name); 
    return $path.$new_name;
}

?>
up
1
shmohel at gmail dot com ΒΆ
18 years ago
Rather than making a lengthy function that essentially runs twice (once as width, once as height) I came up with a helpful function that uses variable variables to set a maximum height/width. Hope someone finds this helpful.

function scaleimage($location, $maxw=NULL, $maxh=NULL){
    $img = @getimagesize($location);
    if($img){
        $w = $img[0];
        $h = $img[1];

        $dim = array('w','h');
        foreach($dim AS $val){
            $max = "max{$val}";
            if(${$val} > ${$max} && ${$max}){
                $alt = ($val == 'w') ? 'h' : 'w';
                $ratio = ${$alt} / ${$val};
                ${$val} = ${$max};
                ${$alt} = ${$val} * $ratio;
            }
        }

        return("<img src='{$location}' alt='image' width='{$w}' height='{$h}' />");
    }
}
up
1
geoff at spacevs dot com ΒΆ
16 years ago
This function returns the width and height of a JPEG image from a string, allowing the dimensions of images stored in a database to be retrieved without writing them to the disk first, or using "imagecreatefromstring" which is very slow in comparison.

<?PHP
function getJPEGImageXY($data) {
        $soi = unpack('nmagic/nmarker', $data);
        if ($soi['magic'] != 0xFFD8) return false;
        $marker = $soi['marker'];
        $data   = substr($data, 4);
        $done   = false;

        while(1) {
                if (strlen($data) === 0) return false;
                switch($marker) {
                        case 0xFFC0:
                                $info = unpack('nlength/Cprecision/nY/nX', $data);
                                return array($info['X'], $info['Y']);
                                break;

                        default:
                                $info   = unpack('nlength', $data);
                                $data   = substr($data, $info['length']);
                                $info   = unpack('nmarker', $data);
                                $marker = $info['marker'];
                                $data   = substr($data, 2);
                                break;
                }
        }
}
?>

Doing this 10,000 times takes 0.43 seconds, compared with using imagecreatefromstring/imagesx/imagesy which takes around 1.52 seconds to do the same.

Do not use this instead of getimagesize when dealing with files, getimagesize is much faster coming in at 0.15 seconds.
up
0
zankaria dot auxa at mailu dot io ΒΆ
7 months ago
Looking at the number of channels, you can detect if the image SUPPORTS transparecy

function image_supports_transparency(string $file_path): ?bool {
    $ret = \getimagesize($file_path);
    $image_type = $ret[2];
    $supports_transparency = null;

    if (isset($ret['channels'])) {
        $channels = $ret['channles'];

        if ($image_type === \IMAGETYPE_JPEG || $image_type === \IMAGETYPE_GIF) {
            $supports_transparency = false;
        } elseif ($image_type === \IMAGETYPE_PNG) {
            if ($channels === 2 || $channels === 4) {
                $supports_transparency = true;
            } elseif ($channels !== 1) {
                // PNG also supports transparency via palette, which only uses a single channel.
                $supports_transparency = false;
            }
        } elseif ($image_type === \IMAGETYPE_WEBP) {
            $supports_transparency = $channels === 4;
        } elseif ($mime === 'image/avif') {
            $supports_transparency = $channels === 4 || $channels == 2;
        } elseif ($image_type === \IMAGETYPE_BMP) {
            $supports_transparency = $channels === 4;
        }
    }

    return $supports_transparency;
}

I say supports because an image may have an alpha channel but not actually use it, you'll need to check all the image's pixels to detect that.
Also note that it's not always possible to detect the alpha channel with just getimagesize(); see for example png which can define the number of channles actually used in the palette chunk.
up
0
freecorvette at gmail dot com ΒΆ
8 years ago
For some images, using getimagesize() without the second parameter will return the correct info, but when you add the second parameter it will return false. This is most likely a bug (and it has been reported as such), but meanwhile, if you encounter this problem, a workaround is to use exif_read_data().
up
0
alexyam at live dot com ΒΆ
14 years ago
I wanted to use getimagesize() on .SWF files stored in the database as blob data and couldn't find a simple solution, so I created my own.

I am releasing this code under the MIT license to save everyone some time:

<?php
/*
    ----------------------------------------------------------------------
    PHP Blob Data As File Stream v1.0 (C) 2012 Alex Yam <alexyam@live.com>
    This code is released under the MIT License.
    ----------------------------------------------------------------------
    [Summary]

    A simple class for PHP functions to read and write blob data as a file
    using a stream wrapper.

    Particularly useful for running getimagesize() to get the width and
    height of .SWF Flash files that are stored in the database as blob data.

    Tested on PHP 5.3.10.

    ----------------------------------------------------------------------    
    [Usage Example]

    //Include
        include('./blob_data_as_file_stream.php');

    //Register the stream wrapper
        stream_wrapper_register("BlobDataAsFileStream", "blob_data_as_file_stream");

    //Fetch a .SWF file from the Adobe website and store it into a variable.
    //Replace this with your own fetch-swf-blob-data-from-database code.
        $swf_url = 'http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf';
        $swf_blob_data = file_get_contents($swf_url);
    
    //Store $swf_blob_data to the data stream
        blob_data_as_file_stream::$blob_data_stream = $swf_blob_data;
    
    //Run getimagesize() on the data stream
        $swf_info = getimagesize('BlobDataAsFileStream://');
        var_dump($swf_info);

    ----------------------------------------------------------------------
    [Usage Output]

    array(5) {
      [0]=>
      int(159)
      [1]=>
      int(91)
      [2]=>
      int(13)
      [3]=>
      string(23) "width="159" height="91""
      ["mime"]=>
      string(29) "application/x-shockwave-flash"
    }

*/

class blob_data_as_file_stream {

    private static $blob_data_position = 0;
    public static $blob_data_stream = '';

    public static function stream_open($path,$mode,$options,&$opened_path){
        static::$blob_data_position = 0;
        return true;
    }

    public static function stream_seek($seek_offset,$seek_whence){
        $blob_data_length = strlen(static::$blob_data_stream);
        switch ($seek_whence) {
            case SEEK_SET:
                $new_blob_data_position = $seek_offset;
                break;
            case SEEK_CUR:
                $new_blob_data_position = static::$blob_data_position+$seek_offset;
                break;
            case SEEK_END:
                $new_blob_data_position = $blob_data_length+$seek_offset;
                break;
            default:
                return false;
        }
        if (($new_blob_data_position >= 0) AND ($new_blob_data_position <= $blob_data_length)){
            static::$blob_data_position = $new_blob_data_position;
            return true;
        }else{
            return false;
        }
    }

    public static function stream_tell(){
        return static::$blob_data_position;
    }

    public static function stream_read($read_buffer_size){
        $read_data = substr(static::$blob_data_stream,static::$blob_data_position,$read_buffer_size);
        static::$blob_data_position += strlen($read_data);
        return $read_data;
    }

    public static function stream_write($write_data){
        $write_data_length=strlen($write_data);
        static::$blob_data_stream = substr(static::$blob_data_stream,0,static::$blob_data_position).
            $write_data.substr(static::$blob_data_stream,static::$blob_data_position+=$write_data_length);
        return $write_data_length;
    }

    public static function stream_eof(){
        return static::$blob_data_position >= strlen(static::$blob_data_stream);
    }

}
?>
up
0
Jesus Zamora ΒΆ
15 years ago
Returns a array with 4 elements.
The 0 index is the width of the image in pixels.
The 1 index is the height of the image in pixels.
The 2 index is a flag for the image type:

1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. 

The 3 index contains ' height="yyy" width="xxx" '
up
0
ajreading at classixshop dot com ΒΆ
21 years ago
A simple piece of code i wrote to proportionally resize an image to a max height and width then display it

<?php
// Max height and width
$max_width = 100;
$max_height = 100;

// Path to your jpeg

$upfile '/path/to/file.jpg';
    Header("Content-type: image/jpeg");
    
    $size = GetImageSize($upfile); // Read the size
          $width = $size[0];
          $height = $size[1];
          
          // Proportionally resize the image to the
          // max sizes specified above
          
          $x_ratio = $max_width / $width;
          $y_ratio = $max_height / $height;

          if( ($width <= $max_width) && ($height <= $max_height) )
          {
               $tn_width = $width;
               $tn_height = $height;
          }
          elseif (($x_ratio * $height) < $max_height)
          {
               $tn_height = ceil($x_ratio * $height);
               $tn_width = $max_width;
          }
          else
          {
               $tn_width = ceil($y_ratio * $width);
               $tn_height = $max_height;
          }
     // Increase memory limit to support larger files
     
     ini_set('memory_limit', '32M');
     
     // Create the new image!
     $src = ImageCreateFromJpeg($upfile);
     $dst = ImageCreateTrueColor($tn_width, $tn_height);
     ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
     ImageJpeg($dst);
// Destroy the images
ImageDestroy($src);
ImageDestroy($dst);
?>
up
0
mail at soylentgreens dot com ΒΆ
21 years ago
How about this for cropping images...

<?php

$imgfile = "img.jpg";
$cropStartX = 300;
$cropStartY = 250;
$cropW   = 200;
$cropH   = 200;

// Create two images
$origimg = imagecreatefromjpeg($imgfile);
$cropimg = imagecreatetruecolor($cropW,$cropH);

// Get the original size
list($width, $height) = getimagesize($imgfile);

// Crop
imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

// TODO: write code to save new image
// or, just display it like this:
header("Content-type: image/jpeg");
imagejpeg($cropimg);

// destroy the images
imagedestroy($cropimg);
imagedestroy($origimg);

?>
up
-1
anonymous ΒΆ
17 years ago
Note that if you specify a remote file (via a URL) to check the size of, PHP will first download the remote file to your server.

If you're using this function to check the size of user provided image links, this could constitute a security risk.  A malicious user could potentially link to a very large image file and cause PHP to download it.  I do not know what, if any, file size limits are in place for the download.  But suppose the user provided a link to an image that was several gigabytes in size?

It would be nice if there were a way to limit the size of the download performed by this function.  Hopefully there is already a default with some sensible limits.
up
-2
user at example dot net ΒΆ
17 years ago
When validating images, allways check both, image type *AND* file extension!

Because most image types allow sections for comments or other irrelevant data. Those section can be used to infiltrate php code onto the server. If these files are stored as sent by the client, files with a ".php" extension can be executed and do tremendous harm.
up
-3
Coodiss at w3bbix dot net ΒΆ
21 years ago
Heres a easy way to scale images to the <td> that they are in
*this is broken up so anyone can understand it :)

<?
$imageinfo = getimagesize("images/picture.jpg");
          
$ix=$imageinfo[0];
$iy=$imageinfo[1];

$widthscale = $ix/175;  //<TD> WIDTH
$heightscale = $iy/175; //<TD> HEIGHT

if($widthscale < 1)
$nwidth = $ix*$widthscale;
else
$nwidth = $ix/$widthscale;

if($heightscale < 1)
$nheight = $iy*$heightscale;
else
$nheight = $iy/$heightscale;

?>
up
-4
pfarthing at hotmail dot com ΒΆ
18 years ago
Correction: to find $y2 it should be...

// set y side to a proportional size
$y2 = $m * $x_max; // not $x1

Thanks Norbert =)