Fun Image Textifier

Started on the old forum in October 2009 · view the original in the archive · old links redirect here

2 replies · 996 views · started 17 years ago · 1 min read

2 participants
WhiteWarrior Principal · 1,419 posts #1

Basically, you give it an image (PNG or JPG/JPEG is all I wrote support for), set some effects, and run the script. You then get a bunch of text.

You can use a glow effect, use random characters or one you specify, force "wobble removal" by using the "courier" font so characters are monospaced, or use your own custom text. Mix and match effects, try it out.

<html> <head> <title>Effect</title> <style> body {background-color: black; } </style> </head> <body>

<?php

/* You can change this stuff */ 
/*                           */ 
         
        $dpx = 9;                    // Pixel differential - The samller you make this, the larger the image (and the time it takes to make it) 
        $repl = '#';                // Character to use in the image. Using effects may alter this 
        $font = 'arial';            // Font to use for the text. Using effects may alter this 
        $image_name = 'image.png';    // Name of the picture to use 
         
        /* Adds a glow to the image, radiating from the center */ 
        $effect_glow = true; 
         
        /* Uses random characters to form the image */ 
        $effect_random_char = true; 
            /* The random characters you want to allow */ 
            $random_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=[];,.!@#$%^&*()_+|}{":?><~'; 
             
        /* When enabled, the font is set to what you chose above. This will enable variable character lenghts and create a weird-looking image */ 
        $effect_wobble = true; 
             
        /* Uses your text string to form the image */ 
        /* NOTE: THIS OVERRIDES EFFECT_RANDOM_CHAR */ 
        $effect_my_text = false;     
            /* The text string for your image */ 
            $my_text = 'I can place whatever text I want here and then it will be used to create the image. Cool, huh?'; 
 
/*                                */ 
/* Ok, that's it for the settings */ 
 
$ext = strtolower(substr(strrchr($image_name, '.'), 1)); 
 
switch($ext) { 
    case 'png': 
        $image = imagecreatefrompng($image_name); 
    break; 
     
    case 'jpg': 
    case 'jpeg': 
        $image = imagecreatefromjpeg($image_name); 
    break; 
} 
 
$DPY = $dpx * 2;     // When font is courier (evenly spaced), multiplier helps set image dimensions better. 
list($w, $h) = getimagesize($image_name); 
 
// Effect setup 
if($effect_glow) { 
    $center_x = $w/2; 
    $center_y = $h/2; 
    $max_distance = sqrt(pow((1-$center_x),2) + pow((1-$center_y),2)); 
} 
if(!$effect_wobble) { 
    $font = 'courier'; // Required so image dimensions are preserved 
} 
if($effect_my_text) { 
    $my_text_length = strlen($my_text); 
    $current_my_text_index = 0; 
} 
 
 
/*** GENERATE ***/ 
echo ''; 
// Then go down 
for($i = 0; $i < $h; $i += $DPY) { 
    // Go across first 
    for($j = 0; $j < $w; $j += $dpx) { 
        $rgb = getRGBArray($image, $j, $i); 
         
        if($effect_glow) { 
         
            // Find distance from center 
            $distance = sqrt(pow(($j-$center_x),2) + pow(($i-$center_y),2)); 
             
            // Determine how clearly to show this segment 
            $percent_shown = abs($distance/$max_distance) * 100; 
             
            // Determine the RGB loss 
            $loss = round(255 * $percent_shown/100); 
             
            // Keep loss within valid range due to rounding error 
            if($loss > 255) $loss = 255; 
            if($loss < 0) $loss = 0; 
             
            $rgb[0] -= $loss; if($rgb[0] < 0) $rgb[0] = 0; 
            $rgb[1] -= $loss; if($rgb[1] < 0) $rgb[1] = 0; 
            $rgb[2] -= $loss; if($rgb[2] < 0) $rgb[2] = 0; 
        } 
         
        if($effect_random_char) { 
            $repl = $random_chars[mt_rand(0, strlen($random_chars)-1)]; 
        } 
         
        if($effect_my_text) { 
            if($current_my_text_index > $my_text_length-1) $current_my_text_index = 0; // Wraparound 
            $repl = $my_text[$current_my_text_index]; 
            $current_my_text_index++; 
        } 
         
        $style = 'color: rgb(' . $rgb[0] . ',' . $rgb[1] . ',' . $rgb[2] . ')'; 
        echo '' . $repl . ''; 
    } 
    echo '

'; } echo '';

function getRGBArray($image, $x, $y) { 
    $rgb = imagecolorat($image, $x, $y); 
    $r = ($rgb >> 16) & 0xFF; 
    $g = ($rgb >> 8) & 0xFF; 
    $b = $rgb & 0xFF; 
    return array($r, $g, $b); 
} 

?>

</body> </html>
kei_ki7 Senior · 431 posts #2

..is this what you saying before ??

Did I help you? You can help me too Your donations will help me finance my studies.

WhiteWarrior Principal · 1,419 posts #3

Nope lol this is just a fun little code

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close