Heim > Backend-Entwicklung > PHP-Tutorial > PHP转换图片为ico格式源码

PHP转换图片为ico格式源码

WBOY
Freigeben: 2016-07-29 09:08:25
Original
1496 Leute haben es durchsucht

Class

<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>
Nach dem Login kopieren

Controller

<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>
Nach dem Login kopieren

展示效果

PHP转换图片为ico格式源码

源码地址

ico在线转换工具已经集成到了开源项目 https://github.com/diandianxiyu/ApiTesting 中 ,在线demo地址稍后再放出

2016年新年快乐!

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP转换图片为ico格式源码,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage