PHP combines several photos to produce a synthetic image [easy to print and output as a whole ],

Source: Internet
Author: User

PHP combines several photos to produce a synthetic image [easy to print and output as a whole ],

This example describes how PHP can combine several photos to merge images. We will share this with you for your reference. The details are as follows:

<? Php/*** work merging Program * for single-sided, no special processing for cover */$ src_path = $ argv [1]; // php file, the first parameter is the folder name (portfolio), which can be relative to the path $ dst_path = '.. /image /'. $ src_path; // the target location where the generated file is stored if (! File_exists ($ dst_path) {mkdir ($ dst_path);} // recommended size of the synthetic graph. For a single page, the recommended size is 1120*1600 $ g_width = 1120; $ g_height = 1600; $ g_border = 20; // border // template // number of images => array (position => array (x, y, width, height )) $ g_models = array (1 => array (// total number of sheets on a single page 0 => array (// location 'X' => 0 + $ g_border, 'y' => 0 + $ g_border, 'w' => $ g_width-2 * $ g_border, 'H' => $ g_height-2 * $ g_border ,),), 3 => array (0 => array ('x' => 0 + $ g_border, 'y' => 0 + $ g_border, 'W' => $ g_width-2 * $ g_border, 'H' => ($ g_height-3 * $ g_border)/2 ,), 1 => array ('x' => 0 + $ g_border, 'y' => 0 + $ g_border + ($ g_height-3 * $ g_border)/2 + $ g_border, 'W' => ($ g_width-3 * $ g_border)/2, 'H' => ($ g_height-3 * $ g_border)/2 ,), 2 => array ('x' => 0 + $ g_border + ($ g_width-3 * $ g_border)/2 + $ g_border, 'y' => 0 + $ g_border + ($ g_height-3 * $ g_border)/2 + $ g_border, 'w' => ($ g_width-3 * $ G_border)/2, 'H' => ($ g_height-3 * $ g_border)/2 ,),), 4 => array (0 => array ('x' => 0 + $ g_border, 'y' => 0 + $ g_border, 'W' => ($ g_width-3 * $ g_border)/2, 'H' => ($ g_height-3 * $ g_border)/2 ,), 1 => array ('x' => 0 + $ g_border + ($ g_width-3 * $ g_border)/2 + $ g_border, 'y' => 0 + $ g_border, 'W' => ($ g_width-3 * $ g_border)/2, 'H' => ($ g_height-3 * $ g_border)/2 ,), 2 => array ('x' => 0 + $ g_border, 'y' => 0 + $ g_ B Order + ($ g_height-3 * $ g_border)/2 + $ g_border, 'w' => ($ g_width-3 * $ g_border)/2, 'H' => ($ g_height-3 * $ g_border)/2,), 3 => array ('x' => 0 + $ g_border + ($ g_width-3 * $ g_border) /2 + $ g_border, 'y' => 0 + $ g_border + ($ g_height-3 * $ g_border)/2 + $ g_border, 'W' => ($ g_width-3 * $ g_border)/2, 'H' => ($ g_height-3 * $ g_border)/2 ,),),); // Print $ g_tasks = array (0 => array (0), // cover bottom 1 => array (1), 2 => array (2 ), 3 => array (3), 4 => array (4, 5, 6), 5 => array (7), 6 => array (8 ), 7 => array (, 11), 8 => array (12), 9 => array (13), 10 => array (, 16 ), 11 => array (17), 12 => array (18), 13 => array (19,20, 21), 14 => array (22 ), 15 => array (23), 16 => array (24, 25, 26), 17 => array (27,28, 29), 18 => array (30 ), 19 => array (31), 20 => array (32, 33, 34), 21 => array (35), 22 => array (36 ), 23 => array (37), 24 => array (38, 39,40, 41), 25 => array (, 44), 26 => array (45), 27 => array (46), 28 => array, 49), 29 => array (50), 30 => array (51),); // get all the image names in the folder $ jpgs = array (); $ files = scandir ($ src_path); // all file names under the directory foreach ($ files as $ file) {$ path_parts = pathinfo ($ src_path. '/'. $ file); if ($ path_parts ['extension'] = 'jpg ') {$ jpgs [] = $ src_path. '/'. $ file ;}// determines the total number of images if (count ($ jpgs )! = 52) {echo 'the total number of images is incorrect :'. count ($ jpgs ). '/52 '. nl2br ("\ n"); die () ;}// natural sorting usort ($ jpgs, "strnatcmp"); foreach ($ g_tasks as $ page => $ photos) {$ files = array (); foreach ($ photos as $ r) {$ files [] = $ jpgs [$ r] ;}$ image_all = imagemake ($ files ); $ filename = paipage.'.jpg '; imagejpeg ($ image_all, $ dst_path. '/'. $ filename); unset ($ files); echo $ filename. nl2br ("\ n");} echo 'OK '. nl2br ("\ n"); die (); /*** synthesize image ** @ param array $ images image set on this page * @ return resource merged image */function imagemake ($ files = array () {global $ g_width, $ g_height, $ g_models; // merged image $ image_all = imageCreatetruecolor ($ g_width, $ g_height); // create a white background for a true color canvas $ color = imagecolorallocate ($ image_all, 255,255,255); imagefill ($ image_all, 0, 0, $ color); // imageColorTransparent ($ image_all, $ color); // background transparency // function imagecopyresampled ($ dst_image, $ src_image, $ dst_x, $ dst_y, $ src_x, $ src_y, $ dst_w, $ dst_h, $ src_w, $ src_h) // typographical synthesis $ type = count ($ files ); switch ($ type) {case 2: break; case 1: case 3: case 4: // The image set for merging $ images = array (); // modify the image for ($ I = 0; $ I <$ type; $ I ++) {$ images [] = imagecropper ($ files [$ I], $ g_models [$ type] [$ I] ['W'], $ g_models [$ type] [$ I] ['H']);} // typographical Synthesis for ($ I = 0; $ I <$ type; $ I ++) {imagecopyresampled ($ image_all, $ images [$ I], $ g_models [$ type] [$ I] ['X'], $ g_models [$ type] [$ I] ['y'], 0, 0, $ g_models [$ type] [$ I] ['W'], $ g_models [$ type] [$ I] ['H'], imagesx ($ images [$ I]), imagesy ($ images [$ I]);} break; default: break;} return $ image_all ;} /*** trim the image: center crop proportional scaling * @ param $ source_path source image path * @ param $ target_width target width * @ param $ target_height target height * @ return bool | resource */function imagecropper ($ source_path, $ target_width, $ target_height) {$ source_info = getimagesize ($ source_path); $ source_width = $ source_info [0]; $ source_height = $ source_info [1]; $ source_mime = $ source_info ['mime ']; $ source_ratio = $ source_height/$ source_width; $ target_ratio = $ target_height/$ target_width; switch ($ source_mime) {case 'image/gif': $ source_image = imagecreatefromgif ($ source_path); break; case 'image/jpeg ': $ source_image = imagecreatefromjpeg ($ source_path); break; case 'image/png ': $ source_image = imagecreatefrompng ($ source_path); break; default: return false; break;} // different landscape charts, rotate if ($ target_width-$ target_height) * ($ source_width-$ source_height) <0) {// rotate $ source_image = imagerotate ($ source_image, 90, 0 ); $ source_width = $ source_info [1]; // [0] $ source_height = $ source_info [0]; // [1] $ source_ratio = $ source_height/$ source_width ;} // if ($ source_ratio> $ target_ratio) {$ cropped_width = $ source_width; $ cropped_height = $ source_width * $ target_ratio; $ source_x = 0; $ source_y = ($ source_height-$ cropped_height)/2;} // elseif ($ source_ratio <$ target_ratio) {$ cropped_width = $ source_height/$ target_ratio; $ cropped_height = $ source_height; $ source_x = ($ source_width-$ cropped_width)/2; $ source_y = 0;} // else {$ cropped_width = $ source_width; $ cropped_height = $ source_height; $ source_x = 0; $ source_y = 0;} $ target_image = imagecreatetruecolor ($ target_width, $ target_height); $ cropped_image = crop ($ cropped_width, $ cropped_height); // crop imagecopy ($ cropped_image, $ source_image, 0, 0, $ source_x, $ source_y, $ cropped_width, $ cropped_height); // scale ($ target_image, $ cropped_image, 0, 0, 0, 0, $ target_width, $ target_height, $ cropped_width, $ cropped_height); return $ target_image ;}

PS: This Code is applied to the command line mode. Pay attention to the image folder path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.