PHP generated ICO icon based on YII Framework, PHPYII framework ico_php Tutorial

Source: Internet
Author: User
Tags transparent color php foreach

PHP generated ICO icon based on YII Framework, PHPYII framework ico


1,phpthumb_ico is the class that generates the ICO icon, the source code is as follows

Class Phpthumb_ico {function Phpthumb_ico () {return true; } function gd2icostring (& $gd _image_array) {foreach ($gd _image_array as $key + = $gd _image) {$ImageWidt      hs[$key] = imagesx ($gd _image);      $ImageHeights [$key] = Imagesy ($gd _image); $BPP [$key] = Imageistruecolor ($gd _image)?      32:24;       $totalcolors [$key] = Imagecolorstotal ($gd _image);      $icXOR [$key] = '; for ($y = $ImageHeights [$key]-1, $y >= 0; $y-) {for ($x = 0; $x < $ImageWidths [$key]; $x + +) {$a          RGB = $this->getpixelcolor ($gd _image, $x, $y);          $a = round (255 * (127-$argb [' alpha '])/127);          $r = $argb [' Red '];          $g = $argb [' green '];           $b = $argb [' Blue '];          if ($bpp [$key] = = (+) {$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 ';          }}//Mask bits is 32-bit aligned per scanline 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 _image_array as $key = + $gd _image) {$biSizeImage = $ImageWidths [$key] * $ImageHeig       hts[$key] * ($BPP [$key]/8);      BITMAPINFOHEADER-40 bytes $BitmapInfoHeader [$key] = ';               $BitmapInfoHeader [$key]. = "\x28\x00\x00\x00";      DWORD bisize;   $BitmapInfoHeader [$key]. = $this->littleendian2string ($ImageWidths [$key], 4);      LONG Biwidth; The Biheight member specifies the combined//height of the XOR and and masks.      $BitmapInfoHeader [$key]. = $this->littleendian2string ($ImageHeights [$key] * 2, 4);      LONG Biheight;                   $BitmapInfoHeader [$key]. = "\x01\x00";        WORD biplanes; $BitmapInfoHeader [$key]. = Chr ($bpp [$key]). "             \x00 ";      Wbitcount;               $BitmapInfoHeader [$key]. = "\x00\x00\x00\x00";      DWORD bicompression;      $BitmapInfoHeader [$key]. = $this->littleendian2string ($biSizeImage, 4);      DWORD biSizeImage;               $BitmapInfoHeader [$key]. = "\x00\x00\x00\x00";      LONG Bixpelspermeter;               $BitmapInfoHeader [$key]. = "\x00\x00\x00\x00";      LONG Biypelspermeter;               $BitmapInfoHeader [$key]. = "\x00\x00\x00\x00";      DWORD biclrused;               $BitmapInfoHeader [$key]. = "\x00\x00\x00\x00";    DWORD biclrimportant;                   } $icondata = "\x00\x00";  idreserved;                   Reserved (must be 0) $icondata. = "\x01\x00";    Idtype; Resource TyPE (1 for icons) $icondata. = $this->littleendian2string (count ($gd _image_array), 2);   Idcount;     How many images?    $dwImageOffset = 6 + (count ($gd _image_array) * 16); foreach ($gd _image_array as $key = + $gd _image) {//Icondirentry identries[1];//An entry for each image (Idcoun           T of ' em) $icondata. = Chr ($ImageWidths [$key]);     Bwidth;          Width, in pixels, of the image $icondata. = Chr ($ImageHeights [$key]);     Bheight;           Height, in pixels, of the image $icondata. = Chr ($totalcolors [$key]);   Bcolorcount;                   Number of colors in image (0 if >=8bpp) $icondata. = "\x00";    breserved;                 Reserved (must be 0) $icondata. = "\x01\x00";     Wplanes; Color Planes $icondata. = Chr ($bpp [$key]). "           \x00 ";    Wbitcount;      Bits per pixel $dwBytesInRes = + strlen ($icXOR [$key]) + strlen ($icAND [$key]); $icondata. = $this->littleenDian2string ($dwBytesInRes, 4);  Dwbytesinres;       How many bytes in this resource?   $icondata. = $this->littleendian2string ($dwImageOffset, 4);  Dwimageoffset;      Where in the file is this image?      $dwImageOffset + = strlen ($BitmapInfoHeader [$key]);      $dwImageOffset + = strlen ($icXOR [$key]);    $dwImageOffset + = strlen ($icAND [$key]);      } foreach ($gd _image_array as $key = + $gd _image) {$icondata. = $BitmapInfoHeader [$key];      $icondata. = $icXOR [$key];    $icondata. = $icAND [$key];  } return $icondata;    } function Littleendian2string ($number, $minbytes =1) {$intstring = ';      while ($number > 0) {$intstring = $intstring. chr ($number & 255);    $number >>= 8;  } return Str_pad ($intstring, $minbytes, "\x00", str_pad_right);    } function Getpixelcolor (& $img, $x, $y) {if (!is_resource ($img)) {return false;  } return @ImageColorsForIndex ($img, @ImageColorAt ($img, $x, $y)); } }

2, backstage

Introduction class:

Yii:: $enableIncludePath = false; Yii::import (' Application.extensions.ico.phpthumb_ico ', 1);

Solve the problem of generating a black background

Imagealphablending ($resize _im, false);//Do not merge colors, directly replace with $im image color, including transparent color
Imagesavealpha ($resize _im, true);//Do not lose the transparent color of the $resize_im image
Complete method:

/** * icomaker online Generate ico icon * @author flashalliance */Public Function Actionico () {$this->breadcrumbs=array_me    Rge ($this->breadcrumbs,array (' ico icon Maker '));    $output = "";    $errors =array (); if (Isset ($_get[' action ')) &&$_get[' action '] = = ' make ') {if (Isset ($_files[' upimage '] [' tmp_name ']) && $_files[' upimage ' [' tmp_name '] && is_uploaded_file ($_files[' upimage ' [' tmp_name '])) {if ($_files[' upimage ') [' Size ']>204800) {$errors []=] The file you uploaded is too large to exceed 200K.        ";        } $fileext = Array ("Image/pjpeg", "Image/jpeg", "Image/gif", "Image/x-png", "image/png"); if (!in_array ($_files[' upimage ' [' type '], $fileext)) {$errors []=] The file format you uploaded is incorrect and only supports PNG, JPG, GIF format.        "; if ($im = @imagecreatefrompng ($_files[' upimage ' [' tmp_name ']) or $im = @imagecreatefromgif ($_files[' upimage '] [tmp _name ']) or $im = @imagecreatefromjpeg ($_files[' upimage ' [' tmp_name '])) {$imginfo = @getimagesize ($_files[' Upimag E ' [' Tmp_naMe ']); if (!is_array ($imginfo)) {$errors []=] image format Error!          ";              } if (empty ($errors)) {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              Default              $resize _im = @imagecreatetruecolor (32,32);              $size = 32;            Break            } imagesavealpha ($im, true); Imagealphablending ($resize _im, false);//Do not merge color, directly replace with $im image color, including transparent color Imagesavealpha ($resize _im, true);//Don't Lose $resize             _im the transparent color of the image to solve the problem of generating a black background imagecopyresampled ($resize _im, $im, 0,0,0,0, $size, $size, $imginfo [0], $imginfo [1]);            Yii:: $enableIncludePath = false; Yii::impoRT (' Application.extensions.ico.phpthumb_ico ', 1);            $icon = new Phpthumb_ico ();            $GD _image_array = Array ($resize _im);            $icon _data = $icon->gd2icostring ($gd _image_array);            $bas _path=dirname (Yii::app ()->basepath); $bas _new_path= $bas _path. '            /upload/ico/';            if (!is_dir ($bas _new_path)) {mkdir ($bas _new_path, 0777, true); } $filePath = $bas _new_path. Date ("Ymdhis"). Uniqid (). Rand (1,1000).            ". ico";            if (File_put_contents ($filePath, $icon _data)) {$output = Str_replace ($bas _path, ", $filePath); }}}else{$errors []=] Build error please try again!        ";  }}} $this->render (' ico ', array (' output ' = $output, ' errors ' + $errors)); }

3, front desk

            Online production ico icon <?php$form = $this->beginwidget (' Cactiveform ', array (' id ' = ' ico-form ', ' htmloption s ' = = Array (' id ' = ' view_table ', ' class ' = ' Add-form padding-10 ', ' enctype ' = ' Multipa ') Rt/form-data '), ' action ' = '/tool/ico?action=make ', ' enableajaxvalidation ' and ' = False ');? >Uploading Files                                          Select FileSelect Size                                 16*16                         32*32                         48*48                      Supported FormatsPng,jpg,gif<?php $this->endwidget (); ><?php if (!empty ($errors) or!empty ($output)):?>Generates the result <?php if (!empty ($errors)):?>Build Failed<?php foreach ($errors as $e):?> <?php echo $e;? >
<?php Endforeach;? > <?php endif;? > <? PHP if (!empty ($output)):?> <?php $form = $this->beginwidget (' Cactiveform ', array (' id ' = = ' ic O-form ', ' htmloptions ' = = Array (' id ' = ' view_table ', ' class ' = ' = ' add-form padding -10 ',), ' action ' = '/tool/icodownload ', ' enableajaxvalidation ' and ' = False '); ?> <?php Echo Chtml::hiddenfield (' FilePath ', $output);? >Successful Build"Class=" > "margin-r-10"<?php $this->endwidget ();?> <?php endif;? > <?php endif;? >

And then we'll share a separate class.

phpthumb.ico.php

<?php////////////////////////////////////////////////////////////////Phpthumb () by James Heinrich  
 
  
  Available at http://phpthumb.sourceforge.net///////////////////////////////////////////////////////////// ////phpthumb.ico.php-. ICO output format functions//////////////////////////////////////////////////////////   Class Phpthumb_ico {function Phpthumb_ico () {return true; } function gd2icostring (& $gd _image_array) {foreach ($gd _image_array as $key + = $gd _image) {$ImageWidt       hs[$key] = imagesx ($gd _image);       $ImageHeights [$key] = Imagesy ($gd _image); $BPP [$key] = Imageistruecolor ($gd _image)?       32:24;       $totalcolors [$key] = Imagecolorstotal ($gd _image);       $icXOR [$key] = ';           for ($y = $ImageHeights [$key]-1, $y >= 0; $y-) {for ($x = 0; $x < $ImageWidths [$key]; $x + +) {           $argb = $this->getpixelcolor ($gd _image, $x, $y);           $a = round (255 * (127-$argb [' alpha '])/127);   $r = $argb [' Red '];        $g = $argb [' green '];           $b = $argb [' Blue '];           if ($bpp [$key] = = (+) {$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 ';           }}//Mask bits is 32-bit aligned per scanline 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 _image_array as $key = + $gd _image) {$biSizeImage = $ImageWidths [$key] * $ImageHe   ights[$key] * ($BPP [$key]/8);    BITMAPINFOHEADER-40 bytes $BitmapInfoHeader [$key] = ';               $BitmapInfoHeader [$key]. = "/x28/x00/x00/x00";       DWORD bisize;   $BitmapInfoHeader [$key]. = $this->littleendian2string ($ImageWidths [$key], 4);       LONG Biwidth;       The Biheight member specifies the combined//height of the XOR and and masks. $BitmapInfoHeader [$key]. = $this->littleendian2string ($ImageHeights [$key] * 2, 4);       LONG Biheight;                   $BitmapInfoHeader [$key]. = "/x01/x00";         WORD biplanes; $BitmapInfoHeader [$key]. = Chr ($bpp [$key]). "             /x00 ";       Wbitcount;               $BitmapInfoHeader [$key]. = "/x00/x00/x00/x00";       DWORD bicompression;      $BitmapInfoHeader [$key]. = $this->littleendian2string ($biSizeImage, 4);       DWORD biSizeImage;               $BitmapInfoHeader [$key]. = "/x00/x00/x00/x00";       LONG Bixpelspermeter;               $BitmapInfoHeader [$key]. = "/x00/x00/x00/x00"; LONG  Biypelspermeter;               $BitmapInfoHeader [$key]. = "/x00/x00/x00/x00";       DWORD biclrused;               $BitmapInfoHeader [$key]. = "/x00/x00/x00/x00";     DWORD biclrimportant;                   } $icondata = "/x00/x00";  idreserved;                   Reserved (must be 0) $icondata. = "/x01/x00";    Idtype; Resource Type (1 for icons) $icondata. = $this->littleendian2string (count ($gd _image_array), 2);   Idcount;     How many images?     $dwImageOffset = 6 + (count ($gd _image_array) * 16); foreach ($gd _image_array as $key = + $gd _image) {//Icondirentry identries[1];//An entry for each image (Idcou           NT of ' em) $icondata. = Chr ($ImageWidths [$key]);     Bwidth;          Width, in pixels, of the image $icondata. = Chr ($ImageHeights [$key]);     Bheight;           Height, in pixels, of the image $icondata. = Chr ($totalcolors [$key]);   Bcolorcount; Number of colors in image (0 if &GT;=8BPP) $icondata. = "/x00";    breserved;                 Reserved (must be 0) $icondata. = "/x01/x00";     Wplanes; Color Planes $icondata. = Chr ($bpp [$key]). "           /x00 ";    Wbitcount;       Bits per pixel $dwBytesInRes = + strlen ($icXOR [$key]) + strlen ($icAND [$key]);    $icondata. = $this->littleendian2string ($dwBytesInRes, 4);  Dwbytesinres;       How many bytes in this resource?   $icondata. = $this->littleendian2string ($dwImageOffset, 4);  Dwimageoffset;       Where in the file is this image?       $dwImageOffset + = strlen ($BitmapInfoHeader [$key]);       $dwImageOffset + = strlen ($icXOR [$key]);     $dwImageOffset + = strlen ($icAND [$key]);       } foreach ($gd _image_array as $key = + $gd _image) {$icondata. = $BitmapInfoHeader [$key];       $icondata. = $icXOR [$key];     $icondata. = $icAND [$key];   } return $icondata; } function Littleendian2string ($number, $minbytes =1) {$intstring = ";       while ($number > 0) {$intstring = $intstring. chr ($number & 255);     $number >>= 8;   } return Str_pad ($intstring, $minbytes, "/x00", str_pad_right);     } function Getpixelcolor (& $img, $x, $y) {if (!is_resource ($img)) {return false;   } return @ImageColorsForIndex ($img, @ImageColorAt ($img, $x, $y)); }}?>
 

index.php

 <title>ico icon</title>
 
  
    <? PHP $output = ""; if (Isset ($_get[' action ')) &&$_get[' action '] = = ' make ') {if (Isset ($_files[' upimage '] [' tmp_name ']) && $_files[' upimage ' [' tmp_name '] && is_uploaded_file ($_files[' upimage ' [' tmp_name '])) {if ($_files[' upimage ')       [' type ']>210000) {echo "The size of the file you uploaded exceeds the maximum limit of 200K";     Exit ();     } $fileext = Array ("Image/pjpeg", "Image/gif", "Image/x-png", "Image/png", "Image/jpeg", "image/jpg");       if (!in_array ($_files[' upimage ' [' type '], $fileext)) {echo "The file you uploaded is incorrectly formatted to support jpg,gif,png only";     Exit (); if ($im = @imagecreatefrompng ($_files[' upimage ' [' tmp_name ']) or $im = @imagecreatefromgif ($_files[' upimage '] [' tmp_n Ame ']) or $im = @imagecreatefromjpeg ($_files[' upimage ' [' tmp_name '])) {$imginfo = @getimagesize ($_files[' upimage '] ['       Tmp_name ']); if (!is_array ($imginfo)) {echo ' Graphics format Error!       ";           } 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           Default           $resize _im = @imagecreatetruecolor (32,32);           $size = 32;       Break       } imagecopyresampled ($resize _im, $im, 0,0,0,0, $size, $size, $imginfo [0], $imginfo [1]);       Include "phpthumb.ico.php";       $icon = new Phpthumb_ico ();       $GD _image_array = Array ($resize _im);       $icon _data = $icon->gd2icostring ($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
Click to download "; }}else{echo "Build error Please try again!" "; }}}?>

http://www.bkjia.com/PHPjc/1071227.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071227.html techarticle PHP based on YII Framework implementation of the ICO icon, PHPYII Framework ico 1,phpthumb_ico is to generate the ICO icon class, the source code is as follows class Phpthumb_ico {function Phpthumb_ico () { return true; } fun ...

  • Related Article

    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.