如何调整图像大小

发布于 2024-12-02 05:53:19 字数 348 浏览 0 评论 0原文

如何在 Linux 中减小图像大小。我为此使用了 ImageMagick。但它对于 gif 图像效果不佳。我将其用作:

调整图像大小

exec("convert $original -resize " . $width . 'x' . $height . ' ' . $destination);

减小图像尺寸

exec("convert $original -quality 80% $new_img");

如果您有任何方法减小图像尺寸,请告诉我。此代码适用于 jpg,但不适用于 gif 图像。

感谢您的帮助。

How to reduce image sizes in Linux. I have used ImageMagick for this. But it is not working well for gif images. I have used it as:

To resize image

exec("convert $original -resize " . $width . 'x' . $height . ' ' . $destination);

To reduce image size

exec("convert $original -quality 80% $new_img");

Please let me know if you have any way to reduce image size. This code works well for jpg, but not works well for gif images.

Appreciate your help.

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

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

发布评论

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

评论(4

江南月 2024-12-09 05:53:19

引用 Imagemagick 手册:

-quality value 
JPEG/MIFF/PNG compression level.

这意味着使用质量级别仅适用于上述图像类型,不包括 GIF。

对于 GIF,最简单的选择是使用 -colors 命令来减少图像中使用的颜色数量。结果的质量在很大程度上取决于您的初始图像包含的内容,我见过从 256 色减少到 16 色并没有导致明显的质量损失的情况,而在其他情况下,减少到 128 色会导致图像无法使用。你必须进行实验。

最后一点:您可以将 GIF 转换为 PNG 格式,并对生成的图像使用 -quality 命令。然而 PNG 的质量有点用词不当:

对于 MNG 和 PNG 图像格式,质量值设置 zlib 压缩
级别(质量/10)和过滤器类型(质量%10)。默认 PNG“质量”
是 75,这意味着带有自适应 PNG 过滤的压缩级别 7,除非
图像有一个颜色图,在这种情况下,这意味着压缩级别 7,没有 PNG
过滤。

因此,如果 PNG 的尺寸减小幅度小于 JPG,请不要感到惊讶,它只会提高无损 PNG 图像的压缩率。

Quoting from the Imagemagick manual:

-quality value 
JPEG/MIFF/PNG compression level.

That means that using the quality level will only work on the aforementioned image types, which do not include GIF.

For GIF's your easiest option is to use the -colors command to reduce the number of colors used in your image. The quality of the result depends very much on what your initial image contains, I have seen cases where a reduction from 256 to 16 colors did not cause significant quality loss, and others where a reduction to 128 colors rendered the image unusable. You'll have to experiment.

A last remark: you could transform your GIF to PNG format and use the -quality command on the resulting image. Quality on PNG's however is a bit of a misnomer:

For the MNG and PNG image formats, the quality value sets the zlib compression
level (quality / 10) and filter-type (quality % 10). The default PNG "quality"
is 75, which means compression level 7 with adaptive PNG filtering, unless the
image has a color map, in which case it means compression level 7 with no PNG
filtering.

So don't be surprised if the size reduction is less on PNG's than on JPG', it only improves compression of the lossless PNG image.

情绪失控 2024-12-09 05:53:19

要减小 GIF 图像的大小,您可以尝试将其转换为 PNG 或降低颜色深度。颜色深度是图像中使用的颜色数量。颜色越少意味着图像尺寸越小。

To reduce GIF images in size, you can try converting them to PNG or to reduce the color depth. The color depth is the number of colors used in the image. Less colors means a smaller image size.

绿光 2024-12-09 05:53:19

所以你可以使用GD
http://ua.php.net/manual/en/function.imagecopyresampled.php
<

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

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

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

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

So you can use GD
http://ua.php.net/manual/en/function.imagecopyresampled.php
<

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

// Set a maximum height and width
$width = 200;
$height = 200;

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

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

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

// Output
imagejpeg($image_p, null, 100);
?>
狼性发作 2024-12-09 05:53:19

以下内容适用于 JPEG 图像,并且可能适用于其他图像格式。给定的密​​度 (300) 相当高,适合保持文本可读,因此请根据您的情况进行调整。

convert input-file -density 300 -quality 50% output-file

The following works well for JPEG images and likely works for other image formats. The given density (300) is pretty high and suitable for keeping text readable, so adjust for your situation.

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