Search code examples
phpopencartopencart2.x

How to use a subdomain for images in opencart 2?


This is my config file. I have changed for image subdomain but images do not load.

<?php

    // HTTP
    define('HTTP_SERVER', 'http://www.Yek.com/');
    define('HTTP_IMAGE', 'http://img.Yek.com/');

    // HTTPS
    define('HTTPS_SERVER', 'https://www.Yek.com/');
    define('HTTPS_IMAGE', 'https://img.Yek.com/');

    // DIR
    define('DIR_APPLICATION', '/home/Yek/public_html/catalog/');
    define('DIR_SYSTEM', '/home/Yek/public_html/system/');
    define('DIR_DATABASE', '/home/Yek/public_html/system/database/');
    define('DIR_LANGUAGE', '/home/Yek/public_html/catalog/language/');
    define('DIR_TEMPLATE', '/home/Yek/public_html/catalog/view/theme/');
    define('DIR_CONFIG', '/home/Yek/public_html/system/config/');
    //define('DIR_IMAGE', '/home/Yek/public_html/image/');//defult For Images/catlog on main host
    define('DIR_IMAGE', '/home/Yek/public_html/img/image/'); // image/catalog in download host

    define('DIR_CACHE', '/home/Yek/public_html/system/cache/');
    define('DIR_DOWNLOAD', '/home/Yek/public_html/system/download/');
    define('DIR_LOGS', '/home/Yek/public_html/system/logs/');
    define('DIR_UPLOAD', '/home/Yek/public_html/system/upload/');
    define('DIR_MODIFICATION', '/home/Yek/public_html/system/modification/');


    // DB
  ....
?>

this is my image.php . edit for me? not know where edit this code........................................................................................................................................................................................................................................................................

<?php
class ModelToolImage extends Model {
    public function resize($filename, $width, $height) {
        if (!is_file(DIR_IMAGE . $filename)) {
            return;
        }

        $extension = pathinfo($filename, PATHINFO_EXTENSION);

        $old_image = $filename;
        $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;

        if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image))) {
            $path = '';

            $directories = explode('/', dirname(str_replace('../', '', $new_image)));

            foreach ($directories as $directory) {
                $path = $path . '/' . $directory;

                if (!is_dir(DIR_IMAGE . $path)) {
                    @mkdir(DIR_IMAGE . $path, 0777);
                }
            }

            list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);

            if ($width_orig != $width || $height_orig != $height) {
                $image = new Image(DIR_IMAGE . $old_image);
                $image->resize($width, $height);
                $image->save(DIR_IMAGE . $new_image);
            } else {
                copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
            }
        }

        if ($this->request->server['HTTPS']) {
            return $this->config->get('config_ssl') . 'image/' . $new_image;
        } else {
            return $this->config->get('config_url') . 'image/' . $new_image;
        }
    }
}

Solution

  • /catalog/model/tool/image.php found $this->config->get('config_url') and change

    protected function getImageUrl($new_image) {
        $parts = explode('/', $new_image);
        $new_url = implode('/', array_map('rawurlencode', $parts));
        if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
            return $this->config->get('config_ssl') . 'image/' . $new_url;
        }
        else {
            return $this->config->get('config_url') . 'image/' . $new_url;
        }
    }