美文网首页
php 处理图片

php 处理图片

作者: 王宣成 | 来源:发表于2021-12-07 11:37 被阅读0次
<?php 
        $image = (new Image)->newImagick($src);
        $image->flopImage()
//            ->rollImage(20, 39)
//            ->thumbnailImage(600)
            ->markfFixed($src2,"center")  // top-left  top-right bottom-left bottom-right
//            ->gaussianblur(5,true)
//            ->sharpen(10,true)
//            ->raise(10,true)
//            ->oilpaint(10,true)
//            ->spread(10,true)
//            ->dobrush($src2,200,250)
//            ->rotate(90)
            ->frame("yellow",10,10,5,true);

       echo "<img src='{$image->getBase64Image()}'>");
<?php 
class Image extends Base
{

    public $image;

    public function newImagick($src)
    {
        $this->image = new \Imagick($src);
        return $this;
    }

    /**
     * 图片base64编码
     * @return string
     */
    public function getBase64Image($image = false)
    {
        $file = $this->image->getImageBlob();
        $type = getimagesizefromstring($file);
        $base64 = base64_encode($file);
        return 'data:' . $type['mime'] . ';base64,' . $base64;
    }

    /**
     * 图片等比例缩
     * @param $width
     * @return $this
     */
    public function thumbnailImage($width)
    {
        $srcWH = $this->image->getImageGeometry();
        if ($srcWH['width'] > $width) {
            $srcW['width'] = $width;
            $srcH['height'] = $srcW['width'] / $srcWH['width'] * $srcWH['height'];
        } else {
            $srcW['width'] = $srcWH['width'];
            $srcH['height'] = $srcWH['height'];
        }
        $this->image->thumbnailImage($srcW['width'], $srcH['height'], true);
        return $this;
    }

    /**
     * 图像水平横向翻转
     */
    public function flopImage()
    {
        $this->image->flopImage();
        return $this;
    }

    /**
     * 偏置图像
     * @param $x
     * @param $y
     * @return $this
     */
    public function rollImage($x, $y)
    {
        $this->image->rollImage($x, $y);
        return $this;
    }


    /**
     * 加水印
     * @param $src
     * @param $x
     * @param $y
     * @return $this
     * @throws \ImagickException
     */
    function mark($src, $x, $y)
    {
        $composite_object = new \Imagick($src);
        $this->image->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
        $this->image->setImageArtifact('compose:args', "1,0,-0.5,0.5");
        $this->image->compositeImage($composite_object, \Imagick::COMPOSITE_MATHEMATICS, $x, $y);
        return $this;
    }


    /**
     * 加水印固定
     */
    function markfFixed($src, $position = "center")
    {
        $srcWH = $this->image->getImageGeometry();
        $height = $srcWH['height'];
        $width = $srcWH['width'];

        $image = new \Imagick($src);
        $srcWH2 = $image->getImageGeometry();
        $height2 = $srcWH2['height'];
        $width2 = $srcWH2['width'];

        if ($position == 'top-left') {
            $x = 0;
            $y = 0;
        }

        if ($position == 'top-right') {
            $x = $width - $width2;
            $y = 0;
        }

        if ($position == 'bottom-left') {
            $x = 0;
            $y = $height - $height2;
        }

        if ($position == 'bottom-right') {
            $x = $width - $width2;
            $y = $height - $height2;
        }

        if ($position == 'center') {
            $x = ($width - $width2) / 2;
            $y = ($height - $height2) / 2;
        }

        $composite_object = new \Imagick($src);
        $this->image->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
        $this->image->setImageArtifact('compose:args', "1,0,-0.5,0.5");
        $this->image->compositeImage($composite_object, \Imagick::COMPOSITE_MATHEMATICS, $x, $y);
        return $this;
    }

