【问题标题】:Crop image in PHP without mask在没有蒙版的 PHP 中裁剪图像
【发布时间】: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。或者,获取您的背景并将其与您的图像组合以进行合成。

标签: php image


【解决方案1】:

您必须将角设置为透明并将其另存为 png(您可以使用 php 库,如wideimage 或 imagemagick,它会为您轻松完成此操作)。图像文件总是矩形的,如果它有其他形状,那是因为其他部分是透明的。

【讨论】:

    【解决方案2】:

    仅供参考,使其成为png... 先改成这样:

    header("Content-Type: image/png");
    

    然后是这个(在代码的末尾):

    imagepng($dest_image, NULL);  //instead of imagejpeg()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多