【发布时间】:2014-10-02 22:39:14
【问题描述】:
我正在尝试将 PHP 中的图像从正方形裁剪为圆形。我在网上看到了很多解决方案,它们通过掩盖初始图像并使角落变成不同的颜色来完成圆形图像的获取。然而,这是有问题的,因为将角设置为透明只会导致我的初始方形图像。例如,下面的代码会生成一个带有粉红色角的圆形图像
$image_name = $_POST['filepath'];
$source_image = imagecreatefrompng($image_name);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = $_POST['width'];
$dest_imagey = $_POST['height'];
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagealphablending($dest_image, true);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
header("Content-Type: image/jpeg");
// create masking
$mask = imagecreatetruecolor($source_imagex, $source_imagey);
$mask = imagecreatetruecolor($dest_imagex, $dest_imagey);
$pink = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $pink);
$black = imagecolorallocate($mask, 0, 0, 0);
imagecolortransparent($mask, $black);
imagefilledellipse($mask, $dest_imagex/2, $dest_imagey/2, $dest_imagex, $dest_imagey, $black);
imagecopy($dest_image, $mask, 0, 0, 0, 0, $dest_imagex, $dest_imagey);
imagecolortransparent($dest_image, $pink);
imagejpeg($dest_image, NULL);
有没有办法在 PHP 中裁剪图像以实际去除边缘?
【问题讨论】:
-
为此使用 CSS 有什么问题?
border-radius: 50%应该能满足你的需要。 -
我正在与 iOS 交互,所以我只需要返回圆形图像
-
那怎么了:
cell.yourImageView.layer.cornerRadius = cell.yourImageView.frame.size.height /2; cell.yourImageView.layer.masksToBounds = YES; cell.yourImageView.layer.borderWidth = 0;? -
JPEG 不支持透明度。改为输出 PNG
-
我从未遇到过圆形图像,并且我已经完成了相当多的图像工作。您需要将图像数据保持为矩形,并使用透明度或 BenM 建议的 CSS。或者,获取您的背景并将其与您的图像组合以进行合成。