首页 > 开发 > PHP > 正文

JPG,PNG,GIF转Ascii

2024-05-04 22:59:51
字体:
来源:转载
供稿:网友
这个范例可以用来图形转xpm给php_gtk用的,不过大家先看一下这个简单程序码,先了解原理,说xpm大概没几个知道,只好改写成这个范例。图形转ascii。

 
<?
## 转换图档
$pic   "jerry.jpg"
;
## 欲转换之字符串
$ascii "#"
;    

$imagesize=getimagesize($pic
);

if(
is_array($imagesize
)):
   
$width  =$imagesize[0]; 
## 获取宽度
   
$height =$imagesize[1]; 
## 获取长度 
   
switch($imagesize[2]):  
## 判断格式,因为4是swf,所以就没写出来
      
case '1'
:
         if(
imagetypes() & img_gif
):
            
$img=imagecreatefromgif($pic
);            
         else:
            exit(
"系统不支持gif格式图档"
);
         endif;
      break;
      case 
'2'
:
         if(
imagetypes() & img_jpg
):
            
$img=imagecreatefromjpeg($pic
);
         else:
            exit(
"系统不支持jpg格式图档"
);
         endif;
      break;
      case 
'3'
:
         if(
imagetypes() & img_png
):
            
$img=imagecreatefrompng($pic
);
         else:
            exit(
"系统不支持png格式图档"
);
         endif;
      break;
      default:
         exit(
"系统无法辨识"
);
      break;
   endswitch;
   for(
$y=1;$y<=$height;$y
++):
      for(
$x=1;$x<=$width;$x
++):
         
## 取点索引
         
$index imagecolorat($img,$x,$y
); 
         
## 获取rgb颜色
         
$rgb   imagecolorsforindex($img,$index
);
         
## 255,255,255(rgb)转ffffff(html用的)此种格式
         
$color dechex($rgb[red]).dechex($rgb[green]).dechex($rgb[blue
]); 
         echo 
"<font color=$color size=1>$ascii</font>"
;   
      endfor;
      echo 
"<br>"
;
   endfor;
endif;
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表