319: Using a Subdomain to store your images in OpenCart 1.5

NOTE: This article will only work for Opencart versions prior to 1.5.5.1. It does not work for versions 1.5.5.1 or newer.

Google Page Speed recommends using a subdomain to store images that are loaded into your OpenCart store. When a web page tries to load images, scripts, and content they load one at a time. If you have a lot of images, it could dramatically impact your page load speeds. By having the images in a subdomain, they can load at the same time as the site does.

First, you’ll need to create a subdomain for the images in cPanel. During the set up of the subdomain  you’ll need to set the document root for the subdomain to the current folder where your images are stored. In this example, we set up the subdomain to images.inmotiontesting.com and pointed it to the existing image folder in the root folder of my OpenCart website.

Now, you have to edit the config.php file in the root of OpenCart as well as the config.php file in the admin folder:

Change:

define('HTTP_IMAGE', 'http://inmotiontesting.com/image/');

To:

define('HTTP_IMAGE', 'http://images.inmotiontesting.com/'); 

If you are using HTTPS, you’ll want to change the HTTP settings as well:

Change:

define('HTTPS_IMAGE', 'http://opencart.inmotiontesting.com/image/');

To:

define('HTTPS_IMAGE', 'http://images.inmotiontesting.com/');

If you want to test to make sure your changes have successfully worked, after you have made the changes above load your OpenCart site in a web browser. Then look at the source code to make sure all the image references are pointing to the subdomain. Here’s what our new source code looks like:

<div id="logo">
   <a href="http://opencart.inmotiontesting.com/index.php?route=common/home">
     <img src="https://images.inmotiontesting.com/data/head_logo.gif" title="InMotion Hosting OpenCart Demo" alt="InMotion Hosting OpenCart Demo "/>
   </a>
</div>

If you need further assistance please feel free to ask a question on our support center.

