【问题标题】:PHP CORS Proxy for Canvas Images画布图像的 PHP CORS 代理
【发布时间】:2021-03-28 08:20:54
【问题描述】:

我需要一个本地 CORS 代理来从外部源加载图像。这些图像应该在画布对象中呈现。 为此,我使用https://github.com/softius/php-cross-domain-proxy的解决方案

但是没有加载图像但有一个空响应

这是应该在画布对象中呈现图像的部分

loadImages() {
    this.images.forEach((value, index) => {
        const image = {};
        
        image.img = new Image();
        image.img.crossOrigin = 'anonymous';

        image.img.src = 'https://cors-anywhere.herokuapp.com/' + value.packshot;
        //image.img.src = '/proxy.php?csurl=' + value.packshot;

        image.img.onload = (() => {
            // some code
        });
    });
},

这行得通 https://cors-anywhere.herokuapp.com/http://download.falk-ross.eu/ws/picture/163_06_051_f-2015-nc_01.jpg

这不起作用 /proxy.php?csurl=http://download.falk-ross.eu/ws/picture/163_06_051_f-2015-nc_01.jpg

有人知道建议吗?

【问题讨论】:

    标签: php canvas cors


    【解决方案1】:

    知道了 - 非常感谢这个伟大的社区。有时在提出问题时会出现解决方案

    <?php
    $url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
    $headers = ($_POST['headers']) ? $_POST['headers'] : $_GET['headers'];
    $mimeType = ($_POST['mimeType']) ? $_POST['mimeType'] : $_GET['mimeType'];
    $session = curl_init($url);
    
    if ($_POST['url']) {
        $postvars = '';
    
        while ($element = current($_POST)) {
            $postvars .= key($_POST) . '=' . $element . '&';
            next($_POST);
        }
    
        curl_setopt($session, CURLOPT_POST, true);
        curl_setopt($session, CURLOPT_POSTFIELDS, $postvars);
    }
    
    curl_setopt($session, CURLOPT_HEADER, $headers == 'true');
    curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($session);
    
    if ($mimeType != '') {
        header('Content-Type: ' . $mimeType);
    }
    
    echo $response;
    
    curl_close($session);
    

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2012-10-08
      • 2013-02-07
      • 1970-01-01
      • 2021-10-27
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多