我需要使用imagick和php将TIFF格式的上传图像调整为小于95 kB的特定文件大小。有人可以告诉我最好的方法来使大小小于95kb吗?
我正在使用下面的代码,但是有时使用此代码,我得到的图像大小大于95kb?
$filename = "test.jpg";
$image = new Imagick($filename);
$image->setImageCompressionQuality(int(40));
$img = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename).".tiff";
$image->writeImage($img);
答案 0 :(得分:1)
Imagick具有此功能。
$filename = "test.jpg";
$image = new Imagick($filename);
// Setting your default compression
$compression_value = int(40);
// Imagick needs to know how to compress
$image->setImageCompression(COMPRESSION_JPEG);
$image->setImageCompressionQuality($compression_value);
// getImageLength gets the length of the file in bytes.
while ($image->getImageLength() > 95000) {
$compression_value = $compression_value +1;
$image->setImageCompressionQuality($compression_value);
}
$img = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename).".tiff";
$image->writeImage($img);
注意:这就是说,您可能要设置最大循环数,因为它可以使压缩水平越来越高,而收益越高则递减率越高。通常您不会超过100,所以这是我的建议。如果无法将图像 压缩到95k,则可能要在某个位置停止。
答案 1 :(得分:0)
基于我在Danack和我之间的另一个问题(尚未进行测试,仍在进行中)的讨论,给出了一个更为有效的答案(但更为复杂)。
$filename = "test.jpg";
$image = new Imagick($filename);
// Setting your default compression
$height = $image->getImageHeight();
$width = $image->getImageWidth();
$raw_size = $height * $width;
// Imagick needs to know how to compress
$image->setImageCompression(COMPRESSION_JPEG);
$image->setImageCompressionQuality($compression_value);
// 80 compression shaves off about 40% of file size. Using that to estimate
// 1% reduction for every 2 compression. This should be refined
// as more data is gained for your specific scenario.
$compression_value = int(190/$raw_size);
// getImageLength gets the length of the file in bytes.
loop_refine($compression_value, $image);
while () {
}
// This reduces our size just a little more
$image->stripImage();
$img = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename).".tiff";
$image->writeImage($img);
function loop_refine(&$compression_value, &$image){
if ($image->getImageLength() > 95000 && $compression_value < 101){
reduce_image_size($compression_value, $image);
} elseif ($image->getImageLength() > 92000 && $compression_value < 101){
increase_image_size($compression_value, $image);
} else {
return;
}
}
function reduce_image_size(&$compression_value, &$image){
$compression_value = $compression_value + int((101 - $compression_value)/2);
$image->setImageCompressionQuality($compression_value);
loop_refine($compression_value, $image);
}
function increase_image_size(&$compression_value, &$image){
$compression_value = $compression_value - int((101- $compression_value)/4);
$image->setImageCompressionQuality($compression_value);
loop_refine($compression_value, $image);
}
答案 2 :(得分:0)
尝试此代码...
// File and new size image
$filename = 'imagename.jpg';
$percent = 0.4;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes image
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load image
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize image
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// result
imagejpeg($thumb);