我试图在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中裁剪图像,以便实际删除边缘?
发布于 2014-10-02 22:47:30
您必须将您的角设置为透明,并将其保存为png (您可以使用一个php库,如wideimage或imagemagick,它将很容易为您做到这一点)。图像文件总是矩形的,如果它有另一个形状,那是因为其他部分是透明的。
发布于 2014-10-02 22:49:56
为了让它成为巴新..。这方面的第一个改变是:
header("Content-Type: image/png");然后这个(在代码的末尾):
imagepng($dest_image, NULL); //instead of imagejpeg()https://stackoverflow.com/questions/26170747
复制相似问题