PHP Conference Japan 2024

getimagesize

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

getimagesize取得影像的大小

描述

getimagesize(字串 $filename, 陣列 &$image_info = null): 陣列|false

getimagesize() 函式將會判斷任何支援的給定影像檔案的大小,並傳回尺寸以及檔案類型、一個可在普通 HTML IMG 標籤內使用的 height/width 文字字串,以及對應的 HTTP 內容類型。

getimagesize() 也可以在 image_info 參數中傳回更多資訊。

注意

此函式預期 filename 為有效的影像檔案。如果提供非影像檔案,可能會被錯誤地偵測為影像,且函式會成功傳回,但陣列可能包含無意義的值。

請勿使用 getimagesize() 來檢查給定的檔案是否為有效影像。請改用專門的解決方案,例如 Fileinfo 擴充功能。

注意: 請注意,JPC 和 JP2 可以具有不同位元深度的元件。在這種情況下,「bits」的值是遇到的最高位元深度。此外,JP2 檔案可能包含 多個 JPEG 2000 碼流。在這種情況下,getimagesize() 會傳回它在檔案根目錄中遇到的第一個碼流的值。

注意: 關於圖示的資訊會從具有最高位元率的圖示中擷取。

注意: GIF 影像由一個或多個影格組成,其中每個影格可能只佔據影像的一部分。getimagesize() 報告的影像大小是整體大小(從邏輯螢幕描述符讀取)。

參數

filename

此參數指定您想要擷取資訊的檔案。它可以參考本機檔案或(在允許的設定下)使用其中一個支援的串流的遠端檔案。

image_info

此選用參數可讓您從影像檔案中擷取一些擴充資訊。目前,這將傳回不同的 JPG APP 標記做為關聯陣列。某些程式會使用這些 APP 標記將文字資訊嵌入影像中。一個非常常見的做法是在 APP13 標記中嵌入 » IPTC 資訊。您可以使用 iptcparse() 函式將二進位 APP13 標記剖析為可讀取的內容。

注意:

image_info 僅支援 JFIF 檔案。

傳回值

傳回一個最多包含 7 個元素的陣列。並非所有影像類型都會包含 channelsbits 元素。

索引 0 和 1 分別包含影像的寬度和高度。

注意:

某些格式可能不包含影像或可能包含多個影像。在這些情況下,getimagesize() 可能無法正確判斷影像大小。在這些情況下,getimagesize() 將會傳回寬度和高度為零。

索引 2 是其中一個 IMAGETYPE_* 常數,表示影像的類型。

索引 3 是一個文字字串,其中包含正確的 height="yyy" width="xxx" 字串,可以直接在 IMG 標籤中使用。

mime 是影像的對應 MIME 類型。此資訊可用於以正確的 HTTP 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 對於 RGB 圖片將為 3,對於 CMYK 圖片將為 4。

bits 是每個顏色的位元數。

對於某些影像類型,channelsbits 值的存在可能會讓人有些困惑。例如,GIF 總是每個像素使用 3 個通道,但對於具有全域色彩表的動畫 GIF 而言,無法計算每個像素的位元數。

如果失敗,則會傳回 false

錯誤/例外

如果無法存取 filename 影像,getimagesize() 將會產生層級 E_WARNING 的錯誤。在讀取錯誤時,getimagesize() 將會產生層級 E_NOTICE 的錯誤。

變更日誌

版本 描述
8.2.0 現在傳回 AVIF 影像的實際影像尺寸、位元和通道;先前,尺寸回報為 0x0,而位元和通道則完全沒有回報。
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 getimagesize (URL)

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

// 如果檔案名稱中有空格,請正確編碼
$size = getimagesize("http://www.example.com/gifs/lo%20go.gif");

?>

範例 #4 getimagesize() 傳回 IPTC

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

註解

注意:

此函式不需要 GD 影像程式庫。

另請參閱

新增註解

使用者貢獻的註解 24 則註解

james dot relyea at zifiniti dot com
15 年前
如下所述,getimagesize 會在檢查要求的資訊之前下載整個圖像。對於遠端存取的大型圖像來說,這非常慢。由於寬度/高度在檔案的前幾個位元組中,因此無需下載整個檔案。我編寫了一個函式,透過串流位元組直到找到正確的資料來報告寬度和高度,以取得 JPEG 的大小

