niklasvh / html2canvas-proxy

Proxies for html2canvas
http://html2canvas.hertzen.com/
32 stars 15 forks source link

Support for redirected images #5

Open marcalj opened 10 years ago

marcalj commented 10 years ago

Example, show Facebook profile images:

https://graph.facebook.com/1415773021975267/picture becomes: https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash1/276836_1415773021975267_370639944_q.jpg

I'm looking at this, but hopefully I will use a hack in my code... Anyone had the same issue?

marcalj commented 10 years ago

Why this jsFiddle is working with not same origin images? http://jsfiddle.net/xKJB8/3/

niklasvh commented 10 years ago

It isn't.

"Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': tainted canvases may not be exported."

marcalj commented 10 years ago

It works without errors at the first time, after opening the console and refresh full page doesn't work... strange.

niklasvh commented 10 years ago

If the image isn't in cache, it won't be drawn and as such the toDataURL just returns a blank canvas in base64. The image should be drawn only after it has been loaded

marcalj commented 10 years ago

I've solved the issue using file_get_contents. This is the important change (quick&dirty code)

        $image = file_get_contents($_GET['url']);

        if (false === $image) {
            $fp = false;
        } else {
            fwrite($sf, $image);

            $finfo = new finfo(FILEINFO_MIME);
            $mime = $finfo->buffer($image) . PHP_EOL;

            $mime = preg_split('/;+/', $mime, -1, PREG_SPLIT_NO_EMPTY);

            if (is_array($mime)) {
                $mime = trim($mime[0]);
            }

            $allowMimes = Array(
                'image/jpeg', 'image/jpg', 'image/png', 'image/gif',
                'text/html', 'application/xhtml', 'application/xhtml+xml'
            );

            if (!in_array($mime, $allowMimes)) {
                $err = $mime . ' mime is invalid';
                $mime = false;
            }

            $fp = true;
        }
        fclose($sf);
marcalj commented 10 years ago

Sorry, wrong repo!!!!! :dancer: :S