ImageMagick生成PNG和PDF

这段代码的作用是根据JSON串中的相关信息将文字和图片覆盖的背景图上并生成PNG和PDF

            $bg_url = $one_data['front_bg'];
            $page = $one_data['taili_subid'] + 1;
            $imgs = isset($one_data['front_img']) ? $one_data['front_img'] : false;
            $fronts = isset($one_data['front_fields']) ? $one_data['front_fields'] : false;

            if(empty($bg_url)) continue;
            $bg_ext = get_ext($bg_url);

            $bg_path = './temp/bg_'.$page.$bg_ext;
            file_put_contents($bg_path, file_get_contents($bg_url));
            //获取背景图片的信息
            $bg_info = getimagesize($bg_path);
            if(empty($bg_info)) continue;

            $bg_width = $bg_info[0];
            $bg_height = $bg_info[1];

            $fg_imgs = array();
            if(!empty($one_data['front_img'])){
                $n = 1;
                foreach($one_data['front_img'] as $fimg){
                    $file_name = $fimg['url'];
                    $file_ext = get_ext($fimg['url']);
                    file_put_contents('./temp/fg_'.$page.'_'.$n.$file_ext, file_get_contents($file_name));
                    $fg_imgs[] = array('path'=>'./temp/fg_'.$page.'_'.$n.$file_ext,'data'=>$fimg);
                    ++$n;
                }
            }

            //计算比例 暂定为3
            $fix = 3;

            //生成前景
            $image = new Imagick($bg_path);
            $draw = new ImagickDraw();
            $image->setImageFormat('png');
            $image->adaptiveResizeImage($bg_width*$fix,$bg_height*$fix);

            //生成文字
            if(!empty($one_data['front_fields'])){
                foreach($one_data['front_fields'] as $v){
                    if($v['z'] < 0) continue;
                    $draw->setFillColor($v['color']);
                    $draw->setFont(MIO_PATH.'Font/'.$v['font'].'.ttf');
                    $draw->setFontSize( $v['size']*$fix );
                    $image->annotateImage($draw, $v['x']*$fix, ($v['y']+$v['size'])*$fix, 0, $v['content']);
                }
            }
            //生成图片
            if(!empty($fg_imgs)){
                foreach($fg_imgs as $v){
                    $fg_obj = new Imagick($v['path']);
                    $fg_obj->setImageFormat('png');
                    $fg_obj->adaptiveResizeImage($v['data']['width']*$fix,$v['data']['height']*$fix);
                    $image->compositeImage( $fg_obj, $fg_obj->getImageCompose(), str_replace('px','',$v['data']['x'])*$fix, str_replace('px','',$v['data']['y'])*$fix);
                }
            }
            $image->writeImage("./temp/image_taili_{$page}_{$id}.png");
            $image->setImageFormat('pdf');
            $image->writeImage("./temp/image_taili_{$page}_{$id}.pdf" );