Ok I am going to put the entire class here for review... I am not the
original author, simply trying to get it to work because I think its pretty
neat:  Here is the file, asciiartist.php:
If you scroll down the page I have commented where the error occurs using
***** ERRORS *****
<?php
/**
* ASCIIArtist - Class to convert Bitmap-Images into nice ASCII Texts in HTML
format
*
* Copyright 2001 by Sebastian Röbke ([EMAIL PROTECTED])
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version. This program is distributed in the hope that it will be
useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* Credits: Florian Schäfer, Maxi Kellner, Andrea Spacca
* Requirements: PHP with GD support for the desired image format
*
* @author   Sebastian Röbke <[EMAIL PROTECTED]>
* @version 1.1 2001-12-31 18:41 CET
* @link     http://www.sebastian-r.de/asciiart/index.php
* @access   public
*/

class ASCIIArtist
{
    /**
    * The replace characters from dark to light used by render modes 0,1
    * (current version can handle 9 variations)
    *
    * @var      array
    * @access   public
    */
    var $replace_characters = array (
        1 => "W",
        2 => "@",
        3 => "#",
        4 => "*",
        5 => "+",
        6 => ":",
        7 => ".",
        8 => "&rsquo;",
        9 => "&nbsp;"
    );

    /**
    * Possible image types
    *
    * @var      array
    * @access   public
    */
    var $image_types = array (
        1 => "GIF",
        2 => "JPEG",
        3 => "PNG"
    );

    /**
    * Image file handler
    *
    * @var      string
    * @access   public
    */
    var $image = NULL;

    /**
    * Image file height
    *
    * @var      integer
    * @access   public
    */
    var $image_height = 0;

    /**
    * Image file height
    *
    * @var      integer
    * @access   public
    */
    var $image_width = 0;

    /**
    * Container for the rendered HTML/ASCII image
    *
    * @var      string
    * @access   public
    */
    var $image_html = NULL;

    /**
    * CSS for the HTML Image Output
    *
    * @var      string
    * @access   public
    */
    var $image_css = "
        color           : #000000;
        background-color: #FFFFFF;
        font-size       : 8px;
        font-family     : \"Courier New\", Courier, mono;
        line-height     : 5px;
        letter-spacing  : -1px;
    ";

    /**
    * Formatting the HTML Image using CSS
    *
    * Tip: Use width-fixed fonts such as Courier only.
    *
    * @param    string    $css    CSS Content
    * @access   public
    * @see      $image_css
    */
    function setImageCSS ($css)
    {
        $this->image_css = $css;
    }

    /**
    * Returns the hex string of a rgb array
    *
    * Example:
    * $rbg = array("red" -> 255, "green" -> 255, "blue" -> 255);
    * rgb2hex($rgb) will return "FFFFFF"
    *
    * @param    array   $rgb    An array of red, green and blue values
    * @return   string  The hex values as one string
    * @access   private
    */
    function _rgb2hex($rgb)
    {
        return
sprintf("%02X%02X%02X",$rgb["red"],$rgb["green"],$rgb["blue"]);
    } // end func rgb2hex

