Bravo List

Bravo List (http://www.bvlist.com/index.php)
-   TBDev (http://www.bvlist.com/forumdisplay.php?f=20)
-   -   External image proxy modifications (http://www.bvlist.com/showthread.php?t=11931)

Tedmorris 14th February 2019 12:08

External image proxy modifications
 
I'm trying to use an external image proxy server to load http images over https. For example, a user uses the image tag on the forums of http://images2.imagebam.com/2f/9d/32...1038500004.png
By adding https://images.weserv.nl/?url=ssl: to the start of the url and stripping the http:// the embeded link becomes https://images.weserv.nl/?url=ssl:im...1038500004.png but I'm struggling to get it working in globalstar..

This..

PHP Code:

[img]http://www/image.gif[/img]
$s preg_replace("/\[img\](http:\/\/[^\s'\"<>]+(\.gif|\.jpg|\.png))\[\/img\]/""<img border=0 src=\"\\1\">"$s); 

To this..

PHP Code:

[img]http://www/image.gif[/img]
$s preg_replace("/\[img\](http:\/\/[^\s'\"<>]+(\.gif|\.jpg|\.png))\[\/img\]/""<img border=0 src=\"\\https://images.weserv.nl/?url=ssl:\1\" >"$s); 

Is outputting https://images.weserv.nl/?url=ssl:" width="150" height="150" alt=""> I'm not getting the image url inserted and I know I need to strip the http:// from it but wanted to try atleast getting the http link added to the end of the image proxy link first. The src="\\1" appears to be the user inputted url but I've formatted it wrong in the modded version..

Any help would be great and much appreciated.[/QUOTE]

darkalchemy 14th February 2019 12:18

I think you are missing a back slash.
Code:

url=ssl:\1
Code:

url=ssl:\\1
There may be other mistakes, but this is the first thing I saw.

Tedmorris 14th February 2019 12:48

Thanks champ.

Now outputting
https://images.weserv.nl/?url=ssl:http://i68.tinypic.com/2qcm3x5.jpg" width="150" height="150" alt="">

Any idea how to strip the http:// from the image inserted via the [img] tag ?

darkalchemy 14th February 2019 12:56

Change:
Code:

(http:\/\/
To:
Code:

http:\/\/(

Tedmorris 14th February 2019 13:27

Sweet. Now outputting

https://images.weserv.nl/?url=ssl:images2.imagebam.com/2f/9d/32/b55ec21038500004.png">

From..
PHP Code:

$s preg_replace("/\[img\]http:\/\/([^\s'\"<>]+(\.(jpg|gif|png)))\[\/img\]/i""<a href=\"\\1\" target=\"_self\"><img border=\"0\" src=\"\\https://images.weserv.nl/?url=ssl:\\1\"></a>"$s); 

For some reason I'm guessing the >" at the end of the png is causing the problem as the image wont render and just outputs the above.

darkalchemy 14th February 2019 14:03

This is a bit difficult to do on my phone. But this should work.

Code:

$s = preg_replace("/\[img\]http:\/\/([^\s'\"<>]+(\.gif|\.jpg|\.png))\[\/img\]/", "", $s);
It seems that you changed your needs from your first post.

Code:

$s = preg_replace("/\[img\]http:\/\/([^\s'\"<>]+(\.(jpg|gif|png))\[\/img\]/i", "", $s);

Tedmorris 14th February 2019 14:37

Legend :ok:

PHP Code:

$s preg_replace("/\[img\]http:\/\/([^\s'\"<>]+(\.gif|\.jpg|\.png))\[\/img\]/""<img border=\"0\" src=\"https://images.weserv.nl/?url=ssl:\\1\">"$s); 

Works like a charm. Thank you so much. Now I just need to figure out how to apply it to rendered avatar images pulled from a users table row. Eventually I'd like to do as you have with the image proxy on the same server as the tracker in order to prevent being defendant on a 3rd party image proxy but one thing at a time I guess lol

Again many thanks m8. It's very appreciated. What does the "i" do that was removed from \[\/img\]/i",

I know why it changed from first post, I used an older version in my last post that I had commented out and didnt realise until you pointed it out sry, I'm guessing if it's got the \.gif|\.jpg|\.png etc and not just gif| jpg|png then the "i" is required

I'm thinking for the http avatars in forums, change this..

PHP Code:

if ($CURUSER["avatars"] == "yes")
 
$avatar htmlspecialchars($row["avatar"]);
        
print(
"<td align=center width=150 style='padding: 0px'><img width=150 src=$avatar></td>\n"); 

To this..

PHP Code:

if ($CURUSER["avatars"] == "yes")
 
$avatar preg_replace("/\http:\/\/([^\s'\"<>]+(\.gif|\.jpg|\.png))\/""<img border=\"0\" src=\"https://images.weserv.nl/?url=ssl:\\$users[avatar]\">"$avatar);
        
print(
"<td align=center width=150 style='padding: 0px'><img width=150 src=$avatar></td>\n"); 


darkalchemy 14th February 2019 17:21

Avatars are a bit more work, since they are requested all over.

They way that I handled it was to create a function that would replace the url with the proxied one. Then everywhere avatars are requested use the function. This also makes caching much easier.

Tedmorris 14th February 2019 18:27

Yeah it does seem a bit tricky, I've been playing around with it for the best part of 2 hours lol..is your function in the Pu-239 source?

I couldnt get the preg_replace to work, I'm still learning the metacharacters and how to use them properly. Havent played with the preg_replace function before.

I tested that it outputs with a variable added without preg_replace..

PHP Code:

$avatar $CURUSER["avatar"];
$avatar2 "https://images.weserv.nl/?url=ssl:$avatar";
  echo(
"<img src='$avatar2' />"); 

This works but the link outputs to https://images.weserv.nl/?url=ssl:ht...com/avatar.png so I need to strip the http:// from the variable first or try make preg_replace do it or try make it work with your custom function I guess.

I tried using
PHP Code:

$avatar2 preg_replace("/\http:\/\/([^\s'\"<>]+(\.gif|\.jpg|\.png))\/""<img border=\"0\" src=\"https://images.weserv.nl/?url=ssl:\\$avatar\">"$avatar2); 

But it doesn't work, I think I removed the img tags from it incorrectly.

PHP Code:

$avatar2 preg_replace("/\[img\]http:\/\/([^\s'\"<>]+(\.gif|\.jpg|\.png))\[\/img\]/""<img border=\"0\" src=\"https://images.weserv.nl/?url=ssl:\\$avatar\">"$avatar2); 


darkalchemy 14th February 2019 18:59

Just strip it out like
Code:

str_replace(['http://', 'https://'], $avatar)
My function is url_proxy, in bittorrent.php.

Code:

function url_proxy($url, $image = false, $width = null, $height = null, $quality = null)
{
    global $site_config;
    if (empty($url) || preg_match('#' . preg_quote($site_config['domain']) . '#', $url) || preg_match('#' . preg_quote($site_config['pic_baseurl']) . '#', $url)) {
        return $url;
    }
    if (!$image) {
        return (!empty($site_config['anonymizer_url']) ? $site_config['anonymizer_url'] : '') . $url;
    }
    if ($site_config['image_proxy']) {
        $image_proxy = new Pu239\ImageProxy();
        $image = $image_proxy->get_image($url, $width, $height, $quality);
        if (!$image) {
            return $site_config['pic_baseurl'] . 'noposter.png';
        } else {
            return $site_config['pic_baseurl'] . 'proxy/' . $image;
        }
    }
    return $url;
}


Tedmorris 14th February 2019 21:39

Thx m8, you have been a huge help. Much appreciated.

I'm not sure if I should try doing it with a function like your code or use the php function parse_url() to do all the work.

I'm guessing you proxy all images even if they are already https as I dont see anything that looks for a https match or specifys the function only for http images.

darkalchemy 14th February 2019 21:48

That's correct. I proxy all images. So I don't have to deal with image not found when the image finally gets deleted from the host.

Besides proxying all external images, my function also anonymizes non image urls. So every external url passes through that function.

Btw, "i' in regex/preg_match make the regex case-insensitive.


All times are GMT +2. The time now is 21:17.

Powered by vBulletin® Version 3.8.11 Beta 3
Copyright ©2000 - 2024, vBulletin Solutions Inc.