【问题标题】:image cropping selected position using imagick php extension使用 imagick php 扩展裁剪选定位置的图像
【发布时间】:2012-09-15 17:07:47
【问题描述】:

有没有办法在 PHP imagick 扩展中裁剪选择位置的图像?

我在 php 5.3+ 中使用 imagick php 扩展。

如果有人知道如何像下面这样裁剪位置,请与我分享

我有一个 500 像素正方形的图像,我想将顶部 200 像素(左侧)裁剪到右侧底部 400 像素。

请查看下面我的示例。

示例..

Example original

输出..

Output should look like this

【问题讨论】:

    标签: php imagick


    【解决方案1】:

    我希望这段代码会有所帮助。

    $mask = new Imagick();
    $mask->newimage(500, 500, new ImagickPixel('transparent'));
    $mask->setimageformat('png');
    $polygon = new ImagickDraw();
    $polygon->setFillColor(new ImagickPixel('black'));
    /*
      You didn't specified from where to start the crop and where to end it, 
      so just assumed those values
    */
    $polygon->polygon(array(
        array('x' => 200, 'y' => 0),
        array('x' => 400, 'y' => 0),
        array('x' => 450, 'y' => 500),
        array('x' => 250, 'y' => 500),
        array('x' => 200, 'y' => 0),
    ));
    $mask->drawimage($polygon);
    $image = new Imagick();
    $image->readimage("Your source image path :) ");
    $image->setImageFormat('png');
    $image->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
    $image->setImageMatte(true);
    $image->compositeimage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
    $image->trimimage(0);
    header('Content-Type: image/png');
    echo $image;
    die();
    

    【讨论】:

    • 任何想法如何保持图像的透明度?
    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多