    /**
    * Renders the image into HTML
    *
    * The following modes are implemented:
    * 1 = black/white using $replaceCharacters by brightness,
    * 2 = colourized using $replaceCharacters by brightness,
    * 3 = colourized using a fixed character definded by $fixedChar.
    * A resolution of 1 means that every pixel is being replaced,
    * whereas 5 for example means a scanned block of 5 pixel height and
width,
    * resulting in less data to replace.
    *
    * @param    integer $mode       Current version can handle mode 1, 2 or
3
    * @param    integer $resolution Resolution for scanning the bitmap.
    * @param    string  $fixed_char Needed for mode 3
    * @param    boolean $flip_h     Flip output horizontally?
    * @param    boolean $flip_v     Flip output vertically?
    * @access   public
    * @see  getHTMLImage(), $replace_characters
    */
    function renderHTMLImage($mode = 1, $resolution = 2, $fixed_char = "@",
$flip_h = false, $flip_v = false)
    {
        // Minimum value for $resolution is 1
        if ($resolution < 1) {
            $resolution = 1;
        }

        // Different loops for flipping
        // (btw.: Can someone give me a hint how to implement
        // dynamic operators or the like to get rid of those endless cases?)
        if (!$flip_h && !$flip_v) {
            // Y-Axis
            for ($y = 0; $y < $this->image_height; $y += $resolution)
            {
                // X-Axis
                for ($x = 0; $x < $this->image_width; $x += $resolution)
                {
                    $this->renderPixel($mode, $x, $y, $fixed_char);
                }
                $this->image_html .= "<br>\n";
            }
        }
        else if ($flip_h && !$flip_v) {
            // Y-Axis
            for ($y = 0; $y < $this->image_height; $y += $resolution)
            {
                // X-Axis
                for ($x = $this->image_width; $x > 0; $x -= $resolution)
                {
                    $this->renderPixel($mode, $x, $y, $fixed_char);
                }
                $this->image_html .= "<br>\n";
            }
        }
        else if (!$flip_h && $flip_v) {
            // Y-Axis
            for ($y = $this->image_height; $y > 0; $y -= $resolution)
            {
                // X-Axis
                for ($x = 0; $x < $this->image_width; $x += $resolution)
                {
                    $this->renderPixel($mode, $x, $y, $fixed_char);
                }
                $this->image_html .= "<br>\n";
            }
        }
        else if ($flip_h && $flip_v) {
            // Y-Axis
            for ($y = $this->image_height; $y > 0; $y -= $resolution)
            {
                // X-Axis
                for ($x = $this->image_width; $x > 0; $x -= $resolution)
                {
                    $this->renderPixel($mode, $x, $y, $fixed_char);
                }
                $this->image_html .= "<br>\n";
            }
        }
    } // end func renderHTMLImage

    /**
    * Renders the current pixel
    *
    * @param    integer $mode       Current version can handle mode 1, 2 or
3
    * @param    integer $x          X Position of the image
    * @param    integer $y          Y Position of the image
    * @param    string  $fixed_char Needed for mode 3
    * @access   public
    * @see  renderHTMLImage(), $image_html
    */
    function renderPixel($mode, $x, $y, $fixed_char)
    {
        // RGB Value of current pixel (Array)
        $rgb = imagecolorsforindex($this->image, imagecolorat($this->image,
$x, $y));

        // Replace by mode
        switch ($mode) {
            case 1:
                // Rounded Brightness
                $brightness = $rgb["red"] + $rgb["green"] + $rgb["blue"];

                // Choose replacing character
                $replace_character_id = round($brightness / 100) + 1;

                $this->image_html .=
$this->replace_characters[$replace_character_id];
                break;
            case 2:
                // Rounded Brightness
                $brightness = $rgb["red"] + $rgb["green"] + $rgb["blue"];

                // Choose replacing character
                $replace_character_id = round($brightness / 100) + 1;

                $this->image_html .= "<font
color=\"#".$this->_rgb2hex($rgb)."\">".$this->replace_characters[$replace_ch
aracter_id]."</font>";
                break;
            case 3:
                $this->image_html .= "<font
color=\"#".$this->_rgb2hex($rgb)."\">".$fixed_char."</font>";
                break;
        }
    } // end func renderPixel

    /**
    * Returns the rendered HTML image and CSS
    *
    * @return   string  The rendered HTML image and CSS
    * @access   public
    */
    function getHTMLImage()
    {
        if ($this->image_html) {
            return "<style type=\"text/css\">"
                   .".asciiimage{"
                   .$this->image_css
                   ."}</style>"
                   ."<span class=\"asciiimage\">"
                   .$this->image_html
                   ."</span>";
        } else {
            return "getHTMLImage(): No content to return. Please be sure to
call renderHTMLImage() first.";
        }
    } // end func getHTMLImage

