如何将图像文件大小调整为可选大小

发布于 2024-08-05 17:18:12 字数 219 浏览 7 评论 0原文

我有图像上传表单,用户附加图像文件,然后选择图像大小来调整上传的图像文件的大小(200kb、500kb、1mb、5mb、原始)。然后我的脚本需要根据用户的可选大小调整图像文件大小,但我不确定如何实现此功能,

例如,用户上传一张 1mb 大小的图像,如果用户选择 200KB 调整大小,那么我的脚本应该保存它大小为 200kb。

有谁知道或有类似任务的经验吗?

感谢您提前回复。

I have image upload form, user attaches aimage file, and selects image size to resize the uploaded image file(200kb, 500kb, 1mb, 5mb, Original). Then my script needs to resize image file size based on user's optional size, but im not sure how to implement this feature,

For example, user uploads image with one 1mb size, and if user selects 200KB to resize, then my script should save it with 200kb size.

Does anyone know or have an experience on similar task ?

Thanks for you reply in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生来就爱笑 2024-08-12 17:18:12

对于 GD 库,使用 imagecopyresampled()

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

编辑:如果您想将图像文件的大小调整为指定的大小,那就有点困难了。所有主要图像格式都使用压缩,压缩率因压缩内容的性质而异。压缩晴朗的蓝天,你会得到比人海更好的压缩比。

您能做的最好的事情就是尝试特定的大小,然后查看文件大小,必要时进行调整。

Resize ratio = desired file size / actual file size
Resize multipler = square root (resize ratio)
New height = resize multiplier * actual height
New width = resize multiplier * actual width

这基本上是预期压缩比的近似值。我希望您有一定的容忍度(例如 +/- 5%),并且您可以根据需要调整数字。

没有直接的方法可以调整到特定的文件大小。最后我要补充一点,调整到特定文件大小是相当不寻常的。调整到特定的高度和/或宽度(保持纵横比)更为常见和期望(用户)。

更新:正如正确指出的那样,这会导致文件大小错误。当您应用两次(一次应用到高度,一次应用到宽度)时,该比率需要是文件大小比率的平方根。

With the GD library, use imagecopyresampled().

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);
?>

Edit: If you want to resize the image file to a specified size, that's a little harder. All the major image formats use compression and compression rates vary by the nature of what's being compressed. Compress clear blue sky and you'll get a better compression ratio than you will sea of people.

The best you can do is try a particular size is try a particular size and see what the file size is, adjusting if necessary.

Resize ratio = desired file size / actual file size
Resize multipler = square root (resize ratio)
New height = resize multiplier * actual height
New width = resize multiplier * actual width

This basically factors in an approximation of the expected compression ratio. I would expect that you would have some tolerance (like +/- 5%) and you can tweak the numbers as necessary.

There is no direct way to resize to a particular file size. Lastly I'll add that resizing to a particular file size is rather unusual. Resizing to a particular height and/or width (maintaining aspect ratio) is far more common and expected (by users).

Update: as correctly pointed out, this gets the file size wrong. The ratio needs to be the square root of the file size ratios as you're applying it twice (once to height, once to width).

热血少△年 2024-08-12 17:18:12

使用PHP中提供的GD库:

// $img is the image resource created by opening the original file
// $w and $h is the final width and height respectively.
$width = imagesx($img);$height = imagesy($img);
$ratio = $width/$height;

if($ratio > 1){
// width is greater than height
$nh = $h;
$nw = floor($width * ($nh/$height));
}else{
$nw = $w;
$nh = floor($height * ($nw/$width));
}

//centralize image
$nx = floor(($nw- $w) / 2.0);
$ny = floor(($nh-$h) / 2.0);

$tmp2 = imagecreatetruecolor($nw,$nh);
imagecopyresized($tmp2, $img,0,0,0,0,$nw,$nh,$width,$height);

$tmp = imagecreatetruecolor($w,$h);
imagecopyresized($tmp, $tmp2,0,0,$nx,$ny,$w,$h,$w,$h);

imagedestroy($tmp2);imagedestroy($img);

imagejpeg($tmp, $final_file);

这段代码将获取原始图像,调整大小到指定的尺寸。它将首先尝试按纵横比调整图像大小,然后裁剪+居中图像,使其很好地落入指定的尺寸。

Using the GD Library provided in PHP:

// $img is the image resource created by opening the original file
// $w and $h is the final width and height respectively.
$width = imagesx($img);$height = imagesy($img);
$ratio = $width/$height;

if($ratio > 1){
// width is greater than height
$nh = $h;
$nw = floor($width * ($nh/$height));
}else{
$nw = $w;
$nh = floor($height * ($nw/$width));
}

//centralize image
$nx = floor(($nw- $w) / 2.0);
$ny = floor(($nh-$h) / 2.0);

$tmp2 = imagecreatetruecolor($nw,$nh);
imagecopyresized($tmp2, $img,0,0,0,0,$nw,$nh,$width,$height);

$tmp = imagecreatetruecolor($w,$h);
imagecopyresized($tmp, $tmp2,0,0,$nx,$ny,$w,$h,$w,$h);

imagedestroy($tmp2);imagedestroy($img);

imagejpeg($tmp, $final_file);

This piece of code will take the original image, resize to the specified dimensions. It will first try to ratio aspect resize the image, then crop off + centralize the image, making it fall nicely into the dimensions specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文