Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I the size of an image file from the URL?

Tags:

php

filesize

Images and script are hosted on the same account (one site), but we know only the URL of the image.

$image = "http://example.com/images/full-1091.jpg"

How can we get the size of this file?

Pseudo-code:

{ do something with $image and get $image_size }
echo $image_size;

I would prefer $image_size to be formatted in human-readable file sizes, like "156,8 Kbytes" or "20,1 Mbytes".

like image 539
James Avatar asked Aug 01 '10 09:08

James


People also ask

How do I find the image size for a URL?

Hover over the URL to see the pop up with all of the image properties. This pop up includes the Rendered size – the dimensions that the website needs – as well as the Intrinsic size – the dimensions of the originally image uploaded.

How do I find the file size of an image?

File size can be expressed as the resolution (the image width multiplied by the image height) multiplied by the bit depth (the number of bits needed to store colours).

Can you resize image in URL?

You can resize images by passing a size parameter in the query string portion of the URL. The size parameter replaces the larger of the height or width parameters.


1 Answers

Use filesize function like this:

echo filesize($filename) . ' bytes';

You can also format the unit of the size with this function:

function format_size($size) {
      $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      if ($size == 0) { return('n/a'); } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
like image 186
Sarfraz Avatar answered Oct 06 '22 21:10

Sarfraz