PHP conversion image for ICO format source code

Source: Internet
Author: User
Class


                () {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)? +: -;$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] = = +){$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]) {$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) * ); 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; } 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[' Upimage '][' Tmp_name '] && Is_uploaded_file ($_files[' Upimage '][' Tmp_name '])) {if($_files[' Upimage '][' type '] >210000) {$error[' text '] ="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 you uploaded is not properly formatted to support Jpg,gif,png only"; }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 ']) { Case1;$resize _im=@imagecreatetruecolor( -, -);$size= -; Break; Case2;$resize _im=@imagecreatetruecolor( +, +);$size= +; Break; Case3;$resize _im=@imagecreatetruecolor( -, -);$size= -; Break; Case4;$resize _im=@imagecreatetruecolor( -, -);$size= -; Break; Case5;$resize _im=@imagecreatetruecolor( -, -);$size= -; Break; Default$resize _im=@imagecreatetruecolor( -, -);$size= -; Break; } imagecopyresampled ($resize _im,$im,0,0,0,0,$size,$size,$imginfo[0],$imginfo[1]);$icon= newIconv();$GD _image_array= Array ($resize _im);$icon _data=$icon-Gdtoicostr($GD _image_array);$filename="temp/". Date"Ymdhis") . Rand1, +) .". ico";if(File_put_contents ($filename,$icon _data)) {//$output="The build was successful!" Click Save As saved to local
/"mce_href="/""". $filename."/"target=/"_blank/"> 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 '] ="Build error please retry"; } } } }Else{$error[' text '] ="Please select a picture!"; }

Show effect

Source Address

ICO Online conversion tool has been integrated into the open source project Https://github.com/diandianxiyu/ApiTesting, the online demo address will be released later

Happy New Year 2016!

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP conversion image for the ICO format source code, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.