Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

php code for finding multiple images' dpi

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

php code for finding multiple images' dpi

Postby arjdat » Wed May 08, 2013 1:55 am

i have the following php code to find image's dpi

<?php
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,4,4);
return array(hexdec($x),hexdec($y));
}
print_r(get_dpi('C:/tv1.jpg'));
?>

how do i use this to find all images' dpi in a folder and output to html?
thanks in advance
arjdat
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed May 08, 2013 1:53 am

Re: php code for finding multiple images' dpi

Postby 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

Re: php code for finding multiple images' dpi

Postby arjdat » Fri May 10, 2013 9:03 am

hi sandeep
thank you so very much for your reply; it really helped a lot
problem is when i use the root folder (where i put my php code in the localhost dir) it's working fine
but i want to define another location for the images and want that dir to be read instead it's not working and shows all dpi as 0
i changed this
if ( $handle = opendir(".") ) { //if current directory
to this
if ( $handle = opendir("d:/jpg") ) { //if current directory

do i need to do something else as well

again thanks a ton
arjun datta
arjdat
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed May 08, 2013 1:53 am

Re: php code for finding multiple images' dpi

Postby 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.
User avatar
sandeep605085
New php-forum User
New php-forum User
 
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions

Re: php code for finding multiple images' dpi

Postby arjdat » Fri May 10, 2013 11:38 pm

hi sandeep
thanks a ton; this really helps...
sorry to bug you just a little more, is there any way it can work with tiff images?
it is giving good results for jpg and png's but with tiff it's giving some weird figures and there is no proportion i could find for them; like for 72 dpi it shows 54784 and for 325 dpi it shows 65281;
thanks for your kind help

regards
arjun datta
arjdat
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed May 08, 2013 1:53 am


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.