    /**
    * Loads the bitmap image and sets file handler, width and height
properties
    *
    * If filename begins with "http://"; (not case sensitive), an HTTP 1.0
connection
    * is opened to the specified server, the page is requested using the
HTTP GET method.
    * If filename begins with "ftp://"; (not case sensitive), an ftp
connection to the
    * specified server is opened.
    * If the server does not support passive mode ftp, this will fail.
    * If filename is one of "php://stdin", "php://stdout", or
"php://stderr",
    * the corresponding stdio stream will be opened.
    * If filename begins with anything else, the file will be opened from
the filesystem.
    *
    * @param    string  $image  Filename of the bitmap Image
    * @access   public
    * @see  $image, $image_height, $image_width
    */
    function ASCIIArtist($image)
    {
        if ($input = @fopen($image, "rb")) {
      $image_data = fread ($input, 2048576);
      fclose($input);
      $tmp_image = tmpfile();
      $output = fopen($tmp_image, "wb"); // ***** ERRORS ***** this is where
it begins
      fwrite($output, $image_data);
      fclose($output);
        } else {
            exit("Cannot access ".$image." for reading. (Does not exist or
has wrong permissions)");
        }

        // Create Image from file by type, get and set size
        list($width,$height,$type) = getimagesize($tmp_image);
        switch ($type) {
            case 1:
            case 2:
            case 3:
                eval("
                    if (!\$this->image =
@imagecreatefrom".$this->image_types[$type]."(\"".$tmp_image."\"))
                    exit(\"This PHP version cannot create image from
".$this->image_types[$type]."\");
                ");
                unlink($tmp_image);
                $this->image_height = $height;
                $this->image_width  = $width;
                break;
            default:
                exit("ASCIIArtist(): Cannot determine image type of
".$image.".");
        }
    } // end constructor
}
?>
I am not quite sure how I can resolve this as is... It is not that important
to get working but I think it would be neat to see and test out a bit.  If
there is a way to do the same thing without using a temp file that might
work in my case.
Jas

"1lt John W. Holmes" <[EMAIL PROTECTED]> wrote in message
012d01c201d3$17a91780$2f7e3393@TB447CCO3">news:012d01c201d3$17a91780$2f7e3393@TB447CCO3...
> You're not even trying to open a file. You're trying to open a Resource,
> which makes me think that fopen() is being called twice or something. You
> obviously have something very wrong.
>
> Can you show the code around these lines where you're trying to open the
> file? All of the errors are because of the first one.
>
> ---John Holmes...
>
> > -----Original Message-----
> > From: Jas [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, May 22, 2002 4:18 PM
> > To: [EMAIL PROTECTED]
> > Subject: [PHP] tmpfile() errors?
> >
> >
> > Here are the errors I am recieving:
> > Warning: fopen("Resource id #4","wb") - Permission denied in
> > /path/to/asciiartist.php on line 295
> >
> > Warning: Supplied argument is not a valid File-Handle resource in
> > /path/to/asciiartist.php on line 296
> >
> > Warning: Supplied argument is not a valid File-Handle resource in
> > /path/to/asciiartist.php on line 297
> >
> > Warning: getimagesize: Unable to open 'Resource id #4' for reading. in
> > /path/to/asciiartist.php on line 303
> > ASCIIArtist(): Cannot determine image type of shared/image02.jpg.
> >
> > I have checked the file permissions for the files and directory in
> question
> > and they are all set to read & write, so I know this is not the problem.
> > Here is the function that I am trying to get to work.
> >
> > function ASCIIArtist($image)
> >     {
> >         if ($input = @fopen($image, "rb")) {
> >       $image_data = fread ($input, 2048576);
> >       fclose($input);
> >       $tmp_image = tmpfile();
> >       $output = fopen($tmp_image, "wb");
> >       fwrite($output, $image_data);
> >       fclose($output);
> >         } else {
> >             exit("Cannot access ".$image." for reading. (Does not exist
or
> > has wrong permissions)");
> >         }
> >
> > This code is from http://www.sebastian-r.de/.  And everything works fine
> > until it hits this function. Could I get some enlightenment on why it
> cannot
> > create a temp file named $tmp_image?  (ecspecially when the permissions
> for
> > the file are correct?)  Thanks in advance,
> > Jas
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to