Source code sharing for converting PHP images to ico format

Source: Internet
Author: User
The pure PHP source code for online ico Image conversion is integrated into open-source projects,

Class
  $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));    }}?>
Controller
If ($ error ['text'] = "" & isset ($ _ FILES ['upimage'] ['tmp _ name']) & $ _ FILES ['upload'] ['tmp _ name'] & is_uploaded_file ($ _ FILES ['upload'] ['tmp _ name']) {if ($ _ FILES ['upimage'] ['type']> 210000) {$ error ['text'] = "the size of the file you uploaded exceeds the limit and cannot exceed 200 KB";} 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'] = "The format of the file you uploaded is incorrect. only jpg, gif, and png files are supported ";} 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'] = "incorrect graphic format! ";}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; defaul T; $ 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 =" generated successfully! Right-click and choose save as local.
Click to download "; // echo $ filename; // data display $ icon_arr = ['class' => '', 'Time' => date ("Y-m-d H: I: s"), 'filename' => $ _ FILES ['upimage'] ['name'], 'filepath' => $ filename, 'size' => $ size] ;}} else {$ error ['text'] = "generate error please try again" ;}}} else {$ error ['text'] = "select an image! ";}
Display effect

The above is the detailed content shared by the source code for converting PHP images to ico format. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.