<?php
// 無需下載/讀取整個圖像即可檢索 JPEG 寬度和高度。
function getjpegsize($img_loc) {
$handle = fopen($img_loc, "rb") or die("無效的檔案串流。");
$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") {
// 讀取區塊大小並跳到前方,開始循環遍歷區塊以搜尋 SOF 標記
$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") {
// 偵測到新區塊,檢查 SOF 標記
$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 標記。寬度和高度資訊包含在此位元組之後的位元組 4-7 中。
$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 {
// 跳過區塊標記並讀取區塊大小
$i += 2;
$block_size = unpack("H*", $new_block[$i] . $new_block[$i+1]);
$block_size = hexdec($block_size[1]);
}
} else {
return
FALSE;
}
}
}
}
return
FALSE;
}
?>
nikolam3244 at gmail dot com
6 年前
有一個程式碼片段可以透過僅取得檔案的前幾個位元組來取得 JPEG 圖像尺寸,但是它不適用於 PNG 檔案,所以我編寫了一個。它只會下載前 24 個位元組,而不是整個圖像,因此比 getimagesize() 快得多,同時還可以節省頻寬

<?php
// 取得 PNG 圖片的寬度和高度,無需下載/讀取整個圖片。
function getpngsize( $img_loc ) {
$handle = fopen( $img_loc, "rb" ) or die( "無效的檔案串流。" );

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;
}
?>
tomasz at trejderowski dot pl
11 年前
如果您想要將 "getimagesize()" 傳回的索引 "2" 值「轉換」為更易於理解的內容,您可以考慮使用類似以下的函式

$imageTypeArray = array
(
0=>'未知',
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]];

或者類似的方式。
php dot net at dannysauer dot com
19 年前
請注意,如果您要成為一個好的程式設計師並使用具名的常數 (IMAGETYPE_JPEG) 而不是它們的值 (2),您應該使用 IMAGETYPE 變體 - IMAGETYPE_JPEG、IMAGETYPE GIF、IMAGETYPE_PNG 等。不知何故,有人做了一個很糟糕的決定,IMG_PNG 在我的 PHP 版本中實際上是 4,而 IMAGETYPE_PNG 是 3。我花了一段時間才弄清楚為什麼將類型與 IMG_PNG 比較會失敗...
simon dot waters at surevine dot com
9 年前
注意:getimagesize 不會嘗試驗證影像檔案格式

格式錯誤的 GIF 影像有可能包含 PHP 並且仍然具有有效的尺寸。

程式設計師需要確保此類影像由其他工具驗證,或者永遠不會被視為 PHP 或其他可執行類型(強制執行適當的副檔名、避免使用者控制的重新命名、將上傳的影像限制在未啟用 PHP 的網站區域)。

http://ha.ckers.org/blog/20070604/passing-malicious-php-through-getimagesize/
redcore at gmail dot com
17 年前
嘗試上傳到您的伺服器或資料庫時,最好檢查影像的尺寸...特別是如果它將顯示在無法容納超出特定大小的影像的頁面上。

<?php

$tmpName
= $_FILES['userfile']['tmp_name'];

list(
$width, $height, $type, $attr) = getimagesize($tmpName);

if(
$width>275 || $height>275)
{
die(
"超出影像尺寸限制。");
}

?>
utilmind
13 年前
這是判斷 PNG 影像是否包含 Alpha 通道的函式

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

PNG 影像的色彩類型儲存在位元組偏移量 25 處。該第 25 個位元組的可能值為
* 0 - 灰階
* 2 - RGB
* 3 - 帶調色盤的 RGB
* 4 - 灰階 + Alpha
* 6 - RGB + Alpha
info at alex-lawrence dot com
16 年前
可能很有用(不知道該發佈在哪裡)

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' ] = "不存在";
}
return ( count( $errors ) > 0 ? $errors : null );
}
Steve
13 年前
定義的 IMAGETYPE_ 常數清單位於 exif_imagetype 的手冊頁面上

