PHP create image from application/octet stream

13,525

Easy :)

$image = imagecreatefromstring( $data );

Specifically:

$data = file_get_contents($_FILES['myphoto']['tmp_name']);
$image = imagecreatefromstring( $data );
Share:
13,525
johnnietheblack
Author by

johnnietheblack

im a manic creatician

Updated on June 05, 2022

Comments

  • johnnietheblack
    johnnietheblack almost 2 years

    My application is having images sent to it from mobile devices with the content-type "application/octet-stream".

    I need to process these images using the GD library, which means I need to be able to create an image object from the data.

    Typically, I have been using imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, etc, to handle files uploaded from web forms, but these don't seem to work when coming to me as application/octet-stream.

    Any ideas on how I can achieve my goal?

    EDIT

    Here is the code I use to create the image identifier...my handler works perfectly throughout my site, the only difference I can tell between my site and the data from the iOS is the content-type

    public function open_image($path) {
            # JPEG:
            $im = @imagecreatefromjpeg($path);
            if ($im !== false) { $this->image = $im; return $im; }
    
            # GIF:
            $im = @imagecreatefromgif($path);
            if ($im !== false) { $this->image = $im; return $im; }
    
            # PNG:
            $im = @imagecreatefrompng($path);
            if ($im !== false) { $this->image = $im; return $im; }
    
            $this->error_messages[] = "Please make sure the image is a jpeg, a png, or a gif.";
            return false;
        }
    
  • johnnietheblack
    johnnietheblack almost 13 years
    HEYAAA, is the $data simply to be found as $_FILES['myphoto']['tmp_name']?
  • Christian
    Christian almost 13 years
    @johnnietheblack - Not exactly, but near enough: $image = imagecreatefromstring( file_get_contents($_FILES['myphoto']['tmp_name']) );
  • johnnietheblack
    johnnietheblack almost 13 years
    ahh, that makes sense...so the tmp file is basically just a "text file" with the string inside? (im obviously new to this aspect of data handling)
  • Christian
    Christian almost 13 years
    @johnnietheblack - The tmp file is probably binary (unless the uploaded image is an SVG), and file_get_contents() just gets the data. The 'tmp_file' part just contains the file name as stored on the server. You can see this by using: echo '<pre>'.print_r($_FILES,true).'</pre>; <- by the way, that's a very useful snippet for inspecting PHP variables.