클래스
<code><span><span><span><span><span><span><span><span><span><span><span><span><span><?php namespace App\Libs; class Iconv { function phpmake_ico<span>()</span> { return true; } function GDtoICOstr<span>(&<span>$gd_ico_array</span>)</span> { foreach <span>(<span>$gd_ico_array</span> as <span>$key</span> => <span>$gd_image</span>)</span> { <span>$IcoWidths</span>[<span>$key</span>] = ImageSX<span>(<span>$gd_image</span>)</span>; <span>$IcoHeights</span>[<span>$key</span>] = ImageSY<span>(<span>$gd_image</span>)</span>; <span>$bpp</span>[<span>$key</span>] = ImageIsTrueColor<span>(<span>$gd_image</span>)</span> ? <span>32</span> : <span>24</span>; <span>$totalcolors</span>[<span>$key</span>] = ImageColorsTotal<span>(<span>$gd_image</span>)</span>; <span>$icXOR</span>[<span>$key</span>] = <span>''</span>; for <span>(<span>$y</span> = <span>$IcoHeights</span>[<span>$key</span>] - <span>1</span>; <span>$y</span> >= <span>0</span>; <span>$y</span>--)</span> { for <span>(<span>$x</span> = <span>0</span>; <span>$x</span> $IcoWidths</span>[<span>$key</span>]; <span>$x</span>++)</span> { <span>$argb</span> = <span>$this</span>->gpc<span>(<span>$gd_image</span>, <span>$x</span>, <span>$y</span>)</span>; <span>$a</span> = round<span>(<span>255</span> * <span>(<span>(<span>127</span> - <span>$argb</span>[<span>'alpha'</span>])</span> / <span>127</span>)</span>)</span>; <span>$r</span> = <span>$argb</span>[<span>'red'</span>]; <span>$g</span> = <span>$argb</span>[<span>'green'</span>]; <span>$b</span> = <span>$argb</span>[<span>'blue'</span>]; if <span>(<span>$bpp</span>[<span>$key</span>] == <span>32</span>)</span> { <span>$icXOR</span>[<span>$key</span>] .= chr<span>(<span>$b</span>)</span>.chr<span>(<span>$g</span>)</span>.chr<span>(<span>$r</span>)</span>.chr<span>(<span>$a</span>)</span>; } elseif <span>(<span>$bpp</span>[<span>$key</span>] == <span>24</span>)</span> { <span>$icXOR</span>[<span>$key</span>] .= chr<span>(<span>$b</span>)</span>.chr<span>(<span>$g</span>)</span>.chr<span>(<span>$r</span>)</span>; } if <span>(<span>$a</span> 128</span>)</span> { @<span>$icANDmask</span>[<span>$key</span>][<span>$y</span>] .= <span>'1'</span>; } else { @<span>$icANDmask</span>[<span>$key</span>][<span>$y</span>] .= <span>'0'</span>; } } while <span>(strlen<span>(<span>$icANDmask</span>[<span>$key</span>][<span>$y</span>])</span> % <span>32</span>)</span> { <span>$icANDmask</span>[<span>$key</span>][<span>$y</span>] .= <span>'0'</span>; } } <span>$icAND</span>[<span>$key</span>] = <span>''</span>; foreach <span>(<span>$icANDmask</span>[<span>$key</span>] as <span>$y</span> => <span>$scanlinemaskbits</span>)</span> { for <span>(<span>$i</span> = <span>0</span>; <span>$i</span> (<span>$scanlinemaskbits</span>)</span>; <span>$i</span> += <span>8</span>)</span> { <span>$icAND</span>[<span>$key</span>] .= chr<span>(bindec<span>(str_pad<span>(substr<span>(<span>$scanlinemaskbits</span>, <span>$i</span>, <span>8</span>)</span>, <span>8</span>, <span>'0'</span>, STR_PAD_LEFT)</span>)</span>)</span>; } } } foreach <span>(<span>$gd_ico_array</span> as <span>$key</span> => <span>$gd_image</span>)</span> { <span>$biSizeImage</span> = <span>$IcoWidths</span>[<span>$key</span>] * <span>$IcoHeights</span>[<span>$key</span>] * <span>(<span>$bpp</span>[<span>$key</span>] / <span>8</span>)</span>; <span>$bfh</span>[<span>$key</span>] = <span>''</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x28\x00\x00\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>$this</span>->le2s<span>(<span>$IcoWidths</span>[<span>$key</span>], <span>4</span>)</span>; <span>$bfh</span>[<span>$key</span>] .= <span>$this</span>->le2s<span>(<span>$IcoHeights</span>[<span>$key</span>] * <span>2</span>, <span>4</span>)</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x01\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= chr<span>(<span>$bpp</span>[<span>$key</span>])</span>.<span>"\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x00\x00\x00\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>$this</span>->le2s<span>(<span>$biSizeImage</span>, <span>4</span>)</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x00\x00\x00\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x00\x00\x00\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x00\x00\x00\x00"</span>; <span>$bfh</span>[<span>$key</span>] .= <span>"\x00\x00\x00\x00"</span>; } <span>$icondata</span> = <span>"\x00\x00"</span>; <span>$icondata</span> .= <span>"\x01\x00"</span>; <span>$icondata</span> .= <span>$this</span>->le2s<span>(count<span>(<span>$gd_ico_array</span>)</span>, <span>2</span>)</span>; <span>$dwImageOffset</span> = <span>6</span> + <span>(count<span>(<span>$gd_ico_array</span>)</span> * <span>16</span>)</span>; foreach <span>(<span>$gd_ico_array</span> as <span>$key</span> => <span>$gd_image</span>)</span> { <span>$icondata</span> .= chr<span>(<span>$IcoWidths</span>[<span>$key</span>])</span>; <span>$icondata</span> .= chr<span>(<span>$IcoHeights</span>[<span>$key</span>])</span>; <span>$icondata</span> .= chr<span>(<span>$totalcolors</span>[<span>$key</span>])</span>; <span>$icondata</span> .= <span>"\x00"</span>; <span>$icondata</span> .= <span>"\x01\x00"</span>; <span>$icondata</span> .= chr<span>(<span>$bpp</span>[<span>$key</span>])</span>.<span>"\x00"</span>; <span>$dwBytesInRes</span> = <span>40</span> + strlen<span>(<span>$icXOR</span>[<span>$key</span>])</span> + strlen<span>(<span>$icAND</span>[<span>$key</span>])</span>; <span>$icondata</span> .= <span>$this</span>->le2s<span>(<span>$dwBytesInRes</span>, <span>4</span>)</span>; <span>$icondata</span> .= <span>$this</span>->le2s<span>(<span>$dwImageOffset</span>, <span>4</span>)</span>; <span>$dwImageOffset</span> += strlen<span>(<span>$bfh</span>[<span>$key</span>])</span>; <span>$dwImageOffset</span> += strlen<span>(<span>$icXOR</span>[<span>$key</span>])</span>; <span>$dwImageOffset</span> += strlen<span>(<span>$icAND</span>[<span>$key</span>])</span>; } foreach <span>(<span>$gd_ico_array</span> as <span>$key</span> => <span>$gd_image</span>)</span> { <span>$icondata</span> .= <span>$bfh</span>[<span>$key</span>]; <span>$icondata</span> .= <span>$icXOR</span>[<span>$key</span>]; <span>$icondata</span> .= <span>$icAND</span>[<span>$key</span>]; } return <span>$icondata</span>; } function le2s<span>(<span>$number</span>, <span>$minbytes</span>=<span>1</span>)</span> { <span>$intstring</span> = <span>''</span>; while <span>(<span>$number</span> > <span>0</span>)</span> { <span>$intstring</span> = <span>$intstring</span>.chr<span>(<span>$number</span> & <span>255</span>)</span>; <span>$number</span> >>= <span>8</span>; } return str_pad<span>(<span>$intstring</span>, <span>$minbytes</span>, <span>"\x00"</span>, STR_PAD_RIGHT)</span>; } function gpc<span>(&<span>$img</span>, <span>$x</span>, <span>$y</span>)</span> { if <span>(!is_resource<span>(<span>$img</span>)</span>)</span> { return false; } return @ImageColorsForIndex<span>(<span>$img</span>, @ImageColorAt<span>(<span>$img</span>, <span>$x</span>, <span>$y</span>)</span>)</span>; } } ?></span></span></span></span></span></span></span></span></span></code>
컨트롤러
<code><span>if</span> ( <span>$error</span>[<span>'text'</span>] == <span>""</span> && isset(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>]) && <span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>] && is_uploaded_file(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>])) { <span>if</span> (<span>$_FILES</span>[<span>'upimage'</span>][<span>'type'</span>] > <span>210000</span>) { <span>$error</span>[<span>'text'</span>] = <span>"你上传的文件体积超过了限制 最大不能超过200k"</span>; } <span>else</span> { <span>$fileext</span> = array(<span>"image/pjpeg"</span>, <span>"image/gif"</span>, <span>"image/x-png"</span>, <span>"image/png"</span>, <span>"image/jpeg"</span>, <span>"image/jpg"</span>); <span>if</span> (!in_array(<span>$_FILES</span>[<span>'upimage'</span>][<span>'type'</span>], <span>$fileext</span>)) { <span>$error</span>[<span>'text'</span>] = <span>"你上传的文件格式不正确 仅支持 jpg,gif,png"</span>; }<span>else</span> { <span>if</span> (<span>$im</span> = <span>@imagecreatefrompng</span>(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>]) <span>or</span><span>$im</span> = <span>@imagecreatefromgif</span>(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>]) <span>or</span><span>$im</span> = <span>@imagecreatefromjpeg</span>(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>])) { <span>$imginfo</span> = <span>@getimagesize</span>(<span>$_FILES</span>[<span>'upimage'</span>][<span>'tmp_name'</span>]); <span>if</span> (!is_array(<span>$imginfo</span>)) { <span>$error</span>[<span>'text'</span>] = <span>"图形格式错误!"</span>; }<span>else</span> { switch (<span>$_POST</span>[<span>'size'</span>]) { <span>case</span><span>1</span>; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>16</span>, <span>16</span>); <span>$size</span> = <span>16</span>; <span>break</span>; <span>case</span><span>2</span>; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>32</span>, <span>32</span>); <span>$size</span> = <span>32</span>; <span>break</span>; <span>case</span><span>3</span>; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>48</span>, <span>48</span>); <span>$size</span> = <span>48</span>; <span>break</span>; <span>case</span><span>4</span>; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>64</span>, <span>64</span>); <span>$size</span> = <span>64</span>; <span>break</span>; <span>case</span><span>5</span>; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>128</span>, <span>128</span>); <span>$size</span> = <span>128</span>; <span>break</span>; default; <span>$resize_im</span> = <span>@imagecreatetruecolor</span>(<span>64</span>, <span>64</span>); <span>$size</span> = <span>64</span>; <span>break</span>; } imagecopyresampled(<span>$resize_im</span>, <span>$im</span>, <span>0</span>, <span>0</span>, <span>0</span>, <span>0</span>, <span>$size</span>, <span>$size</span>, <span>$imginfo</span>[<span>0</span>], <span>$imginfo</span>[<span>1</span>]); <span>$icon</span> = new <span>Iconv</span>(); <span>$gd_image_array</span> = array(<span>$resize_im</span>); <span>$icon_data</span> = <span>$icon</span>-><span>GDtoICOstr</span>(<span>$gd_image_array</span>); <span>$filename</span> = <span>"temp/"</span> . date(<span>"Ymdhis"</span>) . rand(<span>1</span>, <span>1000</span>) . <span>".ico"</span>; <span>if</span> (file_put_contents(<span>$filename</span>, <span>$icon_data</span>)) { <span>//</span><span>$output</span> = <span>"生成成功!请点右键->另存为 保存到本地<br><a href="</span>/<span>" mce_href="</span>/<span>"><span>".$filename."</span>/<span>" target=/"</span>_blank/<span>">点击下载</span></a>"</span>; <span>//</span> echo <span>$filename</span>; <span>//</span>数据展示 <span>$icon_arr</span>=[ <span>'class'</span>=><span>''</span>, <span>'time'</span>=>date(<span>"Y-m-d H:i:s"</span>), <span>'filename'</span>=><span>$_FILES</span>[<span>'upimage'</span>][<span>'name'</span>], <span>'filepath'</span>=><span>$filename</span>, <span>'size'</span>=><span>$size</span> ]; } } } <span>else</span> { <span>$error</span>[<span>'text'</span>] = <span>"生成错误请重试"</span>; } } } }<span>else</span>{ <span>$error</span>[<span>'text'</span>] = <span>"请选择图片!"</span>; } </code>
표시 효과
소스 코드 주소
ico 온라인 변환 도구가 오픈소스 프로젝트 https://github.com/diandianxiyu/ApiTesting에 통합되었으며, 온라인 데모 주소는 추후 공개됩니다
2016년 새해 복 많이 받으세요!
').addClass('사전 번호 매기기').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });위 내용은 PHP에서 이미지를 ico 형식으로 변환하는 소스코드와 관련 내용을 소개하고 있으니 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되었으면 좋겠습니다.