Grafika合并多张图片为一张 支持多类型

//核心代码
首先用composer在项目里安装Grafika 在composer.json require里加入 “kosinix/grafika”: “dev-master”,然后composer update即可;类里use Grafika\Grafika;

$root =  $_SERVER['DOCUMENT_ROOT'];
            $num = strrpos($root,'/');
            $rootdir = substr($root,0,$num);
            $num = strrpos($rootdir,'/');
            $rootdir = substr($root,0,$num);
            if(count($goodsThumb)<=0){return json_encode(['code'=>-101,'message'=>'未接收到图片','data'=>'']);}
            $editor = Grafika::createEditor();
            $imageBack = Grafika::createBlankImage(1200, count($goodsThumb)*1200);//创建一个750*750的空白图像
            $backurl =$rootdir . '/backend/web/';
            foreach ($goodsThumb as $k=>$pic_path) {
                $dir_name =$backurl . $pic_path;
                $editor->open($image,$dir_name );//打开1.jpeg并且存放到$image1
                $editor->resizeFill($image, '1200','1200');//居中剪裁。就是把较短的变缩放到200px,然后将长边的大于200px的部分居中剪裁掉,图片不会变形
                $editor->blend($imageBack, $image, 'normal', 1, 'top-center',0,$k*1200);//将两个图像合成在一起,第一个图像作为基础,第二个图像在顶部。支持多种混合模式
                $num = strrpos($dir_name,'/');
                $savedir = substr($dir_name,0,$num);
                $savedir = $savedir . '/all_'.$ids.'.jpg';
                $file = $editor->save($imageBack, $savedir );//imageBack保存为all.jpg
            }
            if($file){
    //        $shorturl = str_replace($backurl,"",$savedir);
            $httpurl = $savedir;
            // var_dump($httpurl);die;
            $files = file_get_contents($httpurl);
            header("Content-type:application/octet-stream");
            $filename = pathinfo($httpurl, PATHINFO_BASENAME);
            header("Content-Disposition:attachment;filename=" . $filename);
            header("Accept-ranges:bytes");
            exit($files);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章