https://php.dev.org.tw/manual/en/function.exif-imagetype.php
shmohel at gmail dot com
16 年前
我提出了一個有用的函式,使用可變變數來設定最大高度/寬度,而不是建立一個本質上運行兩次(一次作為寬度,一次作為高度)的冗長函式。希望有人覺得這很有用。

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}' />");
}
}
info at personalmis dot com
16 年前
似乎大家嘗試等比例縮放圖片的各種方式,如果能記得基本的代數,可能會更直接。

公式是 y = mx,其中 m 是直線的斜率。這是 y:x 的比率,或 m = y/x。

所以如果...

// x 和 y 的最大值
$y_max = 600;
$x_max = 800;

// 圖片尺寸
$y1 = 2000;
$x1 = 3000;

// 使用寬度來縮放
if ($x1 > $x_max)
{
// 找出斜率
$m = $y1/$x1;
// 將 x 邊設定為最大值
$x2 = $x_max;
// 將 y 邊設定為等比例尺寸
$y2 = $m * $x1;
}

新的等比例縮放圖片尺寸將會是 x2 = 800,y2 = 533 (四捨五入)。

若要從 y 邊進行縮放,只需反轉 x 和 y 的值。
geoff at spacevs dot com
15 年前
這個函式可以從字串中回傳 JPEG 圖片的寬度和高度,允許在不先將圖片寫入磁碟,或使用比較慢的 "imagecreatefromstring" 的情況下,檢索儲存在資料庫中的圖片尺寸。

<?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;
}
}
}
?>

執行此操作 10,000 次需要 0.43 秒,相較之下,使用 imagecreatefromstring/imagesx/imagesy 執行相同的操作大約需要 1.52 秒。

處理檔案時,請勿使用此方法來取代 getimagesize,getimagesize 快得多,只需 0.15 秒。
cloned at clonedmadman dot com
16 年前
我正在製作一個上傳時會調整圖片大小的腳本,然而,我正在製作一個多檔案上傳器,所以我遇到一個問題:如何有效率地取得圖片的高度和寬度,並將它們儲存在陣列中以便稍後調整大小。這是我所想到的方法

<?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 ) )
?>
diablx at hotmail dot com
20 年前
我很抱歉之前的其他腳本,但我在圖片調整大小方面犯了一個錯誤...這是可用的腳本!
<?
// 一些設定變數!
$maxWidth = 90;
$maxHeight = 90;
$maxCols = 8;
$webDir = "https://127.0.0.1/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>
<?
// 開啟 localDir
$dh = opendir($localDir);
while (false !== ($filename = readdir($dh))) {
$filesArray[] = $filename;
}

// 顯示和調整大小
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>
kazuya
10 年前
我製作了函式 img_resize($path,$tmp_name,$new_name,$new_width)
這可能會有用。

<?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;
}

?>
alexyam at live dot com
12 年前
我想要在儲存於資料庫中作為 blob 資料的 .SWF 檔案上使用 getimagesize(),但找不到簡單的解決方案,所以我建立了自己的解決方案。

我以 MIT 許可證發布此程式碼,以節省大家的時間

