php合并图片,_PHP教程

WBOY
풀어 주다: 2016-07-13 10:03:14
원래의
1195명이 탐색했습니다.

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>);
로그인 후 복사

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/969246.htmlTechArticlephp合并图片, 在项目中需要把几张图片合并在一起,如下图 分别由头、身、腿三张图片合并起来 代码如下: 1 function combineImage( $head_img...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