Question

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.

Was it helpful?

Solution

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... :)

OTHER TIPS

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();

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top