Search code examples
codeignitercodeigniter-3

Codeigniter Image resize via subdomain url


i want make dynamic images url, where assets is subdomain

Working route :

$route['assets/images/([A-Za-z]+)/(\d+)/(.+)'] = "resize/img/$1/$2/$3";

img url : localhost/assets/images/test/350/item.jpg

but what i want its :

assets.localhost/images/test/350/item.jpg

how to make dynamic image resize with subdomain url like that ?

  • Resize controler code :
public function img($FileDir, $width,$filename)
    {
        if (isset($filename)){
            
            $FileUrl = 'assets/images/'.$FileDir.'/'.$filename;
            
            if(!file_exists($FileUrl)){
                show_404();
            }
            
            $this->load->library('image_lib');
            
            // configure image library
            $config = array(
                'image_library' => 'gd2',
                'source_image' => $FileUrl,
                'create_thumb' => true,
                'maintain_ratio' => true,
                'dynamic_output' => true,
                'quality' => '60%',
                'width' => $width
            );
            
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
            
        }
    }

Solution

  • Well, it's not important to make a route for assets.

    The correct way of making the assets folder is to put your assets folder inside a public folder in the route directory like this.

    Project_Folder 
    > application
    > public/assets/images 
    > system
    

    Once you did it, the 2nd step is to create a subdomain and assign that subdomain to public/assets directory.

    And here the 3rd step is to create a helper function.

    function assets_url($uri = '')
    {
        return 'https://assets.yourdomain.com/'.$uri;
    }
    

    And now replace this line in your code.

    $FileUrl = assets_url('images/'.$FileDir.'/'.$filename);