PHP conversion image for the ICO format source sharing

Source: Internet
Author: User

Class

<?phpnamespace 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-&GT;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] = = $icXOR [$key]. = Chr ($b). chr ($g). chr ($R);                    if ($a <) {@ $icANDmask [$key] [$y]. = ' 1 ';                    } else {@ $icANDmask [$key] [$y]. = ' 0 '; }} while (Strlen ($icANDmask [$key] [$y]) {$icANDmas                k[$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 = + 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; } functionLe2s ($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[' upimage ' [' Tmp_name '] &                     amp;& is_uploaded_file ($_files[' upimage ' [' tmp_name ']) {if ($_files[' upimage ' [' type '] > 210000) {                $error [' text '] = "The size of the file you uploaded exceeds the maximum limit of 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 '] = "The file format you uploaded is not properly supported JPG,GIF,PN                    G "; }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 '] = "graphic format Error!"                            ";                                        }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 = "Build succeeded! Click Save as saved to local <br><a href= "/" mce_href= "/" ". $filename."                                    /"target=/" _blank/"> Click to download </a>";//Echo $filename;                                        Data show $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 retry"; }}}}else{$error [' text '] = "Please select picture!"            ; }

Show effect

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.