WebjxCom提示:
PHP代码实例:图片转成HTML.
PHP的
<style>
body{}
a{display:inline-block;width:1px;height:1px;}
</style>
<?php
functioncreateImFromFile($path){
if(!is_file($path)){
thrownewException("File:$pathnotfound!");
}
$info=getimagesize($path);
switch($info[2]){
case1:
//gif
$tim=imagecreatefromgif($path);
$w=imagesx($tim);
$h=imagesy($tim);
$im=imagecreatetruecolor($w,$h);
imagecopy($im,$tim,0,0,0,0,$w,$h);
imagedestroy($tim);
break;
case2:
//jpg
$im=imagecreatefromjpeg($path);
break;
case3:
//png
$im=imagecreatefrompng($path);
break;
default:
thrownewException("Notsupportfiletype.File:$path");
}
return$im;
}
$im=createImFromFile("d:\aa.jpg");
$w=imagesx($im);
$h=imagesy($im);
$str="<div>";
for($i=0;$i<$h;$i++){
$str.="<div>";
for($j=0;$j<$w;$j++){
$rgb=str_pad(dechex(imagecolorat($im,$j,$i)),6,"0",STR_PAD_LEFT);
$str.="<astyle='background:#$rgb'></a>";
}
$str.="</div>";
}
$str.="</div>";
imagedestroy($im);
echo$str;
/*$rgb=ImageColorAt($im,100,100);
$r=($rgb>>16)&0xFF;
$g=($rgb>>8)&0xFF;
$b=$rgb&0xFF;*/
?>
.NET的:
protectedstringtmp="";
protectedvoidPage_Load(objectsender,EventArgse){
Bitmapbm=newBitmap("d:\aa.jpg");
intw=bm.Width;
inth=bm.Height;
StringBuildersb=newStringBuilder();
Colorc;
sb.Append("<div>");
for(inti=0;i<h;i++){
sb.Append("<div>");
for(intj=0;j<w;j++){
c=bm.GetPixel(j,i);
sb.Append(string.Format("<astyle='background:#{0}{1}{2}'></a>",V(c.R),V(c.G),V(c.B)));
}
sb.Append("<div>");
}
sb.Append("</div>");
tmp=sb.ToString();
}
privatestringV(intv){
returnstring.Format("{0:X}",v).PadLeft(2,'0');
}
等我一步一步解开他的混淆后
,发现:它只是一个笑话!
另外说明一下:
在PHP里没有直接使用createimagefromgif是因为:
imagecoloratreturnsthecolorindex(positioninthepalette)for
palettebasedimageorthecolorvaluefortruecolorimages.
imagecreatefromjpegcreatestruecolorimages,always.
imagecreatefromgifcreatesalwayspalettebasedimagesand
imagecreatefrompngcreateseitherpaletteortruecolorimagesasPNG
supportsbothimagetypes.