Thoughts on “319: Using a Subdomain to store your images in OpenCart 1.5

  • Hello,

    i have an opencart site running and I need to implement this in order to beat the speed test. DO you guys have  solution for opencart 2.x

     

    Thanks

  • Hi,

    I did as below said:-

    New OpenCart Versions Image Structure: catalog/model/tool/image.php Replace this: return $this->config->get('config_ssl') . 'image/' . $new_image; To This: return 'https://images.yourdomain.com/' . $new_image; ANDReplace this: return $this->config->get('config_url') . 'image/' . $new_image; To This: return 'http://images.yourdomain.com/' . $new_image;

     

    But after doing this and when I checked http://www.webpagetest.org/result/150710_2K_7X2/ URL I got “Cache static content” as “F” grade before this it used to “A”.

    My image domain directory root is as below:-

    dev domain name is dev.domainname.com

    image domain name is imagedev.domainname.com

    and webroot for the dev is /var/www/dev.domainname.com/htdocs

    and webroot for the image is /var/www/dev.domainname.com/htdocs/image

    I have already .htaccess in the htdocs folder which is doing very thing for the better peformance and redirection, but to improve the images load I created image domain and images are getting loaded based on the above PHP code change. But now got “F” grade which mean image caching is not working.

    Do I need to place .htaccess also in htdocs/image folder?

    If so what information should I place in the .htaccess to have better caching and better performance?

    Your impormation is very valuded to me.

    Thank you,

    Sudhakar

     

    • Hello Sudhakar,

      Thank you for contacting us. The above guide is only on changing where your images are stored for your OpenCart site, and does not address any caching in the CMS.

      I recommend posting your question in the official OpenCart forums, since it is frequented by OC developers.

      Thank you,
      John-Paul

  • Hello hi All ,

    Am new for opencart. i was created subdomain is http://img.miimera.com.

    Here in my config file i was written this code.

    when i was open my site and inspect elements am not getting image from subdomain. my opencart version is 1.5.6.4. please help me for this.

    Thanks

    define(‘HTTP_SERVER’, ‘http://www.miimera.com/’);

    define(‘HTTP_IMAGE’, ‘http://img.miimera.com/’);

     

     

     

    // DIR

    define(‘DIR_APPLICATION’, ‘/home/miimera/public_html/catalog/’);

    define(‘DIR_SYSTEM’, ‘/home/miimera/public_html/system/’);

    define(‘DIR_DATABASE’, ‘/home/miimera/public_html/system/database/’);

    define(‘DIR_LANGUAGE’, ‘/home/miimera/public_html/catalog/language/’);

    define(‘DIR_TEMPLATE’, ‘/home/miimera/public_html/catalog/view/theme/’);

    define(‘DIR_CONFIG’, ‘/home/miimera/public_html/system/config/’);

    define(‘DIR_IMAGE’, ‘/home/miimera/public_html/img/’);

    define(‘DIR_CACHE’, ‘/home/miimera/public_html/system/cache/’);

    define(‘DIR_DOWNLOAD’, ‘/home/miimera/public_html/download/’);

    define(‘DIR_LOGS’, ‘/home/miimera/public_html/system/logs/’);

     

    • Hello Niranjan,

      Are you getting any specific errors when OpenCart attempts to display the images? Do you have error reporting enabled? Does it work when not using the subdomain?

      Kindest Regards,
      Scott M

  • One issue I am surprised non of the OC users have talked about is the images which are served from admin/images folder and //catalog/view/theme/default/image folder, what happenens to those images why no subdomain for those images too can someone explain.

    • Hello sunsys,

      Thank you for contacting us. We are happy to help, but it is not clear what you are asking. Can you provide more details on what you are trying to accomplish.

      Thank you,
      John-Paul

  • Anil Damiwal,

     

    I know this is old, however, just wanted to point out a syntax error in your config.php.  You stated that you subdomain was “img.fitnessmart.com.”  Yet your config.php files are point to “img.a2zfitnessmart.com.”  either change the config.php files or create the appropriate subdomin to match the existing entries.

    Have a blessed and prosperous day!  Yes this works in current versions as well.

  • This works for newer versions however due to the way Opencart caches the images it only works the cache image is available within the directory of the subdomain.

    For example

    Standard image is automatically resized 

    http://www.yourdomain.com/image/data/image1.jpg becomes

    http://www.yourdomain.com/image/cache/data/image1-500×500.jpg

    There the cache image must also be created within the folder for the subdomain (images.yourdomain.com/image/cache/data/image1-500×500.jpg)

     

    New OpenCart Versions Image Structure: 

    catalog/model/tool/image.php 
    Replace this: return $this->config->get('config_ssl') . 'image/' . $new_image; 
    To This: return 'https://images.yourdomain.com/' . $new_image; 

    AND

    Replace this: return $this->config->get('config_url') . 'image/' . $new_image; 
    To This: return 'http://images.yourdomain.com/' . $new_image;

  • I made subdomain (img.fitnessmart.com with folder name img), and copy image folder of root directory and paste into img folder. Both config files are changed like:

    1. root/config.php:

    // HTTP
    define('HTTP_SERVER', 'http://a2zfitnessmart.com/'); define('HTTP_IMAGE', 'http://img.a2zfitnessmart.com/'); // HTTPS define('HTTPS_SERVER', 'http://a2zfitnessmart.com/'); define('HTTPS_IMAGE', 'http://img.a2zfitnessmart.com/'); // DIR define('DIR_APPLICATION', '\www.a2zfitnessmart.com/catalog/'); define('DIR_SYSTEM', '\www.a2zfitnessmart.com/system/'); define('DIR_DATABASE', '\www.a2zfitnessmart.com/system/database/'); define('DIR_LANGUAGE', '\www.a2zfitnessmart.com/catalog/language/'); define('DIR_TEMPLATE', '\www.a2zfitnessmart.com/catalog/view/theme/'); define('DIR_CONFIG', '\www.a2zfitnessmart.com/system/config/'); define('DIR_IMAGE', '\img/image/'); define('DIR_CACHE', '\www.a2zfitnessmart.com/system/cache/'); define('DIR_DOWNLOAD', '\www.a2zfitnessmart.com/download/'); define('DIR_LOGS', '\www.a2zfitnessmart.com/system/logs/');

    2. root/admin/config.php

    // HTTP define('HTTP_SERVER', 'http://a2zfitnessmart.com/admin/'); define('HTTP_CATALOG', 'http://a2zfitnessmart.com/'); define('HTTP_IMAGE', 'http://img.a2zfitnessmart.com/'); // HTTPS define('HTTPS_SERVER', 'http://a2zfitnessmart.com/admin/'); define('HTTPS_CATALOG', 'http://a2zfitnessmart.com/'); define('HTTPS_IMAGE', 'http://img.a2zfitnessmart.com/'); // DIR define('DIR_APPLICATION', '\www.a2zfitnessmart.com/admin/'); define('DIR_SYSTEM', '\www.a2zfitnessmart.com/system/'); define('DIR_DATABASE', '\www.a2zfitnessmart.com/system/database/'); define('DIR_LANGUAGE', '\www.a2zfitnessmart.com/admin/language/'); define('DIR_TEMPLATE', '\www.a2zfitnessmart.com/admin/view/template/'); define('DIR_CONFIG', '\www.a2zfitnessmart.com/system/config/'); define('DIR_IMAGE', '\img/image/'); define('DIR_CACHE', '\www.a2zfitnessmart.com/system/cache/'); define('DIR_DOWNLOAD', '\www.a2zfitnessmart.com/download/'); define('DIR_LOGS', '\www.a2zfitnessmart.com/system/logs/'); define('DIR_CATALOG', '\www.a2zfitnessmart.com/catalog/');

    But images are not accessing in admin panel and website front page. So please guide me about that.

    • Hello Anil,

      I installed and checked different versions of OpenCart and your versions (1.5.5.1) is the first version that does not support the HTTP_IMAGE definition. (1.4.4.1 is the last version to have this code) Adding the line to the config file does not break anything, but the definition is not used anywhere in the code. This article is unfortunately obsolete for OpenCart 1.5.5.1 and up. We will make the necessary changes to reflect that. Thank you for helping us to find this out.

      Kindest Regards,
      Scott M

    • Hello Raka,

      This article is about changing the HTTP_IMAGE definition and not the DIR_IMAGE definition. Unfortunately, OpenCart versions after 1.4.4.1 do not support the HTTP_IMAGE definition.

      Kindest Regards,
      Scott M

    • Hello MM,
      After OpenCart 1.5.5.1, this does not work. It is only for versions prior to 1.5.5.1.

      Kindest Regards,
      Scott M

  • Warning: Image cache directory DIR_IMAGEcache/ not writeable!

    Version 1.5.6.4

    Images dont get cached and admin panel displays the above error….

    I think you need to change these just before installation and not after

  • Thanks for reply Mr. Scott.

    I am using opencart 1.5.5.1. I am new for opencart and don’t know how to change the definition. Please guide me about it.

    Regards,

    Anil Damiwal

    • Could you clarify what exactly the issue is? It appears that in the above comment, you only provided the configuration file and nothing more.

    • Hello Anil,

      What version of OpenCart are you using? Did the definition already exist for you to change or did you add it?

      Kindest Regards,
      Scott M

    • Hello Gabriel,

      I will need a bit more information from you. What version of OpenCart are you using? Did the definition already exist for you to change or did you add it?

      Kindest Regards,
      Scott M

  • // HTTP
    define('HTTP_SERVER', 'http://a2zfitnessmart.com/');
    define('HTTP_IMAGE', 'http://img.a2zfitnessmart.com/');
    // HTTPS
     define('HTTPS_SERVER', 'http://a2zfitnessmart.com/');
    define('HTTPS_IMAGE', 'http://img.a2zfitnessmart.com/');
    
    // DIR
    define('DIR_APPLICATION', '\www.a2zfitnessmart.com/catalog/');
    define('DIR_SYSTEM', '\www.a2zfitnessmart.com/system/');
    define('DIR_DATABASE', '\www.a2zfitnessmart.com/system/database/');
    define('DIR_LANGUAGE', '\www.a2zfitnessmart.com/catalog/language/');
    define('DIR_TEMPLATE', '\www.a2zfitnessmart.com/catalog/view/theme/');
    define('DIR_CONFIG', '\www.a2zfitnessmart.com/system/config/');
    define('DIR_IMAGE', '\img.a2zfitnessmart.com/');
    define('DIR_CACHE', '\www.a2zfitnessmart.com/system/cache/');
    define('DIR_DOWNLOAD', '\www.a2zfitnessmart.com/download/');
    define('DIR_LOGS', '\www.a2zfitnessmart.com/system/logs/'); 
    
    • Hello Absarul,

      The article describes changing the HTTP_IMAGE definition. Your config.php does not contain that definition. Looking at a newly installed version of OpenCart, I do not see that definition either. I will check to see if the information here is either out of date or covering a specific type of setup.

      Kindest Regards,
      Scott M

  • This is my config.php. What & How cane i change?

    <?php
    // HTTP
    define('HTTP_SERVER', 'http://kitabonkiduniya.com/');

    // HTTPS
    define('HTTPS_SERVER', 'http://kitabonkiduniya.com/');

    // DIR
    define('DIR_APPLICATION', '/home/kitabonk/public_html/catalog/');
    define('DIR_SYSTEM', '/home/kitabonk/public_html/system/');
    define('DIR_DATABASE', '/home/kitabonk/public_html/system/database/');
    define('DIR_LANGUAGE', '/home/kitabonk/public_html/catalog/language/');
    define('DIR_TEMPLATE', '/home/kitabonk/public_html/catalog/view/theme/');
    define('DIR_CONFIG', '/home/kitabonk/public_html/system/config/');
    define('DIR_IMAGE', '/home/kitabonk/public_html/image/');
    define('DIR_CACHE', '/home/kitabonk/public_html/system/cache/');
    define('DIR_DOWNLOAD', '/home/kitabonk/public_html/download/');
    define('DIR_LOGS', '/home/kitabonk/public_html/system/logs/');

    // DB
    define('DB_DRIVER', 'mysql');
    define('DB_HOSTNAME', 'xxxxxxxxxx');
    define('DB_USERNAME', 'xxxxxxxxxxx');
    define('DB_PASSWORD', 'xxxxxxxxxxxxxx');
    define('DB_DATABASE', 'xxxxxxxxxx');
    define('DB_PREFIX', 'xxxxx');
    ?>

    • You would simply change this line as described within the article:

      define(‘DIR_IMAGE’, ‘/home/kitabonk/public_html/image/’);

  • Don’t you need to do this in the admin/config.php too, otherwise when you go to add an image in admin it will try to add it to the wrong folder?

    Also, do you need to update the separate .htaccess file in the new subdomain to match your main ht.access file?

    • Hello Aaron,

      If the admin/config.php file has HTTP_IMAGE definitions, you will also want to change them there. I have added a sentence to the article stating that. As for the htaccess file, this should not need to be changed for this purpose.

      Kindest Regards,
      Scott M

    • If the lines do not already exist, you may simply add them and OpenCart will use those directives.

  • Good afternoon,

    Here is present the case when is only one store (base store). But how must made in the the case of multi-stores when we have more stores added as sub-domains or addons domains?

    I am very interested about subject, so I waiting your answer much early is possible.

    Thank you for advice.

    Best regards.

Leave a Reply