2024 年 PHP 日本研討會

imagesx

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

imagesx取得影像寬度

說明

imagesx(GdImage $image): int

回傳指定 image 物件的寬度。

參數

image

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

返回值

返回 image 的寬度。

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源

範例

範例 #1 使用 imagesx()

<?php

// 建立一個 300*200 的影像
$img = imagecreatetruecolor(300, 200);

echo
imagesx($img); // 300

?>

參見

新增註釋

使用者貢獻的註釋 1 則註釋

leonardo AT saochico DOT com
21 年前
此函式將影像大小從像素轉換為公分

<?
#$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]);
?>
To Top