php base64转图片旋转 //裁切空白区域

class ImgSpin extends CI
{
    public $degrees;
    public $fileimg;
    public $pathFile; //保存的图片路径

    public function __construct()
    {
        $this->fileimg = '';  // 路径或者base64
        $this->degrees = 90; //逆时针针旋转
        $this->pathFile = false; //保存图片路径 如需保存图片 填写路径 默认返回base或替换原图
        parent::__construct();
    }
    /**  base64 转图片
     * @author shou
     * @Date: 2020/2/21
     * @return bool|string
     */
    public function Imgbase64()
    {
        if ($this->fileimg === '') return false;
//           if (strpos($this->fileimg,'data:image/png;base64,') === false) $this->fileimg = 'data:image/png;base64,'.$this->fileimg;
        $path = MBAPPPATH . 'public/document/Temporarypath/'; //临时路径
        if (!is_dir($path)) @mkdir($path);
        $FileName = date("Y_mdHis", time()) . "_" . substr(time(), -6) . '.png';
        $outputFile = $path . $FileName;
        $ifp = fopen($outputFile, "wb");
        fwrite($ifp, base64_decode($this->fileimg)); //保存base64图片
        fclose($ifp);

        $newImg = $this->SetImg($outputFile); //旋转图片
        //删除临时文件
        unlink($outputFile);
        ob_start();
        if ($this->pathFile === false) { //返回base
            imagepng($newImg);
            $imageString = base64_encode(ob_get_contents());
            unlink($newImg);
        } else {
//            if (!is_dir($this->outputFile)) @mkdir($this->pathFile);
            $imageString = imagepng($newImg, $this->pathFile);
        }
        ob_end_clean();
        return $imageString;
//            $imageString = 'data:image/png;base64,'.base64_encode(ob_get_contents());
        //生成新图
//            imagepng($newImg, $path.'ss.png');
    }

    /**本地图片路径旋转
     * @author shou
     * @Date: 2020/2/21
     */
    public function ImgPath()
    {
        if ($this->fileimg === '') return false;
        $newImg = $this->SetImg($this->fileimg);
        ob_start();
        if ($this->pathFile === false) { //替换原图
            $url = $this->fileimg;
        } else {
            $url = $this->pathFile;
        }
        $imageString = imagepng($newImg, $url);
        ob_end_clean();
        return $imageString;
    }

    private function SetImg($outputFile)
    {
        $ename = getimagesize($outputFile);
        $ename = explode('/', $ename['mime']);
        $ext = $ename[1];
        switch ($ext) {
            case "png":
                $srcImg = imagecreatefrompng($outputFile);
                break;
            case "jpeg":
                $srcImg = imagecreatefromjpeg($outputFile);
                break;
            case "jpg":
                $srcImg = imagecreatefromjpeg($outputFile);
                break;
            case "gif":
                $srcImg = imagecreatefromgif($outputFile);
                break;
        }
        $rotate = imagerotate($srcImg, $this->degrees, 0); //逆时针旋转
        //获取旋转后的宽高
        $srcWidth = imagesx($rotate);
        $srcHeight = imagesy($rotate);
        //创建新图
        $newImg = imagecreatetruecolor($srcWidth, $srcHeight);
        //分配颜色 + alpha,将颜色填充到新图上
        $alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
        imagefill($newImg, 0, 0, $alpha);
        //将源图拷贝到新图上,并设置在保存 PNG 图像时保存完整的 alpha 通道信息
        imagecopyresampled($newImg, $rotate, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
        imagesavealpha($newImg, true);
        return $newImg;
    }
//裁切空白区域
        public function pngMerge($o_pic)
        {
            $picture = imagecreatefrompng($o_pic);
            $img_w = imagesx($picture);
            $img_h = imagesy($picture);
            $color = imagecolorat( $picture, $img_w-1, 1); //背景色
            for( $x = 0; $x < $img_w; $x++ ) {
                for( $y = 0; $y < $img_h; $y++ ) {
                    $c = imagecolorat( $picture, $x, $y );
                    if($color!=$c){
                        $new_x[$x] = $x;
                        $new_y[$y] = $y;
                    }
                }
            }
            list($Max_x,$Min_x) = [max($new_x),min($new_x)];
            list($Max_y,$Min_y) = [max($new_y),min($new_y)];
            $Min_x -= 5; $Max_x+=5; $Min_y-=5;$Max_y+=5;  //左右预留5
            $des_w = $Max_x-$Min_x;
            $des_h = $Max_y-$Min_y;
//            $newImg_w = 134;
//            $newImg_h = 94;
            $dst_ims = imagecreatetruecolor($des_w, $des_h);//创建真彩画布
            $white = imagecolorallocate($dst_ims, 255, 255, 255);
            imagefill($dst_ims, 0, 0, $white);
            imagecopyresampled($dst_ims, $picture, 0, 0, $Min_x, 0, $des_w, $des_h, $des_w, $des_h);
            ob_start();
            imagepng($dst_ims);
            $imageString = 'data:image/png;base64,'.base64_encode(ob_get_contents());
            ob_end_clean();
            imagedestroy($dst_ims);
            imagedestroy($picture);
            return $imageString;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值