此函式將影像大小從像素轉換為公分
<?
#$imagem - 影像來源
#$dpi - 要轉換的解析度,例如:72dpi 或 300dpi
function px2cm($image, $dpi) {
#從檔案或 URL 建立新影像
$img = ImageCreateFromJpeg($image);
#取得影像寬度 / 高度
$x = ImageSX($img);
$y = ImageSY($img);
#轉換為公分
$h = $x * 2.54 / $dpi;
$l = $y * 2.54 / $dpi;
#格式化數字,以千位分隔
$h = number_format($h, 2, ',', ' ');
$l = number_format($l, 2, ',', ' ');
#新增大小單位
$px2cm[] = $h."cm";
$px2cm[] = $l."cm";
#返回包含值的陣列
#$px2cm[0] = X
#$px2cm[1] = Y
return $px2cm;
}
$image = "C:\\inetpub\\wwwroot\\lab\\trata_img\\l0gik.jpg";
$dpi = 300;
$result = px2cm($image, $dpi);
print ($result[0]." x ".$result[1]);
?>