Posts Tagged ‘gd’

Basic OCR

4. January 2010

1 Comment »

This is a very basic optical character recognition script written in PHP. This is untested and serves merely a proof of concept. As noted in the comments, adjusting the sample size can improve results, since with a large sample size on a small image there can be many collisions. A database is needed to compare output results of this script with known values.

<?php

/* create a test image */
$im = @imagecreate(100, 20) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 1, 5, 5,  "Hello, World!", $text_color);

/***
 * Assumptions:
 *   A monochrome image where characters are black
 *   A single character is connected
 *   Characters are disjointed by white space
 */

$width = imagesx($im);
$height = imagesy($im);

/***
 * Notes:
 *   The smaller the sample size the more accurate it will be,
 *   however, it will take longer. Larger images can use a larger
 *   sample size wihtout compromizing much accuracy.
 */
$x_sample = 1;
$y_sample = 1;

$last = 0;
for($i = 0; $i < $width; $i++) {
    $col = array();

    for($j = 0; $j < $height; $j++) {
        $col[$j] = imagecolorat($im, $i, $j);
    }

    if(($current = array_sum($col)) > 0) {
        if($last == 0) {
            $l = 0;
        }

        for($k = 0; $k < $height; $k++) {
            if(($l % $x_sample) == 0) {
                if(($k % $y_sample) == 0) {
                    $sample .= $col[$k];
                }
            }
        }

        $l++;
        $last = $current;
    } else {
        $last = 0;
    }

    if(!empty($sample) && $last == 0) {
        echo $sample . "\n";
        $sample = "";
    }
}
?>

Output (each line represents a character)

00000011111100000000000000001000000000000000000010000000000000000011111100000000
00000000011000000000000000001011000000000000000011010000000000000000010100000000
000000100001000000000000001111110000000000000000000100000000
000000100001000000000000001111110000000000000000000100000000
00000000011000000000000000001001000000000000000010010000000000000000011000000000
000000000001000000000000000001100000000000000000010000000000
00000011111100000000000000000110000000000000000001100000000000000011111100000000
00000000011000000000000000001001000000000000000010010000000000000000011000000000
00000000111100000000000000000100000000000000000010000000000000000000010000000000
000000100001000000000000001111110000000000000000000100000000
00000000011000000000000000001001000000000000000010100000000000000011111100000000
00000001110100000000

Using PHP and GD to add border to text

4. January 2010

2 Comments »

<?php
/**
 * Writes the given text with a border into the image using TrueType fonts.
 * @author John Ciacia
 * @param image An image resource
 * @param size The font size
 * @param angle The angle in degrees to rotate the text
 * @param x Upper left corner of the text
 * @param y Lower left corner of the text
 * @param textcolor This is the color of the main text
 * @param strokecolor This is the color of the text border
 * @param fontfile The path to the TrueType font you wish to use
 * @param text The text string in UTF-8 encoding
 * @param px Number of pixels the text border will be
 * @see http://us.php.net/manual/en/function.imagettftext.php
 */
function imagettfstroketext(&$image, $size, $angle, $x, $y, &$textcolor, &$strokecolor, $fontfile, $text, $px) {

    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);

   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);
}
<?php
$img = imagecreatefrompng("/home/john/Desktop/test.png");
$font_color = imagecolorallocate($img, 0, 0, 0);
$stroke_color = imagecolorallocate($img, 255, 0, 0);
imagettfstroketext($img, 10, 0, 10, 50, $font_color, $stroke_color, "abstract.ttf", "Hello, World!", 2);
?>

Basic CAPTCHA

4. January 2010

No Comments »

Very basic code demonstrating the use of the GD library by creating a simple CAPTCHA.

<?php
function randomString($length){
    $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    if(empty($length)){
        $length = "10";
    }

    for($i=0; $i<$length; $i++){
        $string .= $pattern{rand(0,61)};
    }

    return $string;
}

function captcha() {
    header("Content-type: image/png");
    $im = @imagecreate(100, 50) or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($im, 0, 0, 0);
    $text_color = imagecolorallocate($im, 255, 0, 0);
    $string = randomString();
    imagestring($im, 10, 5, 5,  $string, $text_color);
    imagepng($im);
    imagedestroy($im);
}

captcha();
?>

imagettftext only displays yellow text

9. May 2009

No Comments »

Lately I have been doing some work with the PHP GD library. I wanted to put text on an image so I naturally used imagettftext(). I started by copying the example code provided by the manual and intended on modifying the code to fit my needs. When I executed the code my my browser, the text was yellow. I took a look at the code, and the results did not match. The text should have been black. I tried adjusting the imagecolorallocate parameters to reflect a different color. However, this time with I executed the code, no text showed up. It turns out I needed configure PHP –with-freetype-dir. After a recompile with the new configuration it worked fine.

./configure \
–disable-magic-quotes \
–enable-bcmath \
–enable-sockets \
–with-mysq=/usr/local \
–with-apxs2=/usr/local/apache2/bin/apxs \
–with-curl \
–with-gd \
–with-freetype-dir=/usr
–with-png \
–with-jpeg \
–with-ttf

text1