Home  >  Article  >  Backend Development  >  Source code sharing for converting images to ico format using PHP

Source code sharing for converting images to ico format using PHP

黄舟
黄舟Original
2017-03-11 09:38:472365browse

Class

<?php
namespace  App\Libs;

class Iconv {
    function phpmake_ico() {
        return true;
    }
    function GDtoICOstr(&$gd_ico_array) {
        foreach ($gd_ico_array as $key => $gd_image) {            
        $IcoWidths[$key]  = ImageSX($gd_image);            
        $IcoHeights[$key] = ImageSY($gd_image);            
        $bpp[$key]          = ImageIsTrueColor($gd_image) ? 32 : 24;            
        $totalcolors[$key]  = ImageColorsTotal($gd_image);            
        $icXOR[$key] = &#39;&#39;;
            for ($y = $IcoHeights[$key] - 1; $y >= 0; $y--) {
                for ($x = 0; $x < $IcoWidths[$key]; $x++) {                    
                $argb = $this->gpc($gd_image, $x, $y);                    
                $a = round(255 * ((127 - $argb[&#39;alpha&#39;]) / 127));                    
                $r = $argb[&#39;red&#39;];                    
                $g = $argb[&#39;green&#39;];                    
                $b = $argb[&#39;blue&#39;];
                    if ($bpp[$key] == 32) {                        
                    $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
                    } elseif ($bpp[$key] == 24) {                        
                    $icXOR[$key] .= chr($b).chr($g).chr($r);
                    }
                    if ($a < 128) {
                        @$icANDmask[$key][$y] .= &#39;1&#39;;
                    } else {
                        @$icANDmask[$key][$y] .= &#39;0&#39;;
                    }
                }
                while (strlen($icANDmask[$key][$y]) % 32) {                    
                $icANDmask[$key][$y] .= &#39;0&#39;;
                }
            }            $icAND[$key] = &#39;&#39;;
            foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
                for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {                    
                $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, &#39;0&#39;, STR_PAD_LEFT)));
                }
            }
        }
        foreach ($gd_ico_array as $key => $gd_image) {            
        $biSizeImage = $IcoWidths[$key] * $IcoHeights[$key] * ($bpp[$key] / 8);            
        $bfh[$key]  = &#39;&#39;;            
        $bfh[$key] .= "\x28\x00\x00\x00";            
        $bfh[$key] .= $this->le2s($IcoWidths[$key], 4);            
        $bfh[$key] .= $this->le2s($IcoHeights[$key] * 2, 4);            
        $bfh[$key] .= "\x01\x00";            
        $bfh[$key] .= chr($bpp[$key])."\x00";            
        $bfh[$key] .= "\x00\x00\x00\x00";            
        $bfh[$key] .= $this->le2s($biSizeImage, 4);            
        $bfh[$key] .= "\x00\x00\x00\x00";            
        $bfh[$key] .= "\x00\x00\x00\x00";            
        $bfh[$key] .= "\x00\x00\x00\x00";            
        $bfh[$key] .= "\x00\x00\x00\x00";
        }        
        $icondata  = "\x00\x00";        
        $icondata .= "\x01\x00";        
        $icondata .= $this->le2s(count($gd_ico_array), 2);        
        $dwImageOffset = 6 + (count($gd_ico_array) * 16);
        foreach ($gd_ico_array as $key => $gd_image) {            
        $icondata .= chr($IcoWidths[$key]);            
        $icondata .= chr($IcoHeights[$key]);            
        $icondata .= chr($totalcolors[$key]);            
        $icondata .= "\x00";            
        $icondata .= "\x01\x00";            
        $icondata .= chr($bpp[$key])."\x00";            
        $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);            
        $icondata .= $this->le2s($dwBytesInRes, 4);            
        $icondata .= $this->le2s($dwImageOffset, 4);            
        $dwImageOffset += strlen($bfh[$key]);            
        $dwImageOffset += strlen($icXOR[$key]);            
        $dwImageOffset += strlen($icAND[$key]);
        }
        foreach ($gd_ico_array as $key => $gd_image) {            
        $icondata .= $bfh[$key];            
        $icondata .= $icXOR[$key];            
        $icondata .= $icAND[$key];
        }
        return $icondata;
    }
    function le2s($number, $minbytes=1) {        
    $intstring = &#39;&#39;;
        while ($number > 0) {            
        $intstring = $intstring.chr($number & 255);            
        $number >>= 8;
        }
        return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
    }
    function gpc(&$img, $x, $y) {
        if (!is_resource($img)) {
            return false;
        }
        return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
    }
}
?>

Controller

if ( $error[&#39;text&#39;] == "" && isset($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;]) && $_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;] && is_uploaded_file($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;])) {
                if ($_FILES[&#39;upimage&#39;][&#39;type&#39;] > 210000) {
                    $error[&#39;text&#39;] = "你上传的文件体积超过了限制 最大不能超过200k";
                } else {
                    $fileext = array("image/pjpeg", "image/gif", "image/x-png", "image/png", "image/jpeg", "image/jpg");
                    if (!in_array($_FILES[&#39;upimage&#39;][&#39;type&#39;], $fileext)) {
                        $error[&#39;text&#39;] = "你上传的文件格式不正确 仅支持 jpg,gif,png";
                    }else {
                        if ($im = @imagecreatefrompng($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;]) or $im = @imagecreatefromgif($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;]) 
                        or $im = @imagecreatefromjpeg($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;])) {
                            $imginfo = @getimagesize($_FILES[&#39;upimage&#39;][&#39;tmp_name&#39;]);
                            if (!is_array($imginfo)) {
                                $error[&#39;text&#39;] = "图形格式错误!";
                            }else {
                                switch ($_POST[&#39;size&#39;]) {
                                    case 1;
                                        $resize_im = @imagecreatetruecolor(16, 16);
                                        $size = 16;
                                        break;
                                    case 2;
                                        $resize_im = @imagecreatetruecolor(32, 32);
                                        $size = 32;
                                        break;
                                    case 3;
                                        $resize_im = @imagecreatetruecolor(48, 48);
                                        $size = 48;
                                        break;
                                    case 4;
                                        $resize_im = @imagecreatetruecolor(64, 64);
                                        $size = 64;
                                        break;
                                    case 5;
                                        $resize_im = @imagecreatetruecolor(128, 128);
                                        $size = 128;
                                        break;
                                    default;
                                        $resize_im = @imagecreatetruecolor(64, 64);
                                        $size = 64;
                                        break;
                                }
                                imagecopyresampled($resize_im, $im, 0, 0, 0, 0, $size, $size, $imginfo[0], $imginfo[1]);

                                $icon = new Iconv();

                                $gd_image_array = array($resize_im);
                                $icon_data = $icon->GDtoICOstr($gd_image_array);
                                $filename = "temp/" . date("Ymdhis") . rand(1, 1000) . ".ico";
                                if (file_put_contents($filename, $icon_data)) {
//                            $output = "生成成功!请点右键->另存为 保存到本地<br><a href="/" mce_href="/""".$filename."/" target=/"_blank/">点击下载</a>";
//                                    echo $filename;
                                    //数据展示
                                    $icon_arr=[
                                        &#39;class&#39;=>&#39;&#39;,
                                        &#39;time&#39;=>date("Y-m-d H:i:s"),
                                        &#39;filename&#39;=>$_FILES[&#39;upimage&#39;][&#39;name&#39;],
                                        &#39;filepath&#39;=>$filename,
                                        &#39;size&#39;=>$size
                                    ];
                                }
                            }
                        } else {
                                $error[&#39;text&#39;] = "生成错误请重试";

                        }
                    }
                }
            }else{
                $error[&#39;text&#39;] = "请选择图片!";
            }

Display effect

Source code sharing for converting images to ico format using PHP

The above is the detailed content of Source code sharing for converting images to ico format using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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