首頁 >php教程 >php手册 >php合并图片,

php合并图片,

WBOY
WBOY原創
2016-06-13 09:11:491702瀏覽

php合并图片,

在项目中需要把几张图片合并在一起,如下图

分别由头、身、腿三张图片合并起来

代码如下:

<span> 1</span> <span>function</span> combineImage(<span>$head_img</span>,<span>$middle_img</span>,<span>$footer_img</span>,<span>$save_path</span><span>){
</span><span> 2</span>     <span>$source_w</span> = 400<span>;
</span><span> 3</span>     <span>$source_h</span> = 1142<span>;
</span><span> 4</span>     <span>//</span><span>取头部图片大小</span>
<span> 5</span>     <span>$head_size</span> = <span>getimagesize</span>(<span>$head_img</span><span>);
</span><span> 6</span>     <span>$head_height</span> = <span>$head_size</span>['1'<span>];
</span><span> 7</span>     <span>$head_width</span> = <span>$head_size</span>['0'<span>];
</span><span> 8</span>     <span>$head_start_x</span> = <span>floor</span>((<span>$source_w</span>-<span>$head_width</span>)/2);<span>//</span><span>头部开始位置
</span><span> 9</span> <span>    //取中间图片大小</span>
<span>10</span>     <span>$midd_size</span> = <span>getimagesize</span>(<span>$middle_img</span><span>);
</span><span>11</span>     <span>$midd_height</span> = <span>$midd_size</span>['1'<span>];
</span><span>12</span>     <span>$midd_width</span> = <span>$midd_size</span>['0'<span>];
</span><span>13</span>     <span>$midd_start_y</span> = <span>$head_height</span>-15;<span>//</span><span>中间开始Y坐标,因为头部的图片底部有空白,所以减去15</span>
<span>14</span>     <span>$midd_start_x</span> = <span>floor</span>((<span>$source_w</span>-<span>$midd_width</span>)/2<span>);
</span><span>15</span>     
<span>16</span>     <span>//</span><span>取底部图片大小</span>
<span>17</span>     <span>$foot_size</span> = <span>getimagesize</span>(<span>$footer_img</span><span>);
</span><span>18</span>     <span>$foot_height</span> = <span>$foot_size</span>[1<span>];
</span><span>19</span>     <span>$foot_width</span> = <span>$foot_size</span>[0<span>];
</span><span>20</span>     <span>$foot_start_x</span> = <span>floor</span>((<span>$source_w</span>-<span>$foot_width</span>)/2);<span>//</span><span>底部图片x坐标</span>
<span>21</span>     <span>$foot_start_y</span> = <span>$source_h</span>-<span>$foot_height</span>;<span>//</span><span>底部图片y坐标</span>
<span>22</span>     
<span>23</span>     <span>$head</span> = imagecreatefrompng(<span>$head_img</span><span>);
</span><span>24</span>     <span>$middle</span> = imagecreatefrompng(<span>$middle_img</span><span>);
</span><span>25</span>     <span>$footer</span> = imagecreatefrompng(<span>$footer_img</span><span>);
</span><span>26</span>     
<span>27</span>     <span>$bg_img</span> = imageCreatetruecolor(<span>$source_w</span>,<span>$source_h</span>);<span>//</span><span>生成背景图片</span>
<span>28</span>     <span>$color</span> = imagecolorallocate(<span>$bg_img</span>, 255, 255, 255); <span>//</span><span>设置白色背景</span>
<span>29</span>     imagefill(<span>$bg_img</span>, 0, 0, <span>$color</span>);<span>//</span><span>背景色填充</span>
<span>30</span>     imageColorTransparent(<span>$bg_img</span>, <span>$color</span>);<span>//</span><span>透明</span>
<span>31</span>     imagecopyresampled(<span>$bg_img</span>,<span>$head</span>,<span>$head_start_x</span>,0,0,0,<span>$head_width</span>,<span>$head_height</span>,<span>$head_width</span>,<span>$head_height</span><span>);
</span><span>32</span>     imagecopyresampled(<span>$bg_img</span>,<span>$middle</span>,<span>$midd_start_x</span> , <span>$midd_start_y</span>,0,0,<span>$midd_width</span>,<span>$midd_height</span>,<span>$midd_width</span>,<span>$midd_height</span><span>);
</span><span>33</span>     imagecopyresampled(<span>$bg_img</span>,<span>$footer</span>,<span>$foot_start_x</span> , <span>$foot_start_y</span>,0,0,<span>$foot_width</span>,<span>$foot_height</span>,<span>$foot_width</span>,<span>$foot_height</span><span>);
</span><span>34</span>     
<span>35</span>     imagepng(<span>$bg_img</span>,<span>$save_path</span><span> );
</span><span>36</span> <span>}
</span><span>37</span> <span>$head</span> = <span>dirname</span>(<span>__FILE__</span>).'/public/images/head.png'<span>;
</span><span>38</span> <span>$midd</span> = <span>dirname</span>(<span>__FILE__</span>).'/public/images/midd1.png'<span>;
</span><span>39</span> <span>$foot</span> = <span>dirname</span>(<span>__FILE__</span>).'/public/images/foot1.png'<span>;
</span><span>40</span> <span>$save_path</span> = <span>dirname</span>(<span>__FILE__</span>)."/public/images/testcomblie.png"<span>;
</span><span>41</span> combineImage(<span>$head</span>,<span>$midd</span>,<span>$foot</span>,<span>$save_path</span>);

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn