See this old man's post to interest http://www.oschina.net/code/snippet_234517_15356 By default, the image is enlarged by 10 times, so we can see it clearly ...... The OSC logo is used for testing. let's see how it works. (The picture is scaled down, the new window opens this address to see, 2000*560 graph http://img.it-home.org/data/attachment/forum/2014pic/11033136_K1mt.png) ========================================================== ================ I have tried PNG, GIF, and JPG to convert them to normal characters :)
- $ Txt = array ('A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H ', 'I', 'J', 'k ');
- // Load the original image
- $ RawImage = ImageCreateFromGIF('logo.gif ');
- // Obtain the width and height of the original image
- $ RawImgWidth = ImagesX ($ rawImage );
- $ RawImgHeigh = ImagesY ($ rawImage );
- // Obtain the gray scale of the original image
- $ GrayData = getGrayData ($ rawImage, $ rawImgWidth, $ rawImgHeigh );
- // Destroy the image
- ImageDestroy ($ rawImage );
- // Create a text image
- $ TxtImage = ImageCreate ($ rawImgWidth * 6, $ rawImgHeigh * 9 );
- // New Image background color
- Imagecolorallocate ($ txtImage, 0, 0 );
- // Obtain the maximum gray scale
- For ($ I = 0; $ I $ MaxGrayArray [$ I] = max ($ grayData [$ I]);
- }
- $ MaxGray = max ($ maxGrayArray );
- // Set the grayscale color
- For ($ I = 0; $ I <$ maxGray + 1; $ I ++ ){
- $ Color = 255-round (200/$ maxGray) * $ I + 55;
- $ Gray [$ I] = imagecolorallocate ($ txtImage, $ color );
- }
- // Draw characters
- For ($ y = 0; $ y <$ rawImgHeigh; $ y ++ ){
- For ($ x = 0; $ x <$ rawImgWidth; $ x ++ ){
- Imagechar ($ txtImage, 1, $ x * 6, $ y * 9, $ txt [rand (0, 10)], $ gray [$ grayData [$ x] [$ y]);
- }
- }
- // Create the final image
- $ Image = ImageCreate ($ rawImgWidth * 10, $ rawImgHeigh * 10 );
- // Stretch the image
- Imagecopyresampled ($ Image, $ txtImage, 0, 0, 0, 0, $ rawImgWidth * 10, $ rawImgHeigh * 10, $ rawImgWidth * 6, $ rawImgHeigh * 9 );
- // Define the file header
- Header ('content-type: image/png ');
- // Output image
- ImagePNG ($ Image );
- // Destroy the image
- ImageDestroy ($ Image );
- /*
- Get grayscale value
- */
- Function getGrayData ($ mImage, $ mImgWidth, $ mImgHeigh ){
- For ($ mY = 0; $ mY <$ mImgHeigh; $ mY ++ ){
- For ($ mX = 0; $ mX <$ mImgWidth; $ mX ++ ){
- $ MRGB = Imagecolorat ($ mImage, $ mX, $ mY );
- $ MR = ($ mRGB> 16) & 0xFF;
- $ MG = ($ mRGB> 8) & 0xFF;
- $ MB = $ mRGB & 0xFF ;;
- $ MGrayData [$ mX] [$ mY] = ($ mR * 19595 + $ mG * 38469 + $ mB * 7472)> 16;
- }
- }
- Return $ mGrayData;
- }
- ?>
|