我如何在IMG_CROP_TRANSPARENT中使用imagecropauto()? [英] how do i use imagecropauto() with IMG_CROP_TRANSPARENT?
问题描述
当我尝试裁切图像的透明区域时,它会保持其原始大小,并且透明区域会变成黑色.
When I try to crop transparant area of an image, its get to keep it original size, and the transparant areas are turned black.
如果我运行以下代码:
<?php
// Create a 300x300px transparant image with a 100px wide red circle in the middle
$i = imagecreatetruecolor(300, 300);
imagealphablending($i, FALSE);
imagesavealpha($i, TRUE);
$transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
imagefill($i, 0, 0, $transparant);
$red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
imagefilledellipse($i, 150, 150, 100, 100, $red);
imagepng($i, "red_300.png");
// Crop away transparant parts and save
$i2 = imagecropauto($i, IMG_CROP_TRANSPARENT);
imagepng($i2, "red_crop_trans.png");
imagedestroy($i2);
// Crop away bg-color parts and save
$i2 = imagecropauto($i, IMG_CROP_SIDES);
imagepng($i2, "red_crop_sides.png");
imagedestroy($i2);
// clean up org image
imagedestroy($i);
我最终得到一个red_crop_trans.png
图像,它是一个300x300px
黑色图像,其中带有一个100x100px
红色圆圈.
还有一个red_crop_sides.png,它是一个100x100px
黑色图像,其中有一个100x100px
红色圆圈.
I end up with a red_crop_trans.png
image that is a 300x300px
black image with an 100x100px
red circle in it.
And a red_crop_sides.png that is a 100x100px
black image with a 100x100px
red circle in it.
为什么red_crop_trans.png不被裁剪为100x100px
?为什么两个图像的背景都是黑色的?以及如何在保持透明的同时裁剪它们?
Why is red_crop_trans.png not croped to 100x100px
? and why is the background of both images black? And how do I crop them while keeping the transparace?
推荐答案
我花了一段时间才弄清楚到底是怎么回事.事实证明$i2 = imagecropauto($i, IMG_CROP_TRANSPARENT);
返回的是false而不是true.根据文档:
It took me a while to figure out what exactly was going on. It turned out $i2 = imagecropauto($i, IMG_CROP_TRANSPARENT);
was returning false instead of true. According to the docs:
imagecropauto()在没有要裁剪的内容或没有内容的情况下返回FALSE 整个图像将被裁剪.
imagecropauto() returns FALSE when there is either nothing to crop or the whole image would be cropped.
所以我使用的是IMG_CROP_DEFAULT
而不是IMG_CROP_TRANSPARENT
So instead of IMG_CROP_TRANSPARENT
I used IMG_CROP_DEFAULT
:
尝试使用IMG_CROP_TRANSPARENT,如果失败,则退回到 IMG_CROP_SIDES.
Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.
这给了我预期的结果.现在我自己没有任何黑色背景.但这是一个已知问题,因此很容易找到解决方案:
This gave me the expected result. Now I didn't get any black backgrounds myself. But it's a known issue so the solution was quite easily found:
imagecolortransparent($i, $transparant); // Set background transparent
这使我进入了最终的完整代码:
And that brings me to the final completed code:
<?php
// Create a 300x300px transparant image with a 100px wide red circle in the middle
$i = imagecreatetruecolor(300, 300);
imagealphablending($i, FALSE);
imagesavealpha($i, TRUE);
$transparant = imagecolorallocatealpha($i, 0xDD, 0xDD, 0xDD, 0x7F);
imagecolortransparent($i, $transparant); // Set background transparent
imagefill($i, 0, 0, $transparant);
$red = imagecolorallocate($i, 0xFF, 0x0, 0x0);
imagefilledellipse($i, 150, 150, 100, 100, $red);
imagepng($i, "red_300.png");
// Crop away transparant parts and save
$i2 = imagecropauto($i, IMG_CROP_DEFAULT); //Attempts to use IMG_CROP_TRANSPARENT and if it fails it falls back to IMG_CROP_SIDES.
imagepng($i2, "red_crop_trans.png");
imagedestroy($i2);
// Crop away bg-color parts and save
$i2 = imagecropauto($i, IMG_CROP_SIDES);
imagepng($i2, "red_crop_sides.png");
imagedestroy($i2);
// clean up org image
imagedestroy($i);
?>
这篇关于我如何在IMG_CROP_TRANSPARENT中使用imagecropauto()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!