nielse63 / php-image-cache

Image Cache is a very simple PHP class that accepts an image source and will compress and cache the file, move it to a new directory, and returns the new source for the image.
http://nielse63.github.io/php-image-cache
Other
456 stars 104 forks source link

Incorrect work of makesource function on some servers #5

Closed sil3ntboy closed 10 years ago

sil3ntboy commented 10 years ago

Hi,

Yesterday I used ImageCache class to cache some images on my website. I noticed, that when $_SERVER['DOCUMENT_ROOT'] = '/', then solution presented currently in ImageCache.php is going to replace all occurences of slash, which results in incorrect source string.

For example, if resulting string should be: /path/to/img , then after using makesource function, we've got: pathtoimg.

I've done some workaround for it, which works for me:

private function makesource( $dir ) {
        $cururl = strtolower( reset( explode( '/', $_SERVER['SERVER_PROTOCOL'] ) ) ) . '://' . $_SERVER['SERVER_NAME'];
        $base = $_SERVER['DOCUMENT_ROOT'];

        if($base == '/')
        {
            $localpath = substr($dir, 1);
        }else{
            $localpath = str_replace( $base, '', $dir );
        }

        return $cururl . $localpath;
}

Best regards, Mike

nielse63 commented 10 years ago

Awesome, thanks Mike. I'll make sure to test and implement this in the next iteration.

nielse63 commented 10 years ago

I took care of that issue when I pushed some more changes tonight, Mike. Thanks for checking that out and the heads up.

sil3ntboy commented 10 years ago

No problem, I'm glad that I could help ;-)