/    Sign up×
Community /Pin to ProfileBookmark

Easiest way to view image mime, size, dimensions, etc

I’ve looked through the manual and see no easy solution, most of the tutorials pertain to a file/image being uploaded. What is the easiest way I can display image info. I have thumbnails on one page and want all the image info underneath it.

$file = location/file.jpg

echo $mime // in format like JPG, GIF, BMP, etc
echo $size // In 123KB or 2.5MB format
echo $dimension_width // In pixel ratio 1234pixels
echo $dimension_height // In pixel ration 1234pixels

Can anyone help?

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — Gave me a little project for this evening. ?
[code=php]
<?php

class DirInfo
{
var $files;

function DirInfo($dir = "")
{
if(!empty($dir))
{
$this->dir = $dir;
}
}

function setDir($dir)
{
$this->dir = $dir;
}

function getFiles()
{
if(empty($this->dir))
{
user_error("No directory has been specified.", E_USER_WARNING);
return(FALSE);
}
if(!is_dir($this->dir))
{
user_error("{$this->dir} is not a directory", E_USER_WARNING);
return(FALSE);
}
$files = glob($this->dir."/*");
if($files === FALSE)
{
user_error("Unable to read directory {$this->dir}", E_USER_WARNING);
return(FALSE);
}
if(count($files) === 0)
{
user_error("No files found in directory {$this->dir}", E_USER_WARNING);
return(0);
}
$this->files = array();
foreach($files as $path)
{
if(is_dir($path))
{
continue;
}
$file = basename($path);
$this->files[$file]['size'] = filesize($path);
$parts = explode('.', $file);
if(count($parts) > 1)
{
$this->files[$file]['type'] = strtoupper(array_pop($parts));
}
else
{
$this->files[$file]['type'] = NULL;
}
$this->files[$file]['lastmod'] = filemtime($path);
// get image info if GD lib available
if(function_exists("getimagesize") and
($imgData = @getimagesize($path)) !== FALSE)
{
$this->files[$file]['width'] = $imgData[0];
$this->files[$file]['height'] = $imgData[1];
$this->files[$file]['html'] = $imgData[3];
}
else
{
$this->files[$file]['width'] = NULL;
$this->files[$file]['height'] = NULL;
$this->files[$file]['html'] = NULL;
}
}
return(count($this->files)); // return number of files
}
}

// SAMPLE USAGE:
echo "<html><head><title>File Info</title></head><body>n";
$dir = new DirInfo($_SERVER['DOCUMENT_ROOT']);
$result = $dir->getFiles();
if($result)
{
echo "<table class='files'>n";
echo "<tr><th>File</th><th>Type</th><th>Width</th><th>Height</th></tr>n";
foreach($dir->files as $file => $info)
{
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>n",
$file,
$info['type'],
(!empty($info['width'])) ? $info['width'] : "N/A",
(!empty($info['height'])) ? $info['height'] : "N/A");
}
echo "</table>n";
}
else
{
echo "<p>No files found!</p>n";
}
echo "</body></html";
?>
[/code]
Copy linkTweet thisAlerts:
@phpnstuffauthorJun 09.2006 — This seems too complicated for my brain!

I don't need it to check folders or echo tables. Just simple basic info, let me see if I can explain myself better.

$file_path = "image.jpg";

echo $file_type; JPG, GIF, BMP, etc.

echo $image_width; //pixels

echo $image_height; //pixels

echo $file_size; // 123KB or 12.5MB rounded up to the nearest tenth.

Thats it..no checking folders, I will specify the image name everytime and just echo the results in plain text.

THANKS NogDog, I feel bad now, hope you didn't stay up too much! ?
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — <edit>Never mind, after re-reading your reply I finally got the concept that you just want the info for a single file.</edit>
Copy linkTweet thisAlerts:
@phpnstuffauthorJun 09.2006 — Thanks, I will work with what you gave me, much appreciated!
Copy linkTweet thisAlerts:
@bokehJun 09.2006 — If you are just checking width, height, mime and size you can do that with [I]getimagesize()[/I] and [I]filesize()[/I].[code=php]<?php

$file = 'image.jpg';

$details = getimagesize($file);
echo 'Mime Type: '.$details['mime']."<br>n";
echo 'Size: '.round(filesize($file)/1024)." kilobytes<br>n";
echo 'Width: '.$details[0]."px<br>n";
echo 'Height: '.$details[1]."px<br>n";

?>[/code]
Result:[CODE]Mime Type: image/jpeg
Size: 13 kilobytes
Width: 355px
Height: 136px[/CODE]
Copy linkTweet thisAlerts:
@phpnstuffauthorJun 09.2006 — How do I tell it if its under 999KB to use "KB" if over display in 1.52MB?
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — Here you go: just the facts.
[code=php]
// assume file of interest is $file:
$type = strtoupper(array_pop(explode('.', $file)));
$sizeKb = round(filesize($file)/1024, 1); // kilobytes
list($width, $height) = getimagesize($file); // pixels
[/code]
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — If you are just checking width, height, mime and size you can do that with [I]getimagesize()[/I] and [I]filesize()[/I].[code=php]<?php

$file = 'image.jpg';

$details = getimagesize($file);
echo 'Mime Type: '.$details['mime']."<br>n";
echo 'Size: '.round(filesize($file)/1024)." kilobytes<br>n";
echo 'Width: '.$details[0]."px<br>n";
echo 'Height: '.$details[1]."px<br>n";

?>[/code]
Result:[CODE]Mime Type: image/jpeg
Size: 13 kilobytes
Width: 355px
Height: 136px[/CODE]
[/QUOTE]

Cool, I didn't catch that part about the 'mime' element of the getimagesize result array. Definitely use that instead of my kludge (assuming you're using PHP 4.3 or later).
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — How do I tell it if its under 999KB to use "KB" if over display in 1.52MB?[/QUOTE]
[code=php]
$bytes = filesize($file);
$fileSizeStr = ($bytes >= (1024 * 1024)) ?
round($bytes / (1024 * 1024), 1) . "KB" :
round($bytes / 1024, 1) . "MB";
[/code]
Copy linkTweet thisAlerts:
@bokehJun 09.2006 — I didn't catch that part about the 'mime' element of the getimagesize result[/QUOTE]It works correctly irrespective of file extension.
Copy linkTweet thisAlerts:
@phpnstuffauthorJun 09.2006 — I did it like this...?

[code=php]
$size = round(filesize($file)/1024);
if($size <= '999') { $size_type = 'KB'; } elseif($size >= '1000') { $size_type = 'MB'; }
$size = $size/1000;

[/code]


This will display like 4.583MB, but how do I tell it to stop after two spaces after the (.) period so it displays 4.58MB?
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — Use round() with 2 as the second parameter.

Also, note that a kilobyte is 1024 bytes (2^10), not 1000.
Copy linkTweet thisAlerts:
@bokehJun 09.2006 — note that a kilobyte is 1024 bytes (2^10), not 1000.[/QUOTE]That is a matter open to debate. Due to the binary nature of computers this misuse of the SI prefix has entered the mainstream but according to BIMP, IEC, IEEE and ISO (the bodies that set these international standards) it is incorrect.
Copy linkTweet thisAlerts:
@NogDogJun 09.2006 — That is a matter open to debate. Due to the binary nature of computers this misuse of the SI prefix has entered the mainstream but according to BIMP, IEC, IEEE and ISO (the bodies that set these international standards) it is incorrect.[/QUOTE]
So, let's avoid confusion and use the 1024 value with the KiB (Kibibyte) unit of measure. :p
×

Success!

Help @phpnstuff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...