php生成ico图标使用phpthumb_ico类完整实例,icophpthumb_ico这里我使用的是yii框架。
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 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 $scanlinemaskbits) { for ($i = 0; $i $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) );?> 上传文件
选择文件
选择尺寸 16*16 32*32 48*48
支持格式 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 ) ); ?> 成功生成 class=margin-r-10>
endwidget(); ?>
效果图:
演示地址
http://www.bkjia.com/phpjc/1070038.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1070038.htmltecharticlephp生成ico图标使用phpthumb_ico类完整实例,icophpthumb_ico 这里我使用的是yii框架。 1,phpthumb_ico 是生成ico图标的类,源码如下 class phpthumb_ico...