Jump to content

Help with PHP GD? [SOLVED]


Recommended Posts

I am trying to write a script that creates an image the same size as a pre-existing image, but instead of just outputting the prexisting image, use text the color of individual pixels.

Here's the script:

<?php
header("Content-type: image/png");
header ("Content-type: image/png");
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");

$img = ImageCreateFromPng("av-835.png");
$width = imagesx($img);
$height = imagesy($img);
$im = imagecreate($width,$height);
$pos = 0;
for ($i = 1; $i <= $height; $i++) {
      for ($j = 1; $j <= $width; $j++){
             $rgb = ImageColorAt($img,$j,$i);
             $color = imagecolorsforindex($img,$rgb);
             $r = $color['red'];
             $g = $color['green'];
             $b = $color['blue'];
             $filename = "rabbittest2.txt";
             $handle = fopen($filename, "r");
                    $rabbit = fread($handle, filesize($filename));
             fclose($handle);
            $tmp = substr("$rabbit",$pos,1);
            $bg = imagecolorallocate($im, 0, 0, 0);
            $textcolor = imagecolorallocate($im,$r,$g,$b);
            $font='/usr/share/fonts/ttf/vera/VeraSe.ttf';
            imageTTFtext($im,10,0,$i,$j,$textcolor,$font,"$tmp");
            $pos++;    
            if ($pos >= 2000){ $pos = 0; } 
            }
       }
imagepng($im);
imagedestroy($im);
?>

 

The problem is, it doesn't add any text. Just creates a 160x160 box that has a flesh-colored background (probably one of the colors in my image). If I put any number of imageTTFtext lines right above imagepng($im), they work. Why is not using the ones inside my loop? Is this even possible? I know I'm going to have problems with my letters being more than one pixel wide and high, but that should not affect the outputting of the text inside my loop.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Ok, I changed it up a little bit and now I can see the text, but the text is still all one color:

 

<?php
header("Content-type: image/png");
header ("Content-type: image/png");

Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");

Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");

Header("Pragma: no-cache");

$img = ImageCreateFromPng("av-835.png");
$width = imagesx($img);
$height = imagesy($img);
//	echo "$width\n";
//echo "$height\n";
$im = imagecreate($width,$height);
$pos = 0;
for ($i = 1; $i <= $height; $i=$i+5) {
 for ($j = 1; $j <= $width; $j=$j+5){
      $rgb = ImageColorAt($img,$j,$i);
      $color = imagecolorsforindex($img,$rgb);
      $r = $color['red'];
      $g = $color['green'];
      $b = $color['blue'];
//       echo "$r $g $b\n";
        $filename = "rabbittest2.txt";
 $handle = fopen($filename, "r");
 $rabbit = fread($handle, filesize($filename));
 fclose($handle);
 $tmp = substr("$rabbit",$pos,1);
//  echo "$tmp\n";
 $bg = imagecolorallocate($im, 0, 0, 0);
        $textcolor['$pos'] = imagecolorallocate($im,$r,$g,$b);
 $font='/usr/share/fonts/ttf/vera/VeraSe.ttf';
 imageTTFtext($im,5,0,$j,$i,$textcolor['$pos'],$font,"$tmp");
        $pos++;    
  
 }
          
      }
imagepng($im);
imagedestroy($im);
?>

 

Edit: I'm assuming it is only using the last color or the first color. If I change the script up to not actually create a GD image, but to create acii art, it works properly. The right colors are used. So, somehow there's gotta be a way:

$textcolor['$pos'] = imagecolorallocate($im,$r,$g,$B);

imageTTFtext($im,5,0,$j,$i,$textcolor['$pos'],$font,"$tmp");

that'll use the right colors.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Here's the code so far:

<?php
Header ("Content-type: image/png");
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");

       $font='/usr/share/fonts/ttf/vera/VeraSe.ttf';
$img = ImageCreateFromPng("av-835.png");
$width = imagesx($img);
$height = imagesy($img);
       $im = imagecreatetruecolor($width,$height);
$pos = 0;
for ($i = 1; $i < $height; $i=$i+5) {
     for ($j = 1; $j < $width; $j=$j+5){
   $rgb = ImageColorAt($img,$j,$i);
   $color = imagecolorsforindex($img,$rgb);
   $r = $color['red'];
   $g = $color['green'];
   $b = $color['blue'];
                 $filename = "rabbittest2.txt";
            $handle = fopen($filename, "r");
            $rabbit = fread($handle, filesize($filename));
          fclose($handle);
          $tmp = substr("$rabbit",$pos,1);
          $bg = imagecolorallocate($im, 0, 0, 0);
                 ${textcolor . $pos} = imagecolorallocate($im,$r,$g,$b);
          imageTTFtext($im,4,0,$j,$i+4,${textcolor . $pos},$font,"$tmp");
          $pos++; 
          }
     }
     
