How to get Image DPI in PHP [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

This question already has an answer here:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!