侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP基于yii框架实现生成ICO图标

php  /  管理员 发布于 7年前   179

1,phpthumb_ico 是生成ICO图标的类,源码如下

class phpthumb_ico {   function phpthumb_ico() {    return true;  }    function GD2ICOstring(&$gd_image_array) {    foreach ($gd_image_array as $key => $gd_image) {       $ImageWidths[$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] == 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';          }        }        // mask bits are 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] * $ImageHeights[$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 (idCount 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 = 40 + 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,后台

引入类:

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

解决生成黑色背景的问题

imagealphablending($resize_im, false);//不合并颜色,直接用$im图像颜色替换,包括透明色
imagesavealpha($resize_im, true);//不要丢了$resize_im图像的透明色
完整方法:

/**   * icoMaker 在线生成ICO图标   * @author flashalliance   */  public function actionIco() {    $this->breadcrumbs=array_merge($this->breadcrumbs,array(        'ICO图标制作'    ));    $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[]="你上传的文件过大,最大不能超过200K。";        }        $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png");        if(!in_array($_FILES['upimage']['type'],$fileext)){          $errors[]="你上传的文件格式不正确,仅支持 png, jpg, gif格式。";        }        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)){$errors[]="图像格式错误!";          }          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);//不合并颜色,直接用$im图像颜色替换,包括透明色imagesavealpha($resize_im, true);//不要丢了$resize_im图像的透明色,解决生成黑色背景的问题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[]="生成错误请重试!";        }      }    }    $this->render ( 'ico',array('output'=>$output,'errors'=>$errors));  }

3,前台

在线制作ICO图标
beginWidget ( 'CActiveForm', array ( 'id' => 'ico-form', 'htmlOptions' => array ( 'id' => 'view_table', 'class' => 'add-form padding-10', 'enctype'=>'multipart/form-data' ), 'action'=>'/tool/ico?action=make', 'enableAjaxValidation' => false) );?>
选择文件
png,jpg,gif
endWidget(); ?>
生成结果

beginWidget ( 'CActiveForm', array ( 'id' => 'ico-form', 'htmlOptions' => array ('id' => 'view_table','class' => 'add-form padding-10', ), 'action'=>'/tool/icoDownload', 'enableAjaxValidation' => false ) ); ?>
在线制作ICO图标_favicon.ico endWidget(); ?>

再给大家分享一个独立的类

phpthumb.ico.php

  // //    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) {       $ImageWidths[$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] == 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';           }         }         // mask bits are 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] * $ImageHeights[$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 (idCount 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 = 40 + 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

 ico图标   
210000){ echo "你上传的文件体积超过了限制 最大不能超过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 "你上传的文件格式不正确 仅支持 jpg,gif,png"; exit(); } 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)){ echo "图形格式错误!"; } 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 = "生成成功!请点右键->另存为 保存到本地
点击下载"; } }else{ echo "生成错误请重试!"; } } } ?>
"; } ?>

请上传你要转换成.ico的图片

支持格式 png、jpg、gif在线转换成.ico图标。如何你想制作更丰富的.ico图标请下载ICO制作软件
目标尺寸:
".$output."
load( 'http://link.qim.net.cn/xml.xml' ); $links = $doc->getElementsByTagName( "link" ); foreach( $links as $link ) { $publishers = $link->getElementsByTagName( "homepage" ); $homepage = $publishers->item(0)->nodeValue; $titles = $link->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; $contents = $link->getElementsByTagName( "content" ); $content = $contents->item(0)->nodeValue; echo "$title
"; } ?>

您可能感兴趣的文章:

  • yii操作cookie实例简介
  • yii操作session实例简介
  • 简要剖析PHP的Yii框架的组件化机制的基本知识
  • PHP的Yii框架中YiiBase入口类的扩展写法示例
  • 详解PHP的Yii框架的运行机制及其路由功能
  • 深入解析PHP的Yii框架中的event事件机制
  • 全面解读PHP的Yii框架中的日志功能
  • 在Mac OS上搭建PHP的Yii框架及相关测试环境
  • win7安装php框架Yii的方法
  • Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例
  • PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
  • 解析PHP的Yii框架中cookie和session功能的相关操作


  • 上一条:
    合格的PHP程序员必备技能
    下一条:
    php中session定期自动清理的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客