tags:

views:

26

answers:

1

Hello, I've been trying to come up with a script using GD library to create an image and upload it straight to ImageShack using their API. I am using ImageShack class written by Elliott C. Back, which works flawlessly unmodified by uploading image from file. But when I try to modify one simple line of CURL Options code, it breaks and won't upload.

I have written a sample code that simply creates black image, store it into variable (as well as try to save it in local file - to verify if it won't become corrupted by storing it in variable or anything), and finally tries to upload to ImageShack. Code is commented so it should be rather easy to read. I hope you will be able to help me with this one, as I have already tried everything I could think of. Is it even possible to upload file using CURL from variable?

When I run this script I get bunch of errors, but all are either:

PHP Notice: Undefined offset: 1 in test.php on line 82

or

PHP Notice: Undefined offset: 2 in test.php on line 82

#!/usr/bin/php -q

<?php
// Create some basic image using PHP manual example for GD library
$img = imagecreatetruecolor(200, 200);

// Create new object from class ImageShack
$imageshack = new ImageShack;

// Store image to variable $image
ob_start();
ImagePNG($img);
$image = ob_get_clean();

// Upload image to ImageShack and print the url
$imageshack->upload($image);
$url = $imageshack->get_image_url();
print $url;

// And now let's try to save image from $image variable to local file to see if it's not corruped or anything
$tmpFile = "tmpFile.png";
$fh = fopen($tmpFile, 'w') or die("Can't open file");
fwrite($fh, $image);
fclose($fh);

// And finally destroy the image to free memory.
imagedestroy($img);

// Follows the slightly modified version of ImageShack class by Elliott C. Back
// only modified part is CURLOPT_POSTFIELDS part of upload() function and removed not required functions
// Class works flawlessly if you use it unmodified and upload from file, instead of variable.
class ImageShack
{
    var $is_url = "http://www.imageshack.us/index.php";
    var $is_result = false;
    var $is_result_parsed = false;

    public function upload( $file )
    {
        // send the image to ImageShack
        $ch = curl_init($this->is_url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 240);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'xml'=>'yes', 'fileupload'=>$file ));
  // curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'xml'=>'yes', 'fileupload'=>'@'.$file )); <== This is original line
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect: ' ));
        $this->is_result = curl_exec($ch);
        curl_close($ch);

        // Parse the result
        $this->parse();
    }

    public function get_image_url()
    {
        return $this->get( "image_link" );
    }

    private function get( $key )
    {
        if( !$this->is_result_parsed )
            return false;

        return( $this->is_result_parsed[ $key ] );
    }

    private function parse()
    {
        if (strpos($this->is_result, '<'.'?xml version=”1.0? encoding="iso-8859-1??>') === false)
            $this->is_result_parsed = false;

        $xmlData = explode("\n",$this->is_result);
        $xmlr = array();

        foreach($xmlData as $xmlDatum){
            $xmlDatum = trim($xmlDatum);

            if($xmlDatum != "" && !eregi("links",$xmlDatum) && !eregi("xml",$xmlDatum)){
                $xmlDatum = str_replace(">","<",$xmlDatum);
                list($xmlNull,$xmlName,$xmlValue) = explode("<",$xmlDatum);
                $xmlr[$xmlName] = $xmlValue;
            }
        }

        $this->is_result_parsed = $xmlr;
    }
}
?>
A: 

Some insight:

PHP Notice: Undefined offset: 1 in test.php on line 82 

means that the content is NULL. This usually occurs when using an Array such as $_POST['somevar']; when somevar does not exist. Are you providing the script with the data it needs.

Check that your variables actually contain data for each line number in the error message.

Todd Moses
I suspect this error is happening because Imageshack site returns page that is not expected - upload fails.
Andrew