欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

【PHP】PHP转换图片为ico格式源码 PHP转换图片为ico格式的源码分享

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] = '';
            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['alpha']) / 127));
                    $r = $argb['red'];
                    $g = $argb['green'];
                    $b = $argb['blue'];
                    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] .= '1';
                    } else {
                        @$icANDmask[$key][$y] .= '0';
                    }
                }
                while (strlen($icANDmask[$key][$y]) % 32) {
                    $icANDmask[$key][$y] .= '0';
                }
            }
            $icAND[$key] = '';
            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, '0', STR_PAD_LEFT)));
                }
            }
        }
        foreach ($gd_ico_array as $key => $gd_image) {
            $biSizeImage = $IcoWidths[$key] * $IcoHeights[$key] * ($bpp[$key] / 8);
            $bfh[$key]  = '';
            $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 = '';
        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));
    }
}
?>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

Controller

if ( $error['text'] == "" && isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])) {
                if ($_FILES['upimage']['type'] > 210000) {
                    $error['text'] = "你上传的文件体积超过了限制 最大不能超过200k";
                } else {
                    $fileext = array("image/pjpeg", "image/gif", "image/x-png", "image/png", "image/jpeg", "image/jpg");
                    if (!in_array($_FILES['upimage']['type'], $fileext)) {
                        $error['text'] = "你上传的文件格式不正确 仅支持 jpg,gif,png";
                    }else {
                        if ($im = @imagecreatefrompng($_FILES['upimage']['tmp_name']) or $im = @imagecreatefromgif($_FILES['upimage']['tmp_name']) or $im = @imagecreatefromjpeg($_FILES['upimage']['tmp_name'])) {
                            $imginfo = @getimagesize($_FILES['upimage']['tmp_name']);
                            if (!is_array($imginfo)) {
                                $error['text'] = "图形格式错误!";
                            }else {
                                switch ($_POST['size']) {
                                    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=[
                                        'class'=>'',
                                        'time'=>date("Y-m-d H:i:s"),
                                        'filename'=>$_FILES['upimage']['name'],
                                        'filepath'=>$filename,
                                        'size'=>$size
                                    ];
                                }
                            }
                        } else {
                                $error['text'] = "生成错误请重试";

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

展示效果

这里写图片描述

源码地址

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

来自 http://blog.csdn.net/diandianxiyu_geek/article/details/50443364
 

PHP转换图片为ico格式的源码分享
作者: default|标签:PHP 转换图片 ico格式|2017-3-11 09:38

 

Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?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] = '';
            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['alpha']) / 127));                   
                $r $argb['red'];                   
                $g $argb['green'];                   
                $b $argb['blue'];
                    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] .= '1';
                    else {
                        @$icANDmask[$key][$y] .= '0';
                    }
                }
                while (strlen($icANDmask[$key][$y]) % 32) {                   
                $icANDmask[$key][$y] .= '0';
                }
            }            $icAND[$key] = '';
            foreach ($icANDmask[$keyas $y => $scanlinemaskbits) {
                for ($i = 0; $i strlen($scanlinemaskbits); $i += 8) {                   
                $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits$i, 8), 8, '0', STR_PAD_LEFT)));
                }
            }
        }
        foreach ($gd_ico_array as $key => $gd_image) {           
        $biSizeImage $IcoWidths[$key] * $IcoHeights[$key] * ($bpp[$key] / 8);           
        $bfh[$key]  = '';           
        $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 '';
        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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if $error['text'] == "" && isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])) {
                if ($_FILES['upimage']['type'] > 210000) {
                    $error['text'] = "你上传的文件体积超过了限制 最大不能超过200k";
                else {
                    $fileext array("image/pjpeg""image/gif""image/x-png""image/png""image/jpeg""image/jpg");
                    if (!in_array($_FILES['upimage']['type'], $fileext)) {
                        $error['text'] = "你上传的文件格式不正确 仅支持 jpg,gif,png";
                    }else {
                        if ($im = @imagecreatefrompng($_FILES['upimage']['tmp_name']) or $im = @imagecreatefromgif($_FILES['upimage']['tmp_name'])
                        or $im = @imagecreatefromjpeg($_FILES['upimage']['tmp_name'])) {
                            $imginfo = @getimagesize($_FILES['upimage']['tmp_name']);
                            if (!is_array($imginfo)) {
                                $error['text'] = "图形格式错误!";
                            }else {
                                switch ($_POST['size']) {
                                    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=[
                                        'class'=>'',
                                        'time'=>date("Y-m-d H:i:s"),
                                        'filename'=>$_FILES['upimage']['name'],
                                        'filepath'=>$filename,
                                        'size'=>$size
                                    ];
                                }
                            }
                        else {
                                $error['text'] = "生成错误请重试";
 
                        }
                    }
                }
            }else{
                $error['text'] = "请选择图片!";
            }

展示效果

 

这里写图片描述

以上就是PHP转换图片为ico格式的源码分享的详细内容

来自 http://www.php.cn/php-weizijiaocheng-355999.html
普通分类: