获取未压缩的图像大小
Getting the uncompressed image size
我有一个小的 PHP 脚本可以将图像文件转换为缩略图。我的上传器最大容量为 100MB,我想保留它。
问题是,当打开文件时,GD 将其解压缩,导致文件很大,导致 PHP 运行 内存不足 (Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64000 bytes)
)。我不想再增加超出此允许大小的内存。
我不关心图像,我可以让它显示默认缩略图,这很好。但是我确实需要一种方法来捕获图像太大时产生的错误 imagecreatefromstring(file_get_contents($file))
。
由于产生的错误是致命的,它不能被 try-catched,并且由于它是在一个命令中加载的,所以我无法继续关注它以确保它没有接近极限。在尝试处理图像之前,我需要一种方法来计算图像的大小。
有办法吗? filesize
不会工作,因为它给了我压缩后的大小...
我的代码如下:
$image = imagecreatefromstring(file_get_contents($newfilename));
$ifilename = 'f/' . $string . '/thumbnail/thumbnail.jpg';
$thumb_width = 200;
$thumb_height = 200;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// Image is wider than thumbnail.
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// Image is taller than thumbnail.
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $ifilename, 80);
在 re-sizing 之前尝试查看原始图像大小?也许将它乘以基于平均格式压缩的设定百分比?
$averageJPGFileRatio = 0.55;
$orgFileSize = filesize ($newfilename) * 0.55;
在做任何工作之前先看一下?
次要想法
这样计算:width * height * 3 = filesize
3 代表红色、绿色和蓝色值,如果您使用带有 alpha 通道的图像使用 4 而不是 3。这应该可以让您非常接近地估计位图大小。不考虑 header 信息,但在几个字节上应该可以忽略不计。
我有一个小的 PHP 脚本可以将图像文件转换为缩略图。我的上传器最大容量为 100MB,我想保留它。
问题是,当打开文件时,GD 将其解压缩,导致文件很大,导致 PHP 运行 内存不足 (Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64000 bytes)
)。我不想再增加超出此允许大小的内存。
我不关心图像,我可以让它显示默认缩略图,这很好。但是我确实需要一种方法来捕获图像太大时产生的错误 imagecreatefromstring(file_get_contents($file))
。
由于产生的错误是致命的,它不能被 try-catched,并且由于它是在一个命令中加载的,所以我无法继续关注它以确保它没有接近极限。在尝试处理图像之前,我需要一种方法来计算图像的大小。
有办法吗? filesize
不会工作,因为它给了我压缩后的大小...
我的代码如下:
$image = imagecreatefromstring(file_get_contents($newfilename));
$ifilename = 'f/' . $string . '/thumbnail/thumbnail.jpg';
$thumb_width = 200;
$thumb_height = 200;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// Image is wider than thumbnail.
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// Image is taller than thumbnail.
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $ifilename, 80);
在 re-sizing 之前尝试查看原始图像大小?也许将它乘以基于平均格式压缩的设定百分比?
$averageJPGFileRatio = 0.55;
$orgFileSize = filesize ($newfilename) * 0.55;
在做任何工作之前先看一下?
次要想法
这样计算:width * height * 3 = filesize
3 代表红色、绿色和蓝色值,如果您使用带有 alpha 通道的图像使用 4 而不是 3。这应该可以让您非常接近地估计位图大小。不考虑 header 信息,但在几个字节上应该可以忽略不计。