Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: three plus one?
(Example: nine)

The Note You're Voting On

soapergem at gmail dot com
16 years ago
I came here looking for something similar to the getJPEGresolution function, but noticed the drawbacks that were pointed out in the last post. So, after drawing on some other code examples on the web, I put together the following function which should always properly return the correct values. (But remember that you still need to have the EXIF extension installed with your instance of PHP for this to work!)

<?php

function jpeg_dpi($filename)
{
if (
exif_imagetype($filename) != IMAGETYPE_JPEG ) {
return
false;
} else {
$exif = exif_read_data($filename, 'IFD0');
}

$x = $y = 0;
if ( isset(
$exif['XResolution']) && isset($exif['YResolution']) ) {
$x = intval(preg_replace('@^(\\d+)/(\\d+)$@e', '$1/$2', $exif['XResolution']));
$y = intval(preg_replace('@^(\\d+)/(\\d+)$@e', '$1/$2', $exif['YResolution']));
}

if ( !
$x && !$y && $fp = fopen($filename, 'r') ) {
$string = fread($fp, 20);
fclose($fp);

$data = bin2hex(substr($string, 14, 4));
$x = hexdec(substr($data, 0, 4));
$y = hexdec(substr($data, 4, 4));
}

if (
$x || $y ) {
return array(
$x, $y);
}

return
false;
}

?>

This function returns an array with the x-resolution, y-resolution when they can be determined, otherwise FALSE.

<< Back to user notes page

To Top