    /**
     * 函数说明:模糊处理
     * 函数参数:
     * $radius:模糊程度,int型
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function gaussianblur($radius, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0)
            $apply = false;
        if ($apply) {
            $region = $this->image->getImageRegion($w, $h, $x, $y);
            $region->blurImage($radius, $radius);
            $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
            $region->destroy();
        } else {
            $this->image->blurImage($radius, $radius);
        }

        return $this;
    }

    /**
     * 函数说明:锐化处理
     * 函数参数:
     * $radius:锐化程度,int型
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function sharpen($radius, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0)
            $apply = false;
        if ($apply) {
            $region = $this->image->getImageRegion($w, $h, $x, $y);
            $region->sharpenImage($radius, $radius);
            $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
            $region->destroy();
        } else {
            $this->image->sharpenImage($radius, $radius);
        }

        return $this;
    }

    /**
     * 函数说明:突起效果
     * 函数参数:
     * $raise:突起度,int型
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function raise($raise, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0)
            $apply = false;
        if ($apply) {
            if ($w > (2 * $raise) && $h > (2 * $raise)) {
                $region = $this->image->getImageRegion($w, $h, $x, $y);
                $region->raiseImage($raise, $raise, 0, 0, true);
                $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
                $region->destroy();
            }
        } else {
            $info = $this->image->getImageGeometry();
            if ($info["width"] > (2 * $raise) && $info["height"] > (2 * $raise)) {
                $this->image->raiseImage($raise, $raise, 0, 0, true);
            }
        }
        return $this;
    }

    /**
     * 函数说明:边框效果
     * 函数参数:
     * $frame_width:边框宽度,int型
     * $frame_height:边框宽度,int型
     * $bevel:边框角度,int型
     * $color:边框颜色,string型
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function frame($color, $frame_width, $frame_height, $bevel, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0) {
            $apply = false;
        }
        $framecolor = new \ImagickPixel($color);
        if ($apply) {
            $region = $this->image->getImageRegion($w, $h, $x, $y);
            $region->frameImage($framecolor, $frame_width, $frame_height, $bevel,
                $bevel);
            $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
            $region->destroy();
        } else {
            $this->image->frameImage($framecolor, $frame_width, $frame_height, $bevel, $bevel);
        }

        return $this;
    }

    /**
     * 函数说明:油画效果
     * 函数参数:
     * $radius:油画效果参数
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function oilpaint($radius, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0) {
            $apply = false;
        }
        if ($apply) {
            $region = $this->image->getImageRegion($w, $h, $x, $y);
            $region->oilPaintImage($radius);
            $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
            $region->destroy();
        } else {
            $this->image->oilPaintImage($radius);
        }
        return $this;
    }

    /**
     * 函数说明:发散效果
     * 函数参数:
     * $radius:发散效果参数
     * $apply:表示作用区域,逻辑型,true:局部作用; false:全局作用
     * $w,$h,$x,$y:当$apply为true,来确定区域坐标,int型
     */
    function spread($radius, $apply, $x = 0, $y = 0, $w = 0, $h = 0)
    {
        if ($apply && $x == 0 && $y == 0 && $w == 0 && $h == 0)
            $apply = false;
        if ($apply) {
            $region = $this->image->getImageRegion($w, $h, $x, $y);
            $region->spreadImage($radius);
            $this->image->compositeImage($region, $region->getImageCompose(), $x, $y);
            $region->destroy();
        } else {
            $this->image->spreadImage($radius);
        }

        return $this;
    }

    /**
     * 函数说明:合并图片
     */
    function dobrush($src, $x, $y)
    {
        $imagepng = new \Imagick($src);
        $imagepng->setImageFormat("png");
        $this->image->compositeImage($imagepng, $imagepng->getImageCompose(), $x, $y);
        return $this;
    }

    /**
     * 函数说明:旋转图片
     * $angle:旋转角度,int型
     */
    function rotate($angle)
    {
        $this->image->rotateImage(new \ImagickPixel(), $angle);
        return $this;
    }

}

相关文章

网友评论

      本文标题:php 处理图片

      本文链接:https://www.haomeiwen.com/subject/olwoxrtx.html