brcontainer / html2canvas-php-proxy

PHP Proxy html2canvas
MIT License
134 stars 77 forks source link

Support for redirected images #3

Closed marcalj closed 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

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);
brcontainer commented 10 years ago

When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a close_notify indicator. PHP will report this as

SSL: Fatal Protocol Error

when you reach the end of the data. To work around this, the value of error_reporting should be lowered to a level that does not include warnings. PHP 4.3.7 and higher can detect buggy IIS server software when you open the stream using the https:// wrapper and will suppress the warning. When using fsockopen() to create an ssl:// socket, the developer is responsible for detecting and suppressing this warning.

Another problem is that file_get_contents requires allow_url_fopen = 1

Is why I prefer to fsockopen(), so the whole responsibility goes to the developer (in other words I am responsible for handling the socket). I am doing a new update of the code, on Sunday I will update the repository.

brcontainer commented 10 years ago

Fully upgrade https://github.com/brcontainer/html2canvas-php-proxy/commit/b5936d88b7826ba322c46c8d6bcd6395a4bf39a1 version 0.1.0

marcalj commented 10 years ago

Cool! Thanks!

brcontainer commented 10 years ago

Thank you for showing me the problem!

marcalj commented 10 years ago

:+1: :)