Google Pagespeed可以将RGB图像转换为YUV 4:2:0色彩空间.我想在PHP中使用imagick做同样的事情.
简而言之,为什么需要4:2:0:
"此滤镜将jpeg图像的颜色采样减少到4:2:0.人类视觉对亮度变化的敏感度比色调或饱和度的变化更敏感,4:2:0颜色采样使我们能够保持亮度数据,同时减少色调和饱和度数据的数量为3/4.这可以显着减小图像尺寸,同时对感知的影响很小."
我试图用imagemagick将色彩空间改为YUV,但它根本不能正常工作!白色背景变为绿色,其余颜色错误,加上倒置亮度.
以下代码没用,但它显示了我尝试过的一些内容.
使用mod_pagespeed创建的Google格式,结果图像在浏览器中看起来很好.这就是我希望实现的,用于网络的图像.
$image_info = getimagesize($source_file);
$im = new Imagick();
$im->readImage($source_file);
$profiles = $im->getImageProfiles('*', false);
$has_icc_profile = (array_search('icc', $profiles) !== false);
if ($has_icc_profile === false) {
$icc_srgb = file_get_contents(Mage::getBaseDir('var') . DS . 'metodo' . DS . 'demo_data' . DS . 'AdobeRGB1998.icc');
$im->profileImage('icc', $icc_srgb);
unset($icc_srgb);
}
//$im->setImageColorspace(1);
$im->setInterlaceScheme(Imagick::INTERLACE_PLANE);
$im->setImageCompressionQuality(85);
//$im->stripImage();
$im->setImageColorspace(11);
$im->thumbnailImage($this->_imageSrcWidth, $this->_imageSrcHeight);
//$im->negateImage(false, Imagick::CHANNEL_ALL);
$im->stripImage();
$im->writeImage($fileName);
Run Code Online (Sandbox Code Playgroud)
我对该Imagick::setSamplingFactors方法缺乏文档感到好奇(参见我的评论),所以我试图弄明白.
使用该Imagick::identifyImage方法很明显,Imagick库不使用4:2:0表示法进行色度子采样,但类似于"1x1,1x1,1x1".在http://www.ftgimp.com/help/C/filters/jpeg.html(201180608:不再可用,在此存档),它明确表示'1x1,1x1,1x1'转换为4:4:4,并且'2x2,1x1,1x1'到4:2:0.由于该Imagick::setSamplingFactors方法需要一个数组作为其参数,我尝试了以下,它成功地工作:
$img = new Imagick($source_file)
$img->setSamplingFactors(array('2x2', '1x1', '1x1'));
$im->writeImage($fileName);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2610 次 |
| 最近记录: |