我最近发现Imagick可以支持色彩配置文件,因此与GD相比可以产生更高质量的图像(有关更多详细信息,请参见此问题 / 答案),因此我正在尝试将我的GD包装器移植为使用Imagick类,我的当前GD实现如下:
function Image($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
{
if (isset($input, $output) === true)
{
if (is_string($input) === true)
{
$input = @ImageCreateFromString(@file_get_contents($input));
}
if (is_resource($input) === true)
{
$size = array(ImageSX($input), ImageSY($input));
$crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
$scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));
if (count($crop) == 2)
{
$crop = array($size[0] / $size[1], $crop[0] / $crop[1]);
if ($crop[0] > $crop[1])
{
$size[0] = round($size[1] * $crop[1]);
}
else if ($crop[0] < $crop[1])
{
$size[1] = round($size[0] / $crop[1]);
}
$crop = array(ImageSX($input) - $size[0], ImageSY($input) - $size[1]);
}
else
{
$crop = array(0, 0);
}
if (count($scale) >= 1)
{
if (empty($scale[0]) === true)
{
$scale[0] = round($scale[1] * $size[0] / $size[1]);
}
else if (empty($scale[1]) === true)
{
$scale[1] = round($scale[0] * $size[1] / $size[0]);
}
}
else
{
$scale = array($size[0], $size[1]);
}
$image = ImageCreateTrueColor($scale[0], $scale[1]);
if (is_resource($image) === true)
{
ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
ImageSaveAlpha($image, true);
ImageAlphaBlending($image, true);
if (ImageCopyResampled($image, $input, 0, 0, round($crop[0] / 2), round($crop[1] / 2), $scale[0], $scale[1], $size[0], $size[1]) === true)
{
$result = false;
if ((empty($sharp) !== true) && (is_array($matrix = array_fill(0, 9, -1)) === true))
{
array_splice($matrix, 4, 1, (is_int($sharp) === true) ? $sharp : 16);
if (function_exists('ImageConvolution') === true)
{
ImageConvolution($image, array_chunk($matrix, 3), array_sum($matrix), 0);
}
}
if ((isset($merge) === true) && (is_resource($merge = @ImageCreateFromString(@file_get_contents($merge))) === true))
{
ImageCopy($image, $merge, round(0.95 * $scale[0] - ImageSX($merge)), round(0.95 * $scale[1] - ImageSY($merge)), 0, 0, ImageSX($merge), ImageSY($merge));
}
foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
{
if (preg_match('~' . $key . '$~i', $output) > 0)
{
$type = str_replace('?', '', $key);
$output = preg_replace('~^[.]?' . $key . '$~i', '', $output);
if (empty($output) === true)
{
header('Content-Type: image/' . $type);
}
$result = call_user_func_array('Image' . $type, array($image, $output, $value));
}
}
return (empty($output) === true) ? $result : self::Chmod($output);
}
}
}
}
else if (count($result = @GetImageSize($input)) >= 2)
{
return array_map('intval', array_slice($result, 0, 2));
}
return false;
}
我一直在尝试使用Imagick类的方法,目前为止这是我得到的结果:
function Imagick($input, $crop = null, $scale = null, $merge = null, $output = null, $sharp = true)
{
if (isset($input, $output) === true)
{
if (is_file($input) === true)
{
$input = new Imagick($input);
}
if (is_object($input) === true)
{
$size = array_values($input->getImageGeometry());
$crop = array_values(array_filter(explode('/', $crop), 'is_numeric'));
$scale = array_values(array_filter(explode('*', $scale), 'is_numeric'));
if (count($crop) == 2)
{
$crop = array($size[0] / $size[1], $crop[0] / $crop[1]);
if ($crop[0] > $crop[1])
{
$size[0] = round($size[1] * $crop[1]);
}
else if ($crop[0] < $crop[1])
{
$size[1] = round($size[0] / $crop[1]);
}
$crop = array($input->getImageWidth() - $size[0], $input->getImageHeight() - $size[1]);
}
else
{
$crop = array(0, 0);
}
if (count($scale) >= 1)
{
if (empty($scale[0]) === true)
{
$scale[0] = round($scale[1] * $size[0] / $size[1]);
}
else if (empty($scale[1]) === true)
{
$scale[1] = round($scale[0] * $size[1] / $size[0]);
}
}
else
{
$scale = array($size[0], $size[1]);
}
$image = new IMagick();
$image->newImage($scale[0], $scale[1], new ImagickPixel('white'));
$input->cropImage($size[0], $size[1], round($crop[0] / 2), round($crop[1] / 2));
$input->resizeImage($scale[0], $scale[1], Imagick::FILTER_LANCZOS, 1); // $image->scaleImage($scale[0], $scale[1]);
//if (in_array('icc', $image->getImageProfiles('*', false)) === true)
{
$version = preg_replace('~([^-]*).*~', '$1', ph()->Value($image->getVersion(), 'versionString'));
if (is_file($profile = sprintf('/usr/share/%s/config/sRGB.icm', str_replace(' ', '-', $version))) !== true)
{
$profile = 'http://www.color.org/sRGB_v4_ICC_preference.icc';
}
if ($input->profileImage('icc', file_get_contents($profile)) === true)
{
$input->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}
}
$image->compositeImage($input, Imagick::COMPOSITE_OVER, 0, 0);
if ((isset($merge) === true) && (is_object($merge = new Imagick($merge)) === true))
{
$image->compositeImage($merge, Imagick::COMPOSITE_OVER, round(0.95 * $scale[0] - $merge->getImageWidth()), round(0.95 * $scale[1] - $merge->getImageHeight()));
}
foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
{
if (preg_match('~' . $key . '$~i', $output) > 0)
{
$type = str_replace('?', '', $key);
$output = preg_replace('~^[.]?' . $key . '$~i', '', $output);
if (empty($output) === true)
{
header('Content-Type: image/' . $type);
}
$image->setImageFormat($type);
if (strcmp('jpeg', $type) === 0)
{
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality($value);
$image->stripImage();
}
if (strlen($output) > 0)
{
$image->writeImage($output);
}
else
{
echo $image->getImageBlob();
}
}
}
return (empty($output) === true) ? $result : self::Chmod($output);
}
}
else if (count($result = @GetImageSize($input)) >= 2)
{
return array_map('intval', array_slice($result, 0, 2));
}
return false;
}
基本功能(裁剪/调整大小/水印)已经支持,但是我仍然遇到一些问题。由于PHP Imagick文档有点糟糕,我别无选择,只能尝试所有可用方法和参数的组合,这需要很长时间。
我目前的问题/疑虑是:
1 - 保持透明度
在我的原始实现中,以下行:
ImageFill($image, 0, 0, IMG_COLOR_TRANSPARENT);
ImageSaveAlpha($image, true);
ImageAlphaBlending($image, true);
当你将一个透明的PNG图像转换成PNG输出时,具有保留透明度的效果。然而,如果你尝试将透明的PNG图像转换成JPEG格式,则透明像素应该被设置为白色。到目前为止,使用ImageMagick,我只能将所有透明像素转换成白色,但如果输出格式支持透明度,则无法保留透明度。
2 - 压缩输出格式(即JPEG和PNG)
我的原始实现在PNG上使用9级压缩,而在JPEG上使用90的质量:
foreach (array('gif' => 0, 'png' => 9, 'jpe?g' => 90) as $key => $value)
这行代码:
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality($value);
$image->stripImage();
看起来能够压缩JPEG图像 - 但是,使用相同的$value
作为质量参数时,GD可以更有效地压缩它 - 为什么?我对以下区别也不清楚:
Imagick::setCompression()
/Imagick::setImageCompression()
以及Imagick::setCompressionQuality()
/Imagick::setImageCompressionQuality()
我应该使用哪一个?它们有什么区别?此外,最关键的问题与PNG压缩有关,Imagick压缩常量列表似乎不支持PNG格式:
imagick::COMPRESSION_UNDEFINED (integer)
imagick::COMPRESSION_NO (integer)
imagick::COMPRESSION_BZIP (integer)
imagick::COMPRESSION_FAX (integer)
imagick::COMPRESSION_GROUP4 (integer)
imagick::COMPRESSION_JPEG (integer)
imagick::COMPRESSION_JPEG2000 (integer)
imagick::COMPRESSION_LOSSLESSJPEG (integer)
imagick::COMPRESSION_LZW (integer)
imagick::COMPRESSION_RLE (integer)
imagick::COMPRESSION_ZIP (integer)
imagick::COMPRESSION_DXT1 (integer)
imagick::COMPRESSION_DXT3 (integer)
imagick::COMPRESSION_DXT5 (integer)
这真是个大问题,因为使用 Imagick 输出具有 100-200 KB 大小的 GD PNG 输出时,输出结果会变得非常臃肿(大小约为 2 MB)...
关于此问题在 SO 网站上有一些 问题 讨论 和回答,但我还没有找到任何不依赖外部应用程序的可行解决方案。使用 ImageMagick 真的做不到吗?!
3 - 图像卷积
在GD的实现中,我调用ImageConvolution()
来稍微锐化图像,我知道Imagick有内置方法来锐化图像(我还没有尝试过),但我想知道Imagick是否有ImageConvolution()
函数的等效方法。
4 - 色彩配置文件
这与原始实现无关,但我也希望做得正确。
我是否应该在所有图像中始终添加Imagick / International Color Consortium sRGB颜色配置文件?还是只有在存在(或不存在)特定的颜色配置文件时才添加?
此外,我应该删除现有的颜色配置文件吗?
我理解这可能是一个广泛的问题,但我对颜色配置文件的了解非常有限,因此对此的一些一般指导将不胜感激。
5 - 打开远程图片
GD原生支持打开远程图片,可以通过ImageCreateFrom*
函数或者使用file_get_contents()
与ImageCreateFromString()
结合使用,就像我所做的一样。
Imagick似乎只能打开本地图片或打开文件句柄。有没有简单的方法让Imagick读取远程图片(而不必打开和关闭文件句柄)?
如果有人能够解答这些问题中的任意一个,我将不胜感激。
提前致谢!