使用 GD 库将图像裁剪为完美正方形时出现问题
我使用一个类,它根据某些选项自动将图像裁剪为正方形。问题是,当图像具有一定的宽度和高度时,图像会被裁剪,但会在图像右侧添加一列 1px 的黑色像素。我认为问题出在用于生成新图像尺寸的数学中...也许当高度和宽度的除法给出十进制数时,正方形不完美并且添加了黑色像素...
任何解决方案?
这就是我调用该对象的方式:
$resizeObj = new resize($image_file); // *** 1) Initialise / load image
$resizeObj -> resizeImage(182, 182, 'crop'); // *** 2) Resize image
$resizeObj -> saveImage($destination_path, 92); // *** 3) Save image
我正在谈论的类的一部分:
private function getOptimalCrop($newWidth, $newHeight)
{
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}
$optimalHeight = $this->height / $optimalRatio;
$optimalWidth = $this->width / $optimalRatio;
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
{
// *** Find center - this will be used for the crop
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = 0; // start crop from top
$crop = $this->imageResized;
//imagedestroy($this->imageResized);
// *** Now crop from center to exact requested size
$this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
}
更新:
也许可以更改此:
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
这样:
$heightRatio = round($this->height / $newHeight);
$widthRatio = round($this->width / $newWidth);
I use a class that automatically crops an image as a square based on some options. The problem is that when the image is of a certain width and height, the image is cropped but a 1px column of black pixels is added to the right of the image. I think that the problem is in the mathematics used to generate the new image size... Maybe when the division of the height and the width gives a decimal number then the square is not perfect and the black pixels are added...
Any solution?
This is how I call the object:
$resizeObj = new resize($image_file); // *** 1) Initialise / load image
$resizeObj -> resizeImage(182, 182, 'crop'); // *** 2) Resize image
$resizeObj -> saveImage($destination_path, 92); // *** 3) Save image
The part of the class I'm talking about:
private function getOptimalCrop($newWidth, $newHeight)
{
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
if ($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
} else {
$optimalRatio = $widthRatio;
}
$optimalHeight = $this->height / $optimalRatio;
$optimalWidth = $this->width / $optimalRatio;
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
{
// *** Find center - this will be used for the crop
$cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
$cropStartY = 0; // start crop from top
$crop = $this->imageResized;
//imagedestroy($this->imageResized);
// *** Now crop from center to exact requested size
$this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
}
Update:
Maybe changing this:
$heightRatio = $this->height / $newHeight;
$widthRatio = $this->width / $newWidth;
with this:
$heightRatio = round($this->height / $newHeight);
$widthRatio = round($this->width / $newWidth);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这行:
看起来很可疑。
如果这是整数除法,那么对于奇数像素宽的图像,您将得到截断。尝试:
确保所有算术都使用实数,最好是双精度实数,但是对于您正在处理的范围内的数字,应该可以在单精度浮点数中工作。
This line:
looks suspicious.
If this is integer division then for images that are an odd number of pixels wide you'll get truncation. Try:
Make sure that all your arithmetic is using real numbers, preferably double precision ones, but with numbers in the range you are dealing with it should be OK to work in single precision floats.