php在线生成ico文件的代码
程序员文章站
2022-08-29 19:15:00
index.php复制代码 代码如下:
index.php
<?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 "你上传的文件体积超过了限制 最大不能超过200k";
exit();
}
$fileext = array("image/pjpeg","image/gif","image/x-png");
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 = "生成成功!请点右键->另存为 保存到本地<br><a href=\"".$filename."\" target=\"_blank\">点击下载</a>";
}
}else{
echo "生成错误请重试!";
}
}
}
?>
<html>
<head>
<title>ico图标在线转换</title>
<style>
body{background-color:#fff;color:#000000;font-family:arial;margin:30px;font-size:12px;}
table{border:0}
td{line-height:16px;}
label{cursor:hand;}
</style>
</head>
<body>
<h1>ico图标在线转换</h1>
<form action="?action=make" method="post" enctype='multipart/form-data'>
<table>
<tr>
<td><b>请上传你要转换的图片</b><br>支持格式 png,jpg,gif</td>
</tr>
<tr>
<td><input type="file" name="upimage" size="30"></td>
</tr>
<tr>
<td>目标尺寸:
<input type="radio" name="size" value="1" id="s1"><label for="s1">16*16</label>
<input type="radio" name="size" value="2" id="s2" checked><label for="s2">32*32</label>
<input type="radio" name="size" value="3" id="s3"><label for="s3">48*48</label>
</td>
</tr>
<tr>
<td align="right"><input type="submit" value="生 成"></td>
</tr>
<?php
if($output){
echo "<tr><td><div style=\"border:1px solid #d8d8b2;background-color:#ffffdd;padding:10px\">".$output."</div></td></tr>";
}
?>
<tr>
<td><div style="color:#cccccc;font-size:11px;">powered by <a href="http://veryim.com" style="color:#cccccc">非常爱漫</a> </div></td>
</tr>
</table>
</form>
</body>
</html>
phpthumb.ico.php
<?php
//////////////////////////////////////////////////////////////
/// phpthumb() by james heinrich <info@silisoftware.com> //
// 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));
}
}
?>
复制代码 代码如下:
<?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 "你上传的文件体积超过了限制 最大不能超过200k";
exit();
}
$fileext = array("image/pjpeg","image/gif","image/x-png");
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 = "生成成功!请点右键->另存为 保存到本地<br><a href=\"".$filename."\" target=\"_blank\">点击下载</a>";
}
}else{
echo "生成错误请重试!";
}
}
}
?>
<html>
<head>
<title>ico图标在线转换</title>
<style>
body{background-color:#fff;color:#000000;font-family:arial;margin:30px;font-size:12px;}
table{border:0}
td{line-height:16px;}
label{cursor:hand;}
</style>
</head>
<body>
<h1>ico图标在线转换</h1>
<form action="?action=make" method="post" enctype='multipart/form-data'>
<table>
<tr>
<td><b>请上传你要转换的图片</b><br>支持格式 png,jpg,gif</td>
</tr>
<tr>
<td><input type="file" name="upimage" size="30"></td>
</tr>
<tr>
<td>目标尺寸:
<input type="radio" name="size" value="1" id="s1"><label for="s1">16*16</label>
<input type="radio" name="size" value="2" id="s2" checked><label for="s2">32*32</label>
<input type="radio" name="size" value="3" id="s3"><label for="s3">48*48</label>
</td>
</tr>
<tr>
<td align="right"><input type="submit" value="生 成"></td>
</tr>
<?php
if($output){
echo "<tr><td><div style=\"border:1px solid #d8d8b2;background-color:#ffffdd;padding:10px\">".$output."</div></td></tr>";
}
?>
<tr>
<td><div style="color:#cccccc;font-size:11px;">powered by <a href="http://veryim.com" style="color:#cccccc">非常爱漫</a> </div></td>
</tr>
</table>
</form>
</body>
</html>
phpthumb.ico.php
复制代码 代码如下:
<?php
//////////////////////////////////////////////////////////////
/// phpthumb() by james heinrich <info@silisoftware.com> //
// 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));
}
}
?>