php code for finding multiple images' dpi

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: php code for finding multiple images' dpi

Post by sandeep605085 » Wed May 08, 2013 10:18 am

Hi,

Please check below code if this works for you.

<?php
function get_dpi(){
//valid image extensions (add as many image extensions you have)
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";

if ( $handle = opendir(".") ) { //if current directory

//loop through all files of the directory
while ( false !== ( $file = readdir($handle) ) ) {

//if the file is a valid image
if ( eregi($pattern, $file) ) {
$a = fopen($file, 'r');
$string = fread($a, 20);
fclose($a);

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

closedir($handle);
}
}

get_dpi();
?>

Thanks,
Sandeep Agrawal
Mindfire Solutions.

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: php code for finding multiple images' dpi

Post by sandeep605085 » Fri May 10, 2013 9:48 am

Hi,

Try below.

<?php
function get_dpi(){
//valid image extensions (add as many image extensions you have)
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
$dirname = 'd:/jpg';

if ( $handle = opendir($dirname) ) { //if current directory

//loop through all files of the directory
while ( false !== ( $file = readdir($handle) ) ) {
if ($file != "." && $file != "..")
{


$file = $dirname . '/' . $file;

//if the file is a valid image
if ( eregi($pattern, $file) ) {
$a = fopen($file, 'r');
$string = fread($a, 20);
fclose($a);

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

get_dpi();
?>

Code highlighted as bold in the above code is the new i added to previous one to make it compatible with any directory name you give.

Thanks,
Sandeep Agrawal
Mindfire Solutions.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest