Jpg,png,gif Turn ASCII

Source: Internet
Author: User
Tags exit
This example can be used for graphics to xpm to PHP_GTK use, but we first look at this simple code, first understand the principle, said xpm probably few know, had to rewrite into this paradigm. The graphics turn to ASCII.

?
# # Convert Map file
$Pic = "jerry.jpg" ;
# # to convert the string
$Ascii = "#" ;

$ImageSize=getimagesize($Pic );

If (is_array($ImageSize )):
$Width =$ImageSize[0]; # # Get width
$Height =$ImageSize[1]; # # Get length
Switch ($ImageSize[2]): # # judgment format, because 4 is SWF, so it's not written out
case ' 1 ' :
If (imagetypes() & img_gif ):
$img=imagecreatefromgif($Pic );
Else:
Exit ("system does not support GIF format map" );
EndIf;
break;
case ' 2 ' :
If (imagetypes() & img_jpg ):
$img=imagecreatefromjpeg($Pic );
Else:
Exit ("system does not support JPG map files" );
EndIf;
break;
case ' 3 ' :
If (imagetypes() & img_png ):
$img=imagecreatefrompng($Pic );
Else:
Exit (The system does not support PNG format map files) );
EndIf;
break;
Default:
Exit ("System not recognized" );
break;
Endswitch;
For ($y=1; $y <= $Height ; $y ++):
For ($x=1; $x <= $Width ; $x ++):
# # Fetch Point Index
$index = imagecolorat($img,$x , $y );
# # get RGB color
$rgb = imagecolorsforindex($img,$ Index );
# # 255,255,255 (RGB) to FFFFFF (HTML) This format
$color = dechex($rgb[Red]). Dechex ($rgb[Green]). Dechex ($rgb[Blue ]);
Echo "<font color= $color size=1> $Ascii </font>" ;
ENDfor;
Echo "<BR>" ;
ENDfor;
EndIf;
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.