The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Best way to load image from another domain

Discussion in 'Workarounds and Optimization' started by patchwork, Oct 3, 2012.

  1. patchwork

    patchwork Well-Known Member

    Joined:
    Nov 2, 2001
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    16
    Hi

    I've been thinking about loading all my sites image files from a different domain on another server to save CPU and Memory on my current server

    Currently my sites image files are located in mydomain/images

    I know I could go through all of my sites php/html and edit the code so all the images are fetched from a different location, but I wondered if there was a better way of doing it.

    What would be the easiest way to do it, and most server friendly way.

    DNS /.htaccess / cpanel redirect / etc...

    Thanks
    Pete
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Ideally, you should save and upload the images to the account on the local server. Otherwise, you will have to modify the code in the script itself to use the full URL to the remote image.

    Thank you.
     
  3. srpurdy

    srpurdy Well-Known Member

    Joined:
    Jun 1, 2011
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    actually it's always good to use mutiple urls whenever possible even it the images will still be local. Because of the limitations of browsers and how many files they can load in parallel. It's actually a really good way to gain performance out of the site although it will not help your load issue. You could use a CDN though, but like Michael mentions you will need custom urls for the files you want to load remotely. So you really just need to edit all your scripts.

    Although some CMS systems give you options for CDN's and what not like wordpress. Atleast they're is plugins for this.

    Also multiple urls should be used with care, because of the speed up time of TCP and DNS lookup's but it's used a lot on big sites with a lot of resources.
     
    #3 srpurdy, Oct 6, 2012
    Last edited: Oct 6, 2012
  4. patchwork

    patchwork Well-Known Member

    Joined:
    Nov 2, 2001
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    16
    Hi

    It would have been nice to find a way to just automatically switch the location of the images directory, so it would load the images from a remote server most of the time and switch back to the local server if the remote server was down or getting too stressed.

    At the moment I have opted for a hard coded option, its a bit of a pain because I have to edit all my pages, and it also means the images do not load if I'm editing the page in a HTML editor such as Dreamweaver.

    The code below pings a remote server to make sure its up and not too stressed, if it responds pretty quickly then I use the remote images, and if it fails or if its running very slow then I auto switch back to the local images directory.


    Code:
    function ping($host, $port, $timeout)
    {
    $tB = microtime(true);
    $fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
    if (!$fP) { return "100"; }
    $tA = microtime(true);
    return round((($tA - $tB) * 1000), 0);
    }
    
    $static_images = "";
    if(ping("123.123.123.123", 80, 3) < 5) $static_images = "http://123.123.123.123/";
    
    My HTML then looks like this
    Code:
    <img src="<? echo $static_images ?>images/logo.gif" />
    
    Pete
     
  5. srpurdy

    srpurdy Well-Known Member

    Joined:
    Jun 1, 2011
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    I personally don't think this is a good solution there is a delay time for the fsockopen request which in itself can take longer than it does to load the image, and you still need to do the actual http request after that. Which is another delay. This I would think would have performance issues on your site.

    If your going through all the trouble of coding a custom solution like that. You should just have a switch to turn the feature on or off, and maybe base the feature around a time of day when your server will be most loaded. So it automatically turns on and uses your special address when it's during peak load periods and switches back during a night time cycle. This way you don't need to query the server at all.

    You might have more luck using a CDN though. Like Cloudflare or something like that. Will take a huge amount of load off your server.

    I have 1 site with around 11 million requests per month 7 million of which cloudflare deals with. :)

    Great thing about cloudflare is you won't have to change any urls. :P
     
    #5 srpurdy, Oct 6, 2012
    Last edited: Oct 6, 2012
  6. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
    Just so we have a link to the CloudFlare CDN:

    https://www.cloudflare.com/features-cdn

    They appear to take your content and cache it, then provide it on servers closer to the user who is requesting the page unless I'm misunderstanding how it works (which is possible).

    Of note, another option would be to have two servers that you use for the exact same website for all content and pages, then to use lsyncd to sync the site content from server A to server B when it changes. For both sites to be used, you'd then have to setup a load balancer such as one of those discussed here:

    Looking for open-source load-balancer software to run on Linux

    This would presume you have full control of both machines to where you can setup this type of configuration.
     
  7. patchwork

    patchwork Well-Known Member

    Joined:
    Nov 2, 2001
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    16
    Hi

    Thanks srpurdy :)

    I've now been using cloudflare for just over 24 hours on one of my smaller domains and so far I'm really impressed.

    Its handling all my images, javascript, and css and so far its reduced the server requests to that site by over 50%

    Its still early days but I hope to get my bigger sites going through it :)

    Pete :)
     
  8. Monitorscout

    Monitorscout Registered

    Joined:
    Oct 19, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    DataCenter Provider
    loading from different servers will work locally, not when running from a server. You'll need crossdomain.xml policy files on the servers containing the items you're loading.
    and all you need to do is setup your loader context so it loads checks for policy files by default.

    Here's a snippet:

    var loaderContext:LoaderContext = new LoaderContext(true);
    var loader:Loader = new Loader();
    loader.load(new URLRequest('http://someserver.com/yourImage.jpg'),loaderContext);

    Otherwise, you might need to use some server side language to load the image and write it on you domain. After that is done, you should be able to do all the manipulation you need.
     
Loading...

Share This Page