该问题与 lib/Varien/Image/Adapter/Gd2.php 内的调整大小函数中的 php 函数 imagecopyresampled 有关,在平滑调整大小的图像时会出现一些舍入问题。
我的解决方案是在调整图像大小后将图像中任何非常浅的灰色像素更改为纯白色。为此,首先将 lib/Varien/Image/Adapter/Gd2.php 复制到 app/code/local/Varien/Image/Adapter/Gd2.php
接下来在 resize 函数中找到以下代码(第 312 行左右)
// resample source image and copy it into new frame
imagecopyresampled(
$newImage,
$this->_imageHandler,
$dstX, $dstY,
$srcX, $srcY,
$dstWidth, $dstHeight,
$this->_imageSrcWidth, $this->_imageSrcHeight
);
然后在下面添加以下代码:
// Clean noise on white background images
if ($isTrueColor) {
$colorWhite = imagecolorallocate($newImage,255,255,255);
$processHeight = $dstHeight+$dstY;
$processWidth = $dstWidth+$dstX;
//Travel y axis
for($y=$dstY; $y<($processHeight); ++$y){
// Travel x axis
for($x=$dstX; $x<($processWidth); ++$x){
// Change pixel color
$colorat=imagecolorat($newImage, $x, $y);
$r = ($colorat >> 16) & 0xFF;
$g = ($colorat >> 8) & 0xFF;
$b = $colorat & 0xFF;
if(($r==253 && $g == 253 && $b ==253) || ($r==254 && $g == 254 && $b ==254)) {
imagesetpixel($newImage, $x, $y, $colorWhite);
}
}
}
}
从 Magento 的缓存管理中刷新图像缓存,您应该可以为新显示器提供更好的图像。实现此操作时需要注意的事项很少,第一次再次生成图像时性能会受到很小的影响,并且带有阴影的图像可能具有更清晰的边缘,因为非常浅的灰色已被删除。