imagepng($im);
imagedestroy($im);
?>

 

rabbittest2.txt is where I store the first 70-something thousand characters of the golden string. av-835.png is the name of the image in the same directory that I manipulate.

Licensed under the GPL if anyone cares. It's just a crappy script that's not even really finished. It's still got potential bugs since I haven't limited the size of the image and there may be possible buffer overflow problems. Someone asked me about it, so I figured I'd post my current working copy.

Here's an example of what it outputs:

post-835-1107221669_thumb.jpg

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Ok. Been playing with it and now I have 3 files. One is the upload form (upload.html) that let's a user upload an image, another processes the upload (upload.php) and then sends the image info to phpget.php. I think I've got all my security bases covered, but if you see flaws, please let me know.

 

upload.html

<html><body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="2200000" />
<!-- Name of input element determines name in $_FILES array -->
PNG Image to Convert: <input name="image" type="file" />
<input type="submit" value="Send File" />
</form></body></html>

 

upload.php

<html><body>
<?php
$uploaddir = '/var/www/html/uploads/';
$uploadfile = $uploaddir . basename($_FILES['image']['name']);
$picture = $_FILES['image']['name'];

       if ($picture == ""){
            echo "You tried to send a file with a space in the name, didn't you?!<br>";
            die('Windows user!');
           }
     
       if (!ereg('.png$', $picture)){
            echo "I said PNG and here you go trying to send $picture!<br>";
            die('Bad hacker!');
            }
    	 
       $uploaded_tmp_name = $_FILES['image']['tmp_name'];
       $size = getimagesize($uploaded_tmp_name);	

       if ($size['mime'] != "image/png") {
           echo 'You think you can change the extension and fool me?<br>';
           die('Stupid hacker!');
           }

if ($_FILES['image']['type'] != "image/png"){
          echo "You expect me to believe that is a PNG???";
          die('Really bad hacker!');
          }
      
$width = $size[0];
$height = $size[1];

       if ($height > 500){
            echo "That's $height pixels tall!<br>";
            die('What do you expect me to do with an image that huge? It has to be smaller than 500 pixels high!');
            }

       if ($width > 400){
            echo "That's $height pixels wide!<br>";
            die('What do you expect me to do with an image that huge? It has to be smaller than 400 pixels wide!');
            }
               
echo '<pre>';

       if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
           echo "File is valid, and was successfully uploaded.\n";
           } 
      else {
               echo "Possible file upload attack!\n";
               }
?>
<html><head><meta http-equiv="refresh" content="2;url=http://127.0.0.1/phpget.php?image=<?php echo $picture ?>"></head><body>Good! Just a second.</body></html>

 

phpget.php:

<?php
$picture = $_GET['image'];

       if (!ereg('.png$', $picture)){
           die('Bad hacker!');
           }
      
$img = ImageCreateFromPng("uploads/$picture");
$width = imagesx($img);
$height = imagesy($img);
       
       if ($height > 500){
           die('Too Big!');
           }

       if ($width > 400){
           die('Too Big!');
          }

Header ("Content-type: image/png");
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
// Stupid IE
Header("Pragma: no-cache");
       
       $font='/usr/share/fonts/ttf/vera/VeraSe.ttf';
       $im = imagecreatetruecolor($width,$height);
$pos = 0;
for ($i = 1; $i < $height; $i=$i+5) {
              for ($j = 1; $j < $width; $j=$j+5){
                     $rgb = ImageColorAt($img,$j,$i);
                     $color = imagecolorsforindex($img,$rgb);
                     $r = $color['red'];
                     $g = $color['green'];
                     $b = $color['blue'];
                     $filename = "rabbittest2.txt";
                         $handle = fopen($filename, "r");
                         $rabbit = fread($handle, filesize($filename));
                     fclose($handle);
                    $tmp = substr("$rabbit",$pos,1);
                    $bg = imagecolorallocate($im, 0, 0, 0);
                    ${textcolor . $pos} = imagecolorallocate($im,$r,$g,$b);
                    imageTTFtext($im,4,0,$j,$i+4,${textcolor . $pos},$font,"$tmp");
                    $pos++; 
                    }
             }

imagepng($im);
imagedestroy($im);
?>

 

Ideally, this works on photographs within the range of 150 x 250 and 400 x 500 since I skip every five pixels. The script could easily be broadened to accept images of a little bigger, but at 400 x 500, it is rather browser-intensive.

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...