PHP character image

Source: Internet
Author: User
PHP character image
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 :)

  1. $ Txt = array ('A', 'B', 'C', 'D', 'e', 'e', 'F', 'G', 'H ', 'I', 'J', 'k ');
  2. // Load the original image
  3. $ RawImage = ImageCreateFromGIF('logo.gif ');
  4. // Obtain the width and height of the original image
  5. $ RawImgWidth = ImagesX ($ rawImage );
  6. $ RawImgHeigh = ImagesY ($ rawImage );
  7. // Obtain the gray scale of the original image
  8. $ GrayData = getGrayData ($ rawImage, $ rawImgWidth, $ rawImgHeigh );
  9. // Destroy the image
  10. ImageDestroy ($ rawImage );
  11. // Create a text image
  12. $ TxtImage = ImageCreate ($ rawImgWidth * 6, $ rawImgHeigh * 9 );
  13. // New Image background color
  14. Imagecolorallocate ($ txtImage, 0, 0 );
  15. // Obtain the maximum gray scale
  16. For ($ I = 0; $ I $ MaxGrayArray [$ I] = max ($ grayData [$ I]);
  17. }
  18. $ MaxGray = max ($ maxGrayArray );
  19. // Set the grayscale color
  20. For ($ I = 0; $ I <$ maxGray + 1; $ I ++ ){
  21. $ Color = 255-round (200/$ maxGray) * $ I + 55;
  22. $ Gray [$ I] = imagecolorallocate ($ txtImage, $ color );
  23. }
  24. // Draw characters
  25. For ($ y = 0; $ y <$ rawImgHeigh; $ y ++ ){
  26. For ($ x = 0; $ x <$ rawImgWidth; $ x ++ ){
  27. Imagechar ($ txtImage, 1, $ x * 6, $ y * 9, $ txt [rand (0, 10)], $ gray [$ grayData [$ x] [$ y]);
  28. }
  29. }
  30. // Create the final image
  31. $ Image = ImageCreate ($ rawImgWidth * 10, $ rawImgHeigh * 10 );
  32. // Stretch the image
  33. Imagecopyresampled ($ Image, $ txtImage, 0, 0, 0, 0, $ rawImgWidth * 10, $ rawImgHeigh * 10, $ rawImgWidth * 6, $ rawImgHeigh * 9 );
  34. // Define the file header
  35. Header ('content-type: image/png ');
  36. // Output image
  37. ImagePNG ($ Image );
  38. // Destroy the image
  39. ImageDestroy ($ Image );
  40. /*
  41. Get grayscale value
  42. */
  43. Function getGrayData ($ mImage, $ mImgWidth, $ mImgHeigh ){
  44. For ($ mY = 0; $ mY <$ mImgHeigh; $ mY ++ ){
  45. For ($ mX = 0; $ mX <$ mImgWidth; $ mX ++ ){
  46. $ MRGB = Imagecolorat ($ mImage, $ mX, $ mY );
  47. $ MR = ($ mRGB> 16) & 0xFF;
  48. $ MG = ($ mRGB> 8) & 0xFF;
  49. $ MB = $ mRGB & 0xFF ;;
  50. $ MGrayData [$ mX] [$ mY] = ($ mR * 19595 + $ mG * 38469 + $ mB * 7472)> 16;
  51. }
  52. }
  53. Return $ mGrayData;
  54. }
  55. ?>

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.