I got interested after seeing this guy’s post.
http://www.oschina.net/code/snippet_234517_15356
The image is magnified 10 times by default, so you can see it more clearly...
The OSC logo was used in the test. Let’s see how it works.
(The picture has been abbreviated, open this address in a new window to see the picture, 2000*560
http://img.it-home.org/data/attachment/forum/2014pic/11033136_K1mt.png)
================================================= ===
I tried PNG, GIF, and JPG, and they can all be characterized normally :)
- $txt =array('A','B','C','D','E','F','G','H','I', 'J','K');
- //Load the original image
- $rawImage = ImageCreateFromGIF('logo.gif');
- //Get the original image width and height
- $rawImgWidth = ImagesX($rawImage);
- $rawImgHeigh = ImagesY($rawImage);
- //Get the original image grayscale
- $grayData = getGrayData($rawImage,$rawImgWidth,$rawImgHeigh);
- //Destroy the image
- ImageDestroy($rawImage);
- //Create text image
- $ txtImage = ImageCreate($rawImgWidth*6,$rawImgHeigh*9);
- //New image background color
- imagecolorallocate($txtImage,0,0,0);
- //Get the maximum grayscale
- for($i=0; $i $maxGrayArray[$i] = max($grayData[$i]);
- }
- $maxGray = max($maxGrayArray);
- //Set grayscale correspondence Color
- for($i=0;$i<$maxGray+1;$i++){
- $color = 255-round(200/$maxGray)*$i+55;
- $gray[$i] = imagecolorallocate( $txtImage,$color,$color,$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 file header
- header('Content-type: image/png');
- //Output image
- ImagePNG( $Image);
- //Destroy the image
- ImageDestroy($Image);
- /*
- Get the gray 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;
- }
- ?>
Copy code
|