Home  >  Article  >  Backend Development  >  php imagick 扩张合成图片的两张方法

php imagick 扩张合成图片的两张方法

WBOY
WBOYOriginal
2016-06-13 12:10:551337browse

php imagick 扩展合成图片的两张方法


方法一:compositeimages

/**     *  function: 合成图片     * @param  string      $output_url 图片保存路径     * @param  string      $img_type   图片保存类型     * @param  integral    $line_num   每行显示图片数量     * @param  array       $logo_info  每张待合成图片的信息(要求所有尺寸统一)     * @param  array       $img_list  待合成的图片绝对路径     *      * @return void     */    public function generate($output_url, $img_type, $line_num, $logo_info, $img_list=array()) {        //计算图片有多少行        $lines = ceil(count($img_list)/$line_num);        $bg_width = ($logo_info['width'] + $logo_info['line_width']) * $line_num;        $bg_height = ($logo_info['height'] + $logo_info['line_height']) * $lines;        //构建画布        $canvas = new Imagick();        $canvas->newimage($bg_width, $bg_height, 'white');        $canvas->setimageformat($img_type);        $i = $j = 0;        foreach ($img_list as $item) {            $im = new Imagick($item);            $x = $logo_info['line_width']*2 + $i * $logo_info['width'];            $y = $logo_info['line_height']*2 + $j * $logo_info['height'];                        // $canvas->compositeimage($im -> getimage(), Imagick::COMPOSITE_OVER, $x, $y);            $canvas -> compositeimage($im, $im->getImageCompose(), $x, $y);            if (($i + 1) % $line_num === 0) {                $i = 0;                $j++;            } else {                $i++;            }            // unset($im);            $im -> destroy();        }        $canvas->writeimage($output_url);        //销毁对象        $canvas -> destroy();    }



方法二:combineimages

/**     *  function: 合成图片     * @param  string      $output_url 图片保存路径     * @param  string      $img_type   图片保存类型     * @param  integral    $line_num   每行显示图片数量     * @param  array       $logo_info  每张待合成图片的信息(要求所有尺寸统一)     * @param  array       $img_list  待合成的图片绝对路径     *      * @return void     */    public function generate($output_url, $img_type, $line_num, $logo_info, $img_list=array()) {        //计算图片有多少行        $lines = ceil(count($img_list)/$line_num);        $bg_width = ($logo_info['width'] + $logo_info['line_width']) * $line_num;        $bg_height = ($logo_info['height'] + $logo_info['line_height']) * $lines;        //构建画布        $canvas = null;        $canvas = new Imagick();        $canvas -> newimage($bg_width, $bg_height, 'white');        $i = $j = 0;        foreach ($img_list as $item) {            $im = null;            $im = new Imagick($item);            // $canvas -> readImage($item);            $canvas -> addimage($im);            $im -> clear();            $im -> destroy();        }        // $canvas -> flattenImages();        $canvas -> combineImages( Imagick::CHANNEL_ALL );         $canvas -> writeimage($output_url);        //销毁对象        $canvas -> clear();        $canvas -> destroy();    }

个人认为方法二效率更高
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn