Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

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.
0
polythought
Asked:
polythought
3 Solutions
 
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
 
hernst42Commented:
ok then replace getimagesize with filesize  http://www.php.net/filesize
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now