<?php
/*
----------------------------------------------------------------------
PHP Blob 資料作為檔案串流 v1.0 (C) 2012 Alex Yam <alexyam@live.com>
此程式碼以 MIT 許可證發布。
----------------------------------------------------------------------
[摘要]

一個簡單的 PHP 函式類別,使用串流封裝器來讀取和寫入 blob 資料作為檔案。

特別適用於執行 getimagesize() 以取得儲存在資料庫中作為 blob 資料的 .SWF Flash 檔案的寬度和高度。

已在 PHP 5.3.10 上測試。

----------------------------------------------------------------------
[使用範例]

//包含
include('./blob_data_as_file_stream.php');

//註冊串流封裝器
stream_wrapper_register("BlobDataAsFileStream", "blob_data_as_file_stream");

//從 Adobe 網站擷取 .SWF 檔案並將其儲存到變數中。
//請將此替換為您自己的從資料庫擷取 swf-blob-data 的程式碼。
$swf_url = 'http://www.adobe.com/swf/software/flash/about/flashAbout_info_small.swf';
$swf_blob_data = file_get_contents($swf_url);

//將 $swf_blob_data 儲存到資料串流
blob_data_as_file_stream::$blob_data_stream = $swf_blob_data;

//在資料串流上執行 getimagesize()
$swf_info = getimagesize('BlobDataAsFileStream://');
var_dump($swf_info);

----------------------------------------------------------------------
[使用輸出]

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);
}

}
?>
ajreading at classixshop dot com
19 年前
我寫的一小段程式碼,可將影像按比例調整大小至最大高度和寬度,然後顯示它

<?php
// 最大高度與寬度
$max_width = 100;
$max_height = 100;

// 你的 JPEG 檔案路徑

$upfile '/path/to/file.jpg';
Header("Content-type: image/jpeg");

$size = GetImageSize($upfile); // 讀取尺寸
$width = $size[0];
$height = $size[1];

// 按比例調整圖片大小至
// 上述指定的最大尺寸

$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;
}
// 增加記憶體限制以支援較大的檔案

ini_set('memory_limit', '32M');

// 建立新的圖片!
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
ImageJpeg($dst);
// 銷毀圖片
ImageDestroy($src);
ImageDestroy($dst);
?>
freecorvette at gmail dot com
7 年前
對於某些圖片,使用 getimagesize() 而不帶第二個參數會返回正確的資訊,但當您加入第二個參數時,它會返回 false。這很可能是一個錯誤(而且已經被回報為錯誤),但同時,如果您遇到這個問題,一個解決方法是使用 exif_read_data()。
Jesus Zamora
13 年前
返回一個包含 4 個元素的陣列。
索引 0 是圖片的寬度,以像素為單位。
索引 1 是圖片的高度,以像素為單位。
索引 2 是圖片類型的標誌

1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(小端位元組順序),8 = TIFF(大端位元組順序),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。

索引 3 包含 ' height="yyy" width="xxx" '
anonymous
16 年前
請注意,如果您指定一個遠端檔案(透過 URL)來檢查大小,PHP 會先將遠端檔案下載到您的伺服器。

如果您使用此函數來檢查使用者提供的圖片連結的大小,這可能會構成安全風險。惡意使用者可能會連結到一個非常大的圖片檔案,並導致 PHP 下載它。我不知道下載是否有任何檔案大小限制。但假設使用者提供了一個連結到幾 GB 大小的圖片?

如果能限制此函數執行的下載大小會很好。希望已經有一個預設值,並帶有一些合理的限制。
mail at soylentgreens dot com
19 年前
以下是如何裁切圖片...

<?php

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

// 建立兩個圖片
$origimg = imagecreatefromjpeg($imgfile);
$cropimg = imagecreatetruecolor($cropW,$cropH);

// 取得原始尺寸
list($width, $height) = getimagesize($imgfile);

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

// TODO: 撰寫程式碼來儲存新圖片
// 或者,直接像這樣顯示它:
header("Content-type: image/jpeg");
imagejpeg($cropimg);

// 銷毀圖片
imagedestroy($cropimg);
imagedestroy($origimg);

?>
user at example dot net
16 年前
在驗證圖片時,始終同時檢查圖片類型 *和* 檔案副檔名!

因為大多數圖片類型都允許包含註解或其他不相關資料的區段。這些區段可用於將 php 程式碼滲透到伺服器上。如果這些檔案以客戶端發送的方式儲存,則具有 ".php" 副檔名的檔案可以被執行並造成極大的危害。
pfarthing at hotmail dot com
16 年前
更正:要找到 $y2 應該是...

// 將 y 邊設定為等比例尺寸
$y2 = $m * $x_max; // 不是 $x1

謝謝 Norbert =)
Coodiss at w3bbix dot net
19 年前
這是一個簡單的方法來縮放圖片,以符合它們所在的 <td>
*這被拆分以便任何人都能理解它 :)

<?
$imageinfo = getimagesize("images/picture.jpg");

$ix=$imageinfo[0];
$iy=$imageinfo[1];

$widthscale = $ix/175; //<TD> 寬度
$heightscale = $iy/175; //<TD> 高度

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

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

?>
To Top