Vini App
Vini App

Reputation: 7485

Want to validate image raw data using php

I want to validate whether the image raw data is valid or not.

Below is my code :

private function __blobToImage($imagerawdata)
{       
    $imagedata = base64_decode($imagerawdata);
    // Set the content type header - in this case image/jpeg
    header('Content-Type: image/jpeg');

    $path = WWW_ROOT . "commapp_images".'/';
    $file = mktime().".png";
    $filepath = $path.$file;

    // Output the image     
    $image = imagecreatefromstring($imagedata); 
    ob_start(); 
    imagejpeg($image, $filepath, 80);
    ob_get_contents();
    ob_end_clean();
    return $file;
}

While using my code I'm getting the below error

"Notice (8): imagecreatefromstring() [function.imagecreatefromstring]: gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file"

Any one please help me as because I strucked here with out any solution.

Upvotes: 0

Views: 1603

Answers (1)

jeroen
jeroen

Reputation: 91792

imagecreatefromstring already does that, it returns false on failure. So you could suppress it's message and check for the return value:

$image = @imagecreatefromstring($imagedata);
if ($image !== false)
{
  ...
}

Edit: You don't need the header and output buffering if you are saving to a file and you are saving a jpeg with the png extension.

Upvotes: 1

Related Questions