Home > Backend Development > PHP Tutorial > php imagick extends two methods of compositing images_PHP tutorial

php imagick extends two methods of compositing images_PHP tutorial

WBOY
Release: 2016-07-13 10:11:59
Original
1316 people have browsed it

php imagick extends two methods of compositing images


Method 1: 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();
    }
Copy after login



Method 2: 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();
    }
Copy after login

Personally, I think method 2 is more efficient

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/925220.htmlTechArticlephp imagick Two methods to extend composite images Method 1: compositeimages /** * function: composite image* @param string $output_url Image saving path* @param string $img_type Image...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template