Home Backend Development PHP Tutorial PHP converts images to ico format source code

PHP converts images to ico format source code

Jul 29, 2016 am 09:08 AM
array image key

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>
Copy after login

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>
Copy after login

Display effect

PHP converts images to ico format source code

Source code address

ico online conversion tool has been integrated into the open source project https://github.com/diandianxiyu/ApiTesting, the online demo address will be released later

Happy New Year 2016!

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

The above introduces the source code for converting images into ico format in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

How SpringBoot monitors redis Key change events How SpringBoot monitors redis Key change events May 26, 2023 pm 01:55 PM

1. Function Overview Keyspace notification allows clients to receive events that modify Rediskey changes in some way by subscribing to channels or patterns. All commands that modify key keys. All keys that received the LPUSHkeyvalue[value…] command. All expired keys in the db database. Events are distributed through Redis's subscription and publishing functions (pub/sub), so all clients that support subscription and publishing functions can directly use the keyspace notification function without any modifications. Because the current subscription and publishing functions of Redis adopt a fireandforget strategy, if your program

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

How to solve the problem of batch deletion of key values ​​in redis How to solve the problem of batch deletion of key values ​​in redis May 31, 2023 am 08:59 AM

Problems encountered: During the development process, you will encounter keys that need to be deleted in batches according to certain rules, such as login_logID (ID is a variable). Now you need to delete data such as "login_log*", but redis itself only has batch query. Command keys for class key values, but there is no command for batch deletion of a certain class. Solution: Query first, then delete, use xargs to pass parameters (xargs can convert pipe or standard input (stdin) data into command line parameters), execute the query statement first, and then remove the queried key value and the original del parameters. delete. redis-cliKEYSkey* (search condition)|xargsr

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images on Xiaomi mobile phones? You can delete images on Xiaomi mobile phones, but most users don’t know how to delete images. Next is the tutorial on how to delete images on Xiaomi mobile phones brought by the editor. Interested users can come and join us. Let's see! How to delete images on Xiaomi mobile phone 1. First open the [Album] function in Xiaomi mobile phone; 2. Then check the unnecessary pictures and click the [Delete] button in the lower right corner; 3. Then click [Album] at the top to enter the special area , select [Recycle Bin]; 4. Then directly click [Empty Recycle Bin] as shown in the figure below; 5. Finally, directly click [Permanent Delete] to complete.

Unpatchable Yubico two-factor authentication key vulnerability breaks the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices Unpatchable Yubico two-factor authentication key vulnerability breaks the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices Sep 04, 2024 pm 06:32 PM

An unpatchable Yubico two-factor authentication key vulnerability has broken the security of most Yubikey 5, Security Key, and YubiHSM 2FA devices. The Feitian A22 JavaCard and other devices using Infineon SLB96xx series TPMs are also vulnerable.All

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Imagemagic installation Centos and Image installation tutorial Imagemagic installation Centos and Image installation tutorial Feb 12, 2024 pm 05:27 PM

LINUX is an open source operating system. Its flexibility and customizability make it the first choice of many developers and system administrators. In the LINUX system, image processing is a very important task, and Imagemagick and Image are Two very popular image processing tools, this article will introduce you to how to install Imagemagick and Image in Centos system, and provide detailed installation tutorials. Imagemagic installation Centos tutorial Imagemagick is a powerful image processing toolset, which can perform various image operations under the command line. The following are the steps to install Imagemagick on Centos system: 1

See all articles