> 백엔드 개발 > PHP 튜토리얼 > PHP는 이미지를 ico 형식 소스 코드로 변환합니다.

PHP는 이미지를 ico 형식 소스 코드로 변환합니다.

WBOY
풀어 주다: 2016-07-29 09:08:25
원래의
1495명이 탐색했습니다.

클래스

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

표시 효과

PHP는 이미지를 ico 형식 소스 코드로 변환합니다.

소스 코드 주소

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 튜토리얼에 관심이 있는 친구들에게 도움이 되었으면 좋겠습니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