The previous lesson of PHP Lessons Series was about converting a string to an image in php and in this lesson we will learn to extract the string back from the
image.
To extract the string from the image, we need to convert every pixel of the image to a string character. We need to know the rgb colour value of every pixel of the image and then convert any of the three values (r or g or b) to a character. The (rgb) values we set in the previous lesson were actually the ASCII values of characters, so the rgb value we get from each pixel here is actually the ASCII value of the character. The php script to extract a string from an image is given below:
In the above code the first thing we have done is we have calculated the size of the image using getimagesize() function to know it's width and height. Then we have created an image from png ('new.png') that we created in the previous lesson. In the next step we need to traverse every pixel of the image and get it's rgb values using imagecolorat() and imagecolorsforindex() functions. The $colorTran array in the above code contains the rgb values of the current pixel and we need to convert one of the three values (r or g or b) to a character using chr() function.
Stay tuned for more lessons...
Related PostsTo extract the string from the image, we need to convert every pixel of the image to a string character. We need to know the rgb colour value of every pixel of the image and then convert any of the three values (r or g or b) to a character. The (rgb) values we set in the previous lesson were actually the ASCII values of characters, so the rgb value we get from each pixel here is actually the ASCII value of the character. The php script to extract a string from an image is given below:
<?php
$size = getimagesize('new.png');
$width = $size[0];
$height = $size[1];
$image = imagecreatefrompng('new.png');
$count = 0;
for($i=0;$i<$width;$i++){
for($j=0;$j<$height;$j++){
$count++;
$colorIndex = imagecolorat($image, $i, $j);
$colorTran = imagecolorsforindex($image, $colorIndex);
$chr = chr($colorTran['red']);
echo $chr;
}
}
?>
In the above code the first thing we have done is we have calculated the size of the image using getimagesize() function to know it's width and height. Then we have created an image from png ('new.png') that we created in the previous lesson. In the next step we need to traverse every pixel of the image and get it's rgb values using imagecolorat() and imagecolorsforindex() functions. The $colorTran array in the above code contains the rgb values of the current pixel and we need to convert one of the three values (r or g or b) to a character using chr() function.
Stay tuned for more lessons...
How to create an image in PHP?
Drawing image patterns using imagesetpixel() in PHP
Convert a string to an image in PHP
Write text to image in php using imagettftext()
No comments:
Post a Comment