A-A+
[转]PHP合并图片函数横排竖排(略加修改/批注)
遇到点图片合并的需求,上网搜索了下,发现以下函数,貌似还不错;
先发出来和大家分享下,后面再详描述下它...实战次数不多,,
初步使用,感觉还不错;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /** * PHP合并图片函数横排竖排(略加修改/批注)[转] * * @param array $images 限定数组类型,以数组信息传递图片路径地址 * @param array $align 合并方向 V(竖) | H(横) * * @link http://hi.baidu.com/wshe505/item/922ded564f980313db16350a */ function implode_image(array $images, $align = 'V') { header('Content-type:image/jpeg'); if(count($images) <= 1) { return false; } $images_info = array(); foreach($images as $image) { if($info = getimagesize($image)) { $images_info[] = $info; } else { return false; } } unset($info); $the_max_width = 0; $the_max_height = 0; $total_height = 0; $total_width = 0; foreach($images_info as $info) { if($info[0] > $the_max_width) $the_max_width = $info[0]; if($info[1] > $the_max_height) $the_max_height = $info[1]; $total_width += $info[0]; $total_height += $info[1]; } if(strtoupper($align) == 'V') { if(function_exists('imagecreatetruecolor')) { $dst_im = imagecreatetruecolor($the_max_width, $total_height); } else { $dst_im = imagecreate($the_max_width, $total_height); } $startX = 0; $startY = 0; for($i = 0; $i < count($images); $i++) { $src_im = imagecreatefromjpeg($images[$i]); imagecopymerge($dst_im, $src_im, $startX , $startY , 0, 0, $images_info[$i][0] , $images_info[$i][1] , 100); $startY += $images_info[$i][1]; imagedestroy($src_im); } imagejpeg($dst_im); } elseif(strtoupper($align) == 'H') { if(function_exists('imagecreatetruecolor')) { $dst_im = imagecreatetruecolor($total_width, $the_max_height); } else { $dst_im = imagecreate($total_width, $the_max_height); } $startX = 0; $startY = 0; for($i = 0; $i < count($images); $i++) { $src_im = imagecreatefromjpeg($images[$i]); imagecopymerge($dst_im, $src_im, $startX , $startY , 0, 0, $images_info[$i][0] , $images_info[$i][1] , 100); $startX += $images_info[$i][0]; imagedestroy($src_im); } imagejpeg($dst_im); } } # 使用示例 $images = array( 'softicons/c_haoya.jpg', 'softicons/c_judun.jpg', 'softicons/c_kantuwang.jpg' ); implode_image($images, 'V'); |
上述代码执行结果(网页显示):
布施恩德可便相知重
微信扫一扫打赏
支付宝扫一扫打赏