How to get Image DPI in PHP
22,802
Solution 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... :)
Solution 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();
Solution 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;
}
Author by
John Smith
Updated on August 22, 2020Comments
-
John Smith almost 4 years
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.
-
Kris Khairallah over 10 yearsdoesn't work for all images :( try image saved from cs3, blackberry, iphone, Kodak,..
-
BlueMan over 10 yearsAutor was asking of DPI not image resolution.
-
Robbie Smith over 10 yearsYeah, I've had mixed results with that. Still looking for something better. For me whether its 72 dpi or 300 dpi, it still returns 72 dpi.
-
Felipe Marques over 7 yearsgood solution! thanks
-
Sagar Dobariya over 6 yearsit is get wrong dpi when image small it get 1 or 0 dpi if large image get 1072 dpi it is totally wrong
-
Sagar Dobariya over 6 yearsit gives perfect dpi but i need without use any librery
-
freedayum over 6 years@BlueMan DPI (Dots Per Inch) is resolution. No?.