如何使用PHP CodeIgniter将多个图像合并为一个图像?

时间:2014-05-29 07:30:16

标签: php codeigniter image-processing

我想做的事情如下(最终结果1图片):

Final result

将8张图像转换为带有背景图像的单张图像。现在我知道CI支持GD,GD2,ImageMagick,NetPBM,但CI中用于图像处理的当前功能不能做到这样的事情。是否有更好的CI_Image_lib可用于执行此操作或扩展并执行我要查找的内容。

背景图片: background image

最后看起来像这样: Finally image

2 个答案:

答案 0 :(得分:1)

最终设法使用 CodeIgniter - 图像处理类 - 水印来完成。我使用水印作为我想要合并的8个图像,如下所示:

<?php

class Merge_image extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $config = array(
            'source_image' => 'assets/images/tv-share.jpg',//background image
            'new_image' => 'assets/public/images/tv-share-done.jpg',
            'wm_overlay_path' => 'assets/public/images/1401174641.png',//image number 1
            'wm_vrt_offset' => '180',
            'wm_hor_offset' => '50',
        );
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174641.png';//image number 2
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174646.png';//image number 3
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174641.png';//image number 4
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_vrt_offset'] = $config['wm_vrt_offset'] + 180;
        $config['wm_hor_offset'] = 50;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174646.png';//image number 5
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174641.png';//image number 6
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174646.png';//image number 7
        $data = $this->watermark($config);

        $config = $data;
        $config['wm_hor_offset'] = $config['wm_hor_offset'] + 180;
        $config['source_image'] = $data['new_image'];
        $config['wm_overlay_path'] = 'assets/public/images/1401174641.png';//image number 8
        $data = $this->watermark($config);
        $this->load->view('merge_image_view', $data);
    }

    private function watermark($config) {
        $this->load->library('image_lib');
        $config['image_library'] = 'GD2';
//        $config['source_image'] = 'assets/images/tv-share.jpg';
//        $config['new_image'] = 'assets/public/images/tv-share-done.jpg';
        $config['wm_type'] = 'overlay';
//        $config['wm_overlay_path'] = 'assets/public/images/1401174641.png';
        $config['wm_opacity'] = '100';
        $config['wm_vrt_alignment'] = 'top';
        $config['wm_hor_alignment'] = 'left';
//        $config['wm_vrt_offset'] = '180';
//        $config['wm_hor_offset'] = '50';
//        $config['dynamic_output'] = TRUE;

        $this->image_lib->initialize($config);

        if (!$this->image_lib->watermark()) {
            echo $this->image_lib->display_errors();
            die();
        }
        $this->image_lib->clear();
        return $config;
    }

}

当然我会把它作为一个循环而不是复制和过去,这只是为了解释发生了什么。

答案 1 :(得分:0)

ImageMagick可以通过以下方式执行此操作:

convert +append image1 image2 image3 image4 row1.jpg
convert +append image5 image6 image7 image8 row2.jpg
convert -append row1.jpg row2.jpg combined.jpg
composite -resize "1x1<" -gravity center combined.jpg background.jpg montage.jpg

或使用

一步完成
montage

但是你需要比我更聪明才能计算出参数!