PicoFeed\Filter\Attribute::rewriteImageProxyUrl PHP Method

rewriteImageProxyUrl() public method

Rewrite image url to use with a proxy.
public rewriteImageProxyUrl ( string $tag, string $attribute, string &$value ) : boolean
$tag string Tag name
$attribute string Attribute name
$value string Attribute value
return boolean
    public function rewriteImageProxyUrl($tag, $attribute, &$value)
    {
        if ($tag === 'img' && $attribute === 'src' && !($this->image_proxy_limit_protocol !== '' && stripos($value, $this->image_proxy_limit_protocol . ':') !== 0)) {
            if ($this->image_proxy_url) {
                $value = sprintf($this->image_proxy_url, rawurlencode($value));
            } elseif (is_callable($this->image_proxy_callback)) {
                $value = call_user_func($this->image_proxy_callback, $value);
            }
        }
        return true;
    }

Usage Example

Exemplo n.º 1
0
 public function testRewriteProxyImageUrl()
 {
     $filter = new Attribute(new Url('http://www.la-grange.net'));
     $url = '/2014/08/03/4668-noisettes';
     $this->assertTrue($filter->rewriteImageProxyUrl('a', 'href', $url));
     $this->assertEquals('/2014/08/03/4668-noisettes', $url);
     $filter = new Attribute(new Url('http://www.la-grange.net'));
     $url = '/2014/08/03/4668-noisettes';
     $this->assertTrue($filter->rewriteImageProxyUrl('img', 'alt', $url));
     $this->assertEquals('/2014/08/03/4668-noisettes', $url);
     $filter = new Attribute(new Url('http://www.la-grange.net'));
     $url = '/2014/08/03/4668-noisettes';
     $this->assertTrue($filter->rewriteImageProxyUrl('img', 'src', $url));
     $this->assertEquals('/2014/08/03/4668-noisettes', $url);
     $filter = new Attribute(new Url('http://www.la-grange.net'));
     $filter->setImageProxyUrl('https://myproxy/?u=%s');
     $url = 'http://example.net/image.png';
     $this->assertTrue($filter->rewriteImageProxyUrl('img', 'src', $url));
     $this->assertEquals('https://myproxy/?u=' . rawurlencode('http://example.net/image.png'), $url);
     $filter = new Attribute(new Url('http://www.la-grange.net'));
     $filter->setImageProxyCallback(function ($image_url) {
         $key = hash_hmac('sha1', $image_url, 'secret');
         return 'https://mypublicproxy/' . $key . '/' . rawurlencode($image_url);
     });
     $url = 'http://example.net/image.png';
     $this->assertTrue($filter->rewriteImageProxyUrl('img', 'src', $url));
     $this->assertEquals('https://mypublicproxy/d9701029b054f6e178ef88fcd3c789365e52a26d/' . rawurlencode('http://example.net/image.png'), $url);
 }