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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章