记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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace app\mapi\common\image;
/**
 * 拼接多幅图片成为一张图片
 *
 * 参数说明:原图片为文件路径数组,目的图片如果留空,则不保存结果
 *
 * 例子:
 * <code>
 * $ci = new CombineImage(array("./uploads/1.jpg", "./uploads/2.png"), "./uploads/3.png");
 * $ci->combine();
 * $ci->show();
 * </code>
 *
 * @author yangjianhui
 * @version 2020/6/13
 */
class CombineImage
{
    /**
     * 原图地址数组
     */
    private $srcImages;
    /**
     * 每张图片缩放到这个宽度
     */
    private $width;
    /**
     * 每张图片缩放到这个高度
     */
    private $height;
    /**
     * 目标图片地址
     */
    private $destImage;
 
    /**
     * 临时画布
     */
    private $canvas;
 
    /**
     * CombineImage constructor.
     *
     * @param array $srcImages 需要图片路径数组
     * @param string $desImage 输出目标图片地址
     * @param int $width 输出后图片宽度
     * @param int $height 输出后图片高度
     */
    public function __construct(array $srcImages$desImage ''$width = 750, $height = 12144)
    {
        $this->srcImages = $srcImages;
        $this->destImage = $desImage;
        $this->width     = $width;
        $this->height    = $height;
        $this->canvas    = NULL;
    }
 
    public function __destruct()
    {
        if ($this->canvas != NULL) {
            imagedestroy($this->canvas);
        }
    }
 
    /**
     * 合并图片
     */
    public function combine()
    {
        if (empty($this->srcImages) || $this->width == 0 || $this->height == 0) {
            return;
        }
 
       /*压缩图片 计算压缩后的图片高度*/
       /*获取所有图片高度*/
        $heightAll = 0;
        for ($i = 0; $i count($this->srcImages); $i++) {
            $srcImage $this->srcImages[$i];
            list($srcWidth$srcHeight$fileType) = getimagesize($srcImage);
             
            if ($fileType == 2) {
                $srcImage = imagecreatefromjpeg($srcImage);
            else if ($fileType == 3) {
                $srcImage = imagecreatefrompng($srcImage);
            else {
                continue;
            }
             
            $new_width $srcWidth;//压缩后的图片宽
            $new_height $srcHeight;//压缩后的图片高
      
            if($srcWidth >= 750){
                $per = 750 / $srcWidth;//计算比例
                $new_width $srcWidth $per;
                $new_height $srcHeight $per;
            }
             
             
            // $heightAll+=$srcHeight;
            $heightAll+=$new_height;
        }     
         
        /*设置画布总高度 并固定宽度  开始压缩合成图片*/
        $this->height = $heightAll;
        $this->width = 750;
        $this->createCanvas();
     
        for ($i = 0; $i count($this->srcImages); $i++) {
            $srcImage $this->srcImages[$i];
            //获取原图的基本信息(切记不要https)
            list($srcWidth$srcHeight$fileType) = getimagesize($srcImage);
             
            $new_width $srcWidth;//压缩后的图片宽
            $new_height $srcHeight;//压缩后的图片高
      
            if($srcWidth >= 750){
                $per = 750 / $srcWidth;//计算比例
                $new_width $srcWidth $per;
                $new_height $srcHeight $per;
            }        
             
             
            if ($fileType == 2) {
                // 原图是 jpg 类型
                $srcImage = imagecreatefromjpeg($srcImage);
            else if ($fileType == 3) {
                // 原图是 png 类型
                $srcImage = imagecreatefrompng($srcImage);
            else {
                // 无法识别的类型
                continue;
            }
 
            // 计算当前原图片应该位于画布的哪个位置
            $destX     = 0;
             
            if ($i == 0) {
                $desyY     = 0;
                $desyY_pre  $new_height;
            else {
                $desyY += $desyY_pre;
                $desyY_pre $new_height;
            }
            // $desyY += $srcHeight;
            // echo $i."--".$srcHeight."--".$desyY."\r\n";
            imagecopyresampled($this->canvas, $srcImage$destX$desyY,
                0, 0, $new_width$new_height$srcWidth$srcHeight);
            // echo $desyY.'--';
        }
         
        // die;
        // 如果有指定目标地址,则输出到文件
        if (!empty($this->destImage)) {
            $this->output();
        }
    }
 
    /**
     * 输出结果到浏览器
     */
    public function show()
    {
        if ($this->canvas == NULL) {
            return;
        }
        header("Content-type: image/jpeg");
        imagejpeg($this->canvas);
    }
 
    /**
     * 私有函数,创建画布
     */
    private function createCanvas()
    {
        ini_set('memory_limit''256M');
        $this->canvas = imagecreatetruecolor($this->width, $this->height);
        // 使画布透明
        $white = imagecolorallocate($this->canvas, 255, 255, 255);
        imagefill($this->canvas, 0, 0, $white);
        imagecolortransparent($this->canvas, $white);
    }
 
    /**
     * 私有函数,保存结果到文件
     */
    private function output()
    {
        // 获取目标文件的后缀
        $fileType substr(strrchr($this->destImage, '.'), 1);
        if ($fileType == 'jpg' || $fileType == 'jpeg') {
            imagejpeg($this->canvas, $this->destImage);
        else {
            // 默认输出 png 图片
            imagepng($this->canvas, $this->destImage);
        }
 
    }
    /*图片压缩*/
    public function compressedImage($imgsrc$imgdst) {
        list($width$height$type) = getimagesize($imgsrc);
        
        $new_width $width;//压缩后的图片宽
        $new_height $height;//压缩后的图片高
  
        if($width >= 750){
            $per = 750 / $width;//计算比例
            $new_width $width $per;
            $new_height $height $per;
        }
  
        switch ($type) {
            case 1:
                $giftype = check_gifcartoon($imgsrc);
                if ($giftype) {
                    header('Content-Type:image/gif');
                    $image_wp = imagecreatetruecolor($new_width$new_height);
                    $image = imagecreatefromgif($imgsrc);
                    imagecopyresampled($image_wp$image, 0, 0, 0, 0, $new_width$new_height$width$height);
                    //90代表的是质量、压缩图片容量大小
                    imagejpeg($image_wp$imgdst, 90);
                    imagedestroy($image_wp);
                    imagedestroy($image);
                }
                break;
            case 2:
                header('Content-Type:image/jpeg');
                $image_wp = imagecreatetruecolor($new_width$new_height);
                $image = imagecreatefromjpeg($imgsrc);
                imagecopyresampled($image_wp$image, 0, 0, 0, 0, $new_width$new_height$width$height);
                //90代表的是质量、压缩图片容量大小
                imagejpeg($image_wp$imgdst, 90);
                imagedestroy($image_wp);
                imagedestroy($image);
                break;
            case 3:
                header('Content-Type:image/png');
                $image_wp = imagecreatetruecolor($new_width$new_height);
                $image = imagecreatefrompng($imgsrc);
                imagecopyresampled($image_wp$image, 0, 0, 0, 0, $new_width$new_height$width$height);
                //90代表的是质量、压缩图片容量大小
                imagejpeg($image_wp$imgdst, 90);
                imagedestroy($image_wp);
                imagedestroy($image);
                break;
        }
    }    
     
}
 
?>

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!