CodeIgniter Forums
re-routing images paths - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: re-routing images paths (/showthread.php?tid=8068)



re-routing images paths - El Forum - 05-04-2008

[eluser]AtlantixMedia[/eluser]
Hello,

can anyone tell me what's wrong with this re-routing rule?

$route['img/(general|bg|btn|logo)/(.*)'] = "system/application/images/$1/$2";

so that a url like this www.domain.com/img/general/test.gif

should be re-routed to www.domain.com/system/application/images/general/test.gif

it's not working as expected. thanks!!!


re-routing images paths - El Forum - 05-04-2008

[eluser]AtlantixMedia[/eluser]
anyone? any idea?


re-routing images paths - El Forum - 05-04-2008

[eluser]Pascal Kriete[/eluser]
What you're doing is rerouting to the controller function system/application and passing the rest as parameters. Routes are used internally to point at the right controller, they don't redirect. Basically means that you're restricted to ci controllers and can't point at server locations.

This is a job for htaccess:
Code:
RewriteRule ^img/(general|bg|btn|logo)/(.*)$ system/application/images/$1/$2 [L]

I'm guessing however, that your goal is to restrict access to the actual directory. Your could create an asset controller and 'include' the image with the proper header.
Code:
class Assets extends Controller {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function Assets()
    {
        // Yes, I am this lazy
        define('PATH'    ,    '/system/application/images/');
    }

    /**
     * Returns image files
     *
     * @access    public
     * @param    string    file path
     */
    function images($file)
    {
        if (file_exists(PATH.$file))
        {
            // Figure out mime-type
            $mimes = array(
                'gif'    =>    'image/gif',
                'jpeg'    =>    'image/jpeg',
                'jpg'    =>    'image/jpeg',
                'jpe'    =>    'image/jpeg',
                'png'    =>    'image/png',
                'tiff'    =>    'image/tiff',
                'tif'    =>    'image/tiff'
            );
            
            $ext = end(explode('.', $file));
            
            if (in_array($ext, $mimes));
            {
                header("Content-type: ".$mimes($ext));
                require(PATH.$file);
                exit;
            }
        }
        die('Invalid Image File: '.current(explode('.', $file)) );
    }
}

So now example.com/assets/images/stuff/test.jpg returns the image at system/application/images/stuff/test.jpg. If you change the class name to img and use the index function you can probably achieve the desired result.


re-routing images paths - El Forum - 05-05-2008

[eluser]AtlantixMedia[/eluser]
thanks for your response. how do the two solutions compare in terms of speed/load on resources? I'm trying the htaccess rewrite rule with no luck so far. Is there anything wrong with this?

RewriteRule ^(.*)/img/(general|bg|btn|logo)/(.*)$ $1/system/application/images/$2/$3 [L]

thanks


re-routing images paths - El Forum - 07-04-2008

[eluser]AtlantixMedia[/eluser]
[quote author="inparo" date="1209956594"]What you're doing is rerouting to the controller function system/application and passing the rest as parameters. Routes are used internally to point at the right controller, they don't redirect. Basically means that you're restricted to ci controllers and can't point at server locations.

This is a job for htaccess:
Code:
RewriteRule ^img/(general|bg|btn|logo)/(.*)$ system/application/images/$1/$2 [L]

I'm guessing however, that your goal is to restrict access to the actual directory. Your could create an asset controller and 'include' the image with the proper header.
Code:
class Assets extends Controller {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function Assets()
    {
        // Yes, I am this lazy
        define('PATH'    ,    '/system/application/images/');
    }

    /**
     * Returns image files
     *
     * @access    public
     * @param    string    file path
     */
    function images($file)
    {
        if (file_exists(PATH.$file))
        {
            // Figure out mime-type
            $mimes = array(
                'gif'    =>    'image/gif',
                'jpeg'    =>    'image/jpeg',
                'jpg'    =>    'image/jpeg',
                'jpe'    =>    'image/jpeg',
                'png'    =>    'image/png',
                'tiff'    =>    'image/tiff',
                'tif'    =>    'image/tiff'
            );
            
            $ext = end(explode('.', $file));
            
            if (in_array($ext, $mimes));
            {
                header("Content-type: ".$mimes($ext));
                require(PATH.$file);
                exit;
            }
        }
        die('Invalid Image File: '.current(explode('.', $file)) );
    }
}

So now example.com/assets/images/stuff/test.jpg returns the image at system/application/images/stuff/test.jpg. If you change the class name to img and use the index function you can probably achieve the desired result.[/quote]

I'm not quite sure how I am supposed to use your class. do I have to point an img tag to that?


re-routing images paths - El Forum - 07-04-2008

[eluser]Pascal Kriete[/eluser]
Yes, just treat it like it's the path to an image. If you just enter a path, i.e. assets/images/test.png you should see the image. And to include it in a website you use an image tag or add it to your css.


re-routing images paths - El Forum - 07-04-2008

[eluser]AtlantixMedia[/eluser]
wow fast response. ok, then something is not working for me...this is what I have:

image.php

Code:
<?php

class Image extends Controller {
    
    //===============================================
     function Image()
    //===============================================
    {
        parent::Controller();
    }


    //===============================================
     function Handle($img)
    //===============================================
    {
        header("Content-type: image/jpg");
        readfile("http://www.mydomain.com/system/application/images/thumbs/{$img}");
        exit;
    }

}
?>


test.php

Code:
<img src="http://www.mydomain.com/image/handle/0072027597383.jpg" alt="noimage">

I'm getting the noimage alt text.


re-routing images paths - El Forum - 07-04-2008

[eluser]Pascal Kriete[/eluser]
Can you access http://www.mydomain.com/image/handle/0072027597383.jpg normally and get an image?


re-routing images paths - El Forum - 07-04-2008

[eluser]AtlantixMedia[/eluser]
actually not. I get

The image “http://www.mydomain.com/image/handle/0072027597383.jpg” cannot be displayed, because it contains errors.

the image is there. It's in http://www.mydomain.com/system/application/images/thumbs/