问题
I am searching for the code which could help me to get the Image DPI in PHP.
Could any one look into this ?
Thanks in advance.
回答1:
You can go for some image libraries for that. Eg: Imagick, GD Library...
(OR)
You can use the following function,
function get_dpi($filename){
$a = fopen($filename,'r');
$string = fread($a,20);
fclose($a);
$data = bin2hex(substr($string,14,4));
$x = substr($data,0,4);
$y = substr($data,0,4);
return array(hexdec($x),hexdec($y));
}
Already solved this question here... :)
回答2:
I believe that doing a custom php won't convers all type of images.
The best way to install Imagick:
$image = new Imagick($filename);
$resolutions = $image->getImageResolution();
回答3:
with ImageMagick
function getDPIImageMagick($filename){
$cmd = 'identify -quiet -format "%x" '.$filename;
@exec(escapeshellcmd($cmd), $data);
if($data && is_array($data)){
$data = explode(' ', $data[0]);
if($data[1] == 'PixelsPerInch'){
return $data[0];
}elseif($data[1] == 'PixelsPerCentimeter'){
$x = ceil($data[0] * 2.54);
return $x;
}elseif($data[1] == 'Undefined'){
return $data[0];
}
}
return 72;
}
来源:https://stackoverflow.com/questions/17088718/how-to-get-image-dpi-in-php