Getting image size from URL in PHP - Automation

Hi There,

I have a mysql table with 1000 rows (all img URLs). I wanted to get the size for each of them in an automated way so I can do some operations depending on size. What is the best way to get the size of image (from Url) in PHP ?

Thanks,
Poly.
polythoughtAsked:
Who is Participating?
 
m1tk4Commented:
The problem with all solutions above is that they actually require you to download the file to find out its size. This is absolutely unnecessary, if you have CURL functionality in PHP.

You could just issue "HEAD" requests for these urls using CURLOPT_CUSTOMREQUEST CURL option, then parse the returned headers for the file size.
0
 
hernst42Commented:
Use the function http://de2.php.net/manual/en/function.getimagesize.php like

while ($row = mysql_fetc_array($q)) {
   $info = getimagesize($row['url']);
   // do you things
}
0
 
polythoughtAuthor Commented:
@hernst42

Thanks but I am looking for File Size..not dimensions..
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
hernst42Commented:
ok then replace getimagesize with filesize  http://www.php.net/filesize
0
 
polythoughtAuthor Commented:
When I write this program I got size (in bytes) with this url
<?php

//$filename = "D:/Easymyspace/4th july/1.gif";
$filename = "http://i186.photobucket.com/albums/x307/robin286/n092.gif";
echo $filename . ': ' . filesize($filename) . ' bytes';

?>


But when i change my url i am getting erro message "filesize() [function.filesize]: stat failed for http://i186.photobucket.com/albums/x307/robin286/n092.gif "

<?php


$filename = "http://i186.photobucket.com/albums/x307/robin286/n092.gif";

echo $filename . ': ' . filesize($filename) . ' bytes';

?>


0
 
hernst42Commented:
Yes thats because they have implemented a check which prevents automatic grabbing of images via such methods.
0
 
The_Blasted_OneCommented:
In the case of photobucket you'll need to emulate the browser's behaviour. You will need CURL extension for your PHP. It is a popular thing, so you have good chances that it is already installed on your server or it will be installed by admin on your demand.
Here's the code:

<?php
$res = curl_init();

curl_setopt($res, CURLOPT_NOBODY, TRUE);
curl_setopt($res, CURLOPT_URL, 'http://i186.photobucket.com/albums/x307/robin286/n092.gif');
curl_exec($res);

$size = curl_getinfo($res, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

curl_close($res);

echo $size;
?>

If you can't get CURL, it seems to me you'll get a headache. You may also try such weird thing as using "exec" command for Wget command string downloader (pretty popular on *nix), and then use "Content-length:" section from exec output string. But it is really weird path, hope you'll escape it  )))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.