CodeIgniter Forums
AssetLibPro - An advanced CI Asset Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: AssetLibPro - An advanced CI Asset Library (/thread-8175.html)

Pages: 1 2 3 4 5 6


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]TheLoops[/eluser]
AssetLibPro - An advanced CI Asset Library
http://code.google.com/p/assetlib-pro/

Release Date: Aug. 05th '08
Version: 1.0.5
Bug Fixes: Removed left over debugging statements.
Fixed all the stuff listed here
Improvements: Added support for "modules" for easy page theming.
(Thanks to a&w; for those suggestions and fixes!)
Changes: Paths now need to be relative to asset directory (see config file)
Link: Google Code Project

AssetLibPro uses code from AssetLib, (Thanks Andre!)
but does not have that much in common with AssetLib anymore.

In addition to AssetLib's 'jsmin' and 'csstidy' functionality AssetLibPro brings you the following:

1. Fixes relative url() paths in your css files to ensure their validity.
2. Re-creates compressed files automatically once a source file was changed making sure that your cached files are always up-to-date.
3. Sets HTTP headers for far-future caching expiration for forced browser caching.
4. Supports branching for the use of multiple CSS selections (like "screen" or "print").
5. Allows you to use multiple sets of css/js files per CI application for minimized performace overhead
6. Supports GZip compression of cached files to take bandwidth optimization a step further.

Ps: Don't forget to leave a comment if you like it (or don't) ;-)


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Daeli[/eluser]
Hey TheLoops. Good luck with your project - it sounds very good Smile


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Tom Glover[/eluser]
Your on me blog.


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Unknown[/eluser]
As web developers, we often run into problems while handling various assets such as images, scripts and css files. To make it easier to access and call such assets from within the Code Igniter framework, here is an implementation of the Asset Library, which we have been using at Wallwisher.com. Hope you find it useful.

Structure

In parallel to the Code Igniter system folder in your implementation, place a new folder called assets. Inside this folder, feel free to describe new assets such as images, scripts, styles and others necessary for your implementation. You need the following files to implement the library:
(get the code at www.wallwisher.com/blog)

Configuration File: to be placed in system/application/config/ (assets_config.php.txt)
Helper File: to be placed in system/application/helpers/ (assets_helper.php.txt)
Library File: to be placed in system/application/libraries/ (assets.php.txt)

Examples

Here are a few examples which might help you in understanding the usage of this library:

$new_image = '.'.asset_url('example.jpg','images');
$CI =& get_instance(); $new_css = $CI->assets->get_asset_from_name('css',$name);


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Maxximus[/eluser]
Hi, there is an issue in the _fix_css_urls function: relative_path_to.
Also, when reading the code it seems to rebuild a non-relative path without the http:// part too.

Have disabled the function for now, and works nicely!

EDIT: add header("Content-type: text/css"); to the CSS.php file


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]TheLoops[/eluser]
[quote author="Maxximus" date="1210293699"]Hi, there is an issue in the _fix_css_urls function: relative_path_to.
Also, when reading the code it seems to rebuild a non-relative path without the http:// part too.

Have disabled the function for now, and works nicely![/quote]The _fix_css_urls() function is only required if the css url() paths are set relative to the css file's location.
If they are set relative to the web root (leading slash), then no re-linking is needed.

You are right, there was a bug in there. I uploaded a fixed v1.0.1: http://code.google.com/p/assetlib-pro/downloads/list


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]TheLoops[/eluser]
pranavpiyush, I'm not quite sure how your asset library is comparable to AssetLibPro?
Your library looks like a asset path repository, while AssetLibPro' main purpose is bandwidth and performance optimization.

WackyWebs.net, thanks ;-) Good to see that you guys find it useful :-D


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Maxximus[/eluser]
Well to be honest I just read this part:

Code:
foreach ($matches['urls'] as $old_url) {
                    $old_url = trim($old_url,'"\'');
                    if (strlen($old_url[1]) > 7 && strcasecmp(substr($old_url[1], 0, 7), 'http://') == 0) {
                        $new_url = $old_url;
                    } else {
                        $new_url = dirname($css_path).'/'.$old_url;
                    }
                    $urls[$old_url] = relative_path_to(str_replace(dirname(FCPATH),'',trim($this->cache_dir_css)).'/', $new_url);
                }
It seems to always do the relative_path_to function. Tried it with a relative_path_to function found on the net.

You were pretty quick with your reaction Wink, just to make sure you see it: pls add header("Content-type: text/css"); to the generated CSS when using .php extension.
EDIT: See it's there but for some reason it was notgenerated in the file. I'll look in to it...


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]TheLoops[/eluser]
[quote author="Maxximus" date="1210296123"]Well to be honest I just read this part:

Code:
foreach ($matches['urls'] as $old_url) {
                    $old_url = trim($old_url,'"\'');
                    if (strlen($old_url[1]) > 7 && strcasecmp(substr($old_url[1], 0, 7), 'http://') == 0) {
                        $new_url = $old_url;
                    } else {
                        $new_url = dirname($css_path).'/'.$old_url;
                    }
                    $urls[$old_url] = relative_path_to(str_replace(dirname(FCPATH),'',trim($this->cache_dir_css)).'/', $new_url);
                }
It seems to always do the relative_path_to function. Tried it with a relative_path_to function found on the net.

You were pretty quick with your reaction Wink, just to make sure you see it: pls add header("Content-type: text/css"); to the generated CSS when using .php extension.[/quote]
The code checks the path for a leading "http://". If it finds one, then the path is kept untouched. Else, it considers it relative and tries to re-link it.

Regarding the http header: The header refused to be added to the output due to a small bug. Check my first post for the updated v1.0.1 (at Google Code Project)


AssetLibPro - An advanced CI Asset Library - El Forum - 05-08-2008

[eluser]Maxximus[/eluser]
Ok here's the problem:

Code:
private function _http_headers($type = '', $gzip_compress = FALSE, $far_future_expire = TRUE) {
        if ($type === 'css')
            $mime_type = 'css';
        else if ($type === 'js')
            $mime_type = 'javascript';
        else
            return FALSE;

        $gzip_header = "";

        if ($gzip_compress) {
            $gzip_header = '<?php
// Gzip encode the contents of the output buffer.
function gzip_compress($output) {
$compressed_out = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
$compressed_out .= substr(gzcompress($output, 2), 0, -4);
if (strlen($output) >= 1000) {
    header("Content-Encoding: gzip");
    return $compressed_out;
} else {
    return $output;
}
}
if (strstr($HTTP_SERVER_VARS["HTTP_ACCEPT_ENCODING"], "gzip"))
    ob_start("gzip_compress");
?>';
        }
        if ($far_future_expire) {
            //When will the file expire?
            $offset = 6000000 * 60 ;
            $ExpStr = "Expires: " .
            gmdate("D, d M Y H:i:s",
            time() + $offset)." GMT";

            $far_future_expire .= '<?php
header("Content-type: text/'.$mime_type .'; charset: UTF-8");
header("Cache-Control: must-revalidate");
header("'.$ExpStr.'");
?>';
        }
        return $gzip_header;
    }
The $far_future_expire will never be used, and should be $gzip_header. Personally I would prefer to have the Content-type header as one of the first, so it's outputted also when there's no far_future_expire.