itai
itai

Reputation: 302

php - get data from uri image

I'm using a JavaScript client-side image uploader and editor that posts to the server via URI image.

Example code:

$_POST['image_data']="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO4AAAA6CAYAAAC........"

The image generated is small (20kb-30kb), and I save it directly to the database rather than in a file (since I also embedded it directly in the CSS later).

How can I validate the posted data so that it is not too big in size and dimensions before I add it to the database? I also need to check the data URI so that the image is valid with the exact dimensions (eg: 100x100) otherwise display an error to the user.

To clarify, I am having the image in a post variable, and without saving it at all to a file.

Upvotes: 0

Views: 470

Answers (2)

Daniel Lichtenberg
Daniel Lichtenberg

Reputation: 74

Second solution, if you can't write anything to disk

$_POST["image_data"] = "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7";

function check_uri_image($data, $width=100, $height=100, $mime="image/png"){
    $success = false;

    if(preg_match("#^data:(image/(gif|png|jpeg));base64,(.*)$#mi", $data, $matches)){
        if($img = imagecreatefromstring(base64_decode($matches[3]))){
            if((imagesx($img) == $width) && (imagesy($img) == $height)){
                if(strtolower($matches[1]) == $mime){
                    $success = true;
                    }
                }

            imagedestroy($img);
            }
        }

    return $success;
    }

var_dump(check_uri_image($_POST["image_data"], 16, 16, "image/gif"));

Upvotes: 1

Daniel Lichtenberg
Daniel Lichtenberg

Reputation: 74

Please try:

$_POST["image_data"] = "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7";

function check_uri_image($data, $width=100, $height=100, $mime="image/png"){
    $success = false;

    if(preg_match("#^data:image/(gif|png|jpeg);base64,(.*)$#mi", $data, $matches)){
        if($tmp = tempnam(sys_get_temp_dir(), "datauri.")){
            if($fh = fopen($tmp, "wb")){
                fwrite($fh, base64_decode($matches[2]));
                fclose($fh);

                $info = getimagesize($tmp);

                if(is_array($info)){
                    if(($info[0] == $width) && ($info[1] == $height) && ($info["mime"] == $mime)){
                        $success = true;
                        }
                    }

                unlink($tmp);
                }           
            }
        }

    return $success;
    }

var_dump(check_uri_image($_POST["image_data"], 16, 16, "image/gif"));

?>

Upvotes: 0

Related Questions