Php imagick extended two methods for merging Images
Method 1: compositeimages
/*** Function: merged image * @ param string $ output_url save path * @ param string $ img_type save type * @ param integral $ line_num number of images displayed per line * @ param array $ logo_info each merged image (All dimensions are required to be uniform) * @ param array $ img_list absolute path of the image to be merged ** @ return void */public function generate ($ output_url, $ img_type, $ line_num, $ logo_info, $ img_list = array () {// calculates the number of rows in the image. $ 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; // create a canvas $ 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); // destroy the object $ canvas-> destroy ();}
Method 2: combineimages
/*** Function: merged image * @ param string $ output_url save path * @ param string $ img_type save type * @ param integral $ line_num number of images displayed per line * @ param array $ logo_info each merged image (All dimensions are required to be uniform) * @ param array $ img_list absolute path of the image to be merged ** @ return void */public function generate ($ output_url, $ img_type, $ line_num, $ logo_info, $ img_list = array () {// calculates the number of rows in the image. $ 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; // create a canvas $ 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); // destroy the object $ canvas-> clear (); $ canvas-> destroy ();}
I personally think method 2 is more efficient