class
$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->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] == 24) { $icxor[$key] .= chr($b).chr($g).chr($r); } if ($a 128) { @$icandmask[$key][$y] .= '1'; } else { @$icandmask[$key][$y] .= '0'; } } while (strlen($icandmask[$key][$y]) % 32) { $icandmask[$key][$y] .= '0'; } } $icand[$key] = ''; foreach ($icandmask[$key] as $y => $scanlinemaskbits) { for ($i = 0; $i ($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 = 40 + 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'] = 你上传的文件体积超过了限制 最大不能超过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'] = 你上传的文件格式不正确 仅支持 jpg,gif,png; }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'] = 图形格式错误!; }else { switch ($_post['size']) { case1; $resize_im = @imagecreatetruecolor(16, 16); $size = 16; break; case2; $resize_im = @imagecreatetruecolor(32, 32); $size = 32; break; case3; $resize_im = @imagecreatetruecolor(48, 48); $size = 48; break; case4; $resize_im = @imagecreatetruecolor(64, 64); $size = 64; break; case5; $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 = 生成成功!请点右键->另存为 保存到本地
.$filename./ target=/_blank/>点击下载;// echo $filename; //数据展示 $icon_arr=[ 'class'=>'', 'time'=>date(y-m-d h:i:s), 'filename'=>$_files['upimage']['name'], 'filepath'=>$filename, 'size'=>$size ]; } } } else { $error['text'] = 生成错误请重试; } } } }else{ $error['text'] = 请选择图片!; }
展示效果
源码地址
ico在线转换工具已经集成到了开源项目 https://github.com/diandianxiyu/apitesting 中 ,在线demo地址稍后再放出
2016年新年快乐!
').addclass('pre-numbering').hide(); $(this).addclass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadein(1700); }); }); 以上就介绍了php转换图片为ico格式源码,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。