后台框架为ThinkPHP。
需求分析,微信分享时需要设置图片尺寸为:400x400,但是系统抓取过来的图片大小为300x400。
一 丶遇到的问题
第一反应想到的是采用tp框架自带的Image类来进行实现
$image = new \Think\Image(); $image->open('./1.jpg'); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg $image->thumb(400, 400)->save('./thumb.jpg');
以上方法为tp框架自带的生成缩略图,如果图片尺寸较大,完全没问题,但是如果遇到博主一样的需求就会出现小问题
例: 一张尺寸为800x800的图片要生成400x400的缩略图,框架会等比例缩小原图进而生成缩略图。但是如果原图尺寸为200x200,要生成400x400的图片,缩略图依然会生成成功,但是新生成的图片上下左右会各留100px的黑底,显然,这并不能满足我们得需求。查看Image类发现并没有封装类似功能
二 丶解决方案
首先想到的即是GD库生成一张透明的背景图,然后因为目标图片尺寸大于原图尺寸,所以只需要将两张图片合并居中即可代码如下:
<?php /** * 图片合并 **/ //最多支持九张图片, // $pictureList = array( // 'img1.png', // 'img2.png', // 'img3.png', // 'img4.png', // 'img5.png', // 'img6.png', // 'img7.png', // 'img8.png', // 'img9.png' // ); $pictureList = array( 'http://s3.mogucdn.com/p2/170212/88391240_5e48891jd46hk2i5alali7lljjahh_640x960.jpg_468x468.jpg' ); $pictureList = array_slice($pictureList, 0, 9); // 只操作前9个图片 $bg_w = 400; // 背景图片宽度 $bg_h = 400; // 背景图片高度 $background = imagecreatetruecolor($bg_w,$bg_h); // 背景图片 $bgcolor = imagecolorallocate($background, 255, 255, 255); // 为真彩色画布创建白色背景,再设置为透明 imagefill($background, 0, 0, $bgcolor); imageColorTransparent($background, $bgcolor); $pic_count = count($pictureList); $lineArr = array(); // 需要换行的位置 $space_x = 3; $space_y = 3; $line_x = 0; switch($pic_count) { case 1: // 正中间 $start_x = 50; // 图片在背景中X的位置 $start_y = 0; // 图片在背景中Y的位置 $pic_w = 300; // 宽度 可自行设置为需要的宽度 $pic_h = 400; // 高度 可自行设置为需要的高度 break; case 2: // 中间位置并排 $start_x = 2; $start_y = intval($bg_h/4) + 3; $pic_w = intval($bg_w/2) - 5; $pic_h = intval($bg_h/2) - 5; $space_x = 5; break; case 3: $start_x = 40; $start_y = 5; $pic_w = intval($bg_w/2) - 5; $pic_h = intval($bg_h/2) - 5; $lineArr = array(2); $line_x = 4; break; case 4: $start_x = 4; $start_y = 5; $pic_w = intval($bg_w/2) - 5; $pic_h = intval($bg_h/2) - 5; $lineArr = array(3); $line_x = 4; break; case 5: $start_x = 30; $start_y = 30; $pic_w = intval($bg_w/3) - 5; $pic_h = intval($bg_h/3) - 5; $lineArr = array(3); $line_x = 5; break; case 6: $start_x = 5; $start_y = 30; $pic_w = intval($bg_w/3) - 5; $pic_h = intval($bg_h/3) - 5; $lineArr = array(4); $line_x = 5; break; case 7: $start_x = 53; $start_y = 5; $pic_w = intval($bg_w/3) - 5; $pic_h = intval($bg_h/3) - 5; $lineArr = array(2,5); $line_x = 5; break; case 8: $start_x = 30; $start_y = 5; $pic_w = intval($bg_w/3) - 5; $pic_h = intval($bg_h/3) - 5; $lineArr = array(3,6); $line_x = 5; break; case 9: $start_x = 5; $start_y = 5; $pic_w = intval($bg_w/3) - 5; $pic_h = intval($bg_h/3) - 5; $lineArr = array(4,7); $line_x = 5; break; } foreach( $pictureList as $k=>$pic_path ) { $kk = $k + 1; if ( in_array($kk, $lineArr) ) { $start_x = $line_x; $start_y = $start_y + $pic_h + $space_y; } $pathInfo = pathinfo($pic_path); switch( strtolower($pathInfo['extension']) ) { case 'jpg': case 'jpeg': $imagecreatefromjpeg = 'imagecreatefromjpeg'; break; case 'png': $imagecreatefromjpeg = 'imagecreatefrompng'; break; case 'gif': default: $imagecreatefromjpeg = 'imagecreatefromstring'; $pic_path = file_get_contents($pic_path); break; } $resource = $imagecreatefromjpeg($pic_path); imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource)); // 最后两个参数为原始图片宽度和高度,倒数两个参数为copy时的图片宽度和高度 $start_x = $start_x + $pic_w + $space_x; } header("Content-type: image/jpg"); imagejpeg($background); $meargPic = imagegif($background, "img.png"); //销毁资源 imagedestroy($meargPic); ?>
效果图:
其中留白部分为透明背景色,即在不改变原图的情况下让原图尺寸变为400x400实现需求。如果想合并多张图片只需要把目标图片加入数组中即可