如何通过URL调整图像文件的大小? [英] How can I the size of an image file from the URL?

查看:361
本文介绍了如何通过URL调整图像文件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片和脚本托管在同一帐户(一个站点)上,但我们只知道图片的URL.

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?

伪代码:

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

我希望将$image_size设置为易于阅读的文件格式,例如"156,8 KB"或"20,1 MB".

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

推荐答案

使用 filesize 这样的功能:

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]); }
}

这篇关于如何通过URL调整图像文件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