CodeIgniter Forums
JSMin_PHP Library Port - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: JSMin_PHP Library Port (/showthread.php?tid=14952)



JSMin_PHP Library Port - El Forum - 01-21-2009

[eluser]tonydewan[/eluser]
This is a really quick port I did of the JSMin-php library. Very little was changed, so I don't even really want to take credit for it. Usage is exactly what you would expect.

Usage:
Drop the library in your libraries folder, and load the library as usual:
Code:
$this->CI->load->library('jsmin');

Minify a string like so:
Code:
$this->jsmin->minify($string);

I usually use it with file_get_contents(), like so:
Code:
$this->jsmin->minify( file_get_contents($file_reference, 'r') );

See the attached zip file for the full library.


JSMin_PHP Library Port - El Forum - 01-21-2009

[eluser]tonydewan[/eluser]
I forgot to mention that this is a PHP5 only library, as per the original JSMin-php file. If there is interest in a PHP4 version, I might be able to make that happen...


JSMin_PHP Library Port - El Forum - 01-28-2009

[eluser]tonydewan[/eluser]
New version. Nothing has really changed, just updated the documentation to be more accurate.


JSMin_PHP Library Port - El Forum - 01-28-2009

[eluser]louis w[/eluser]
I have been meaning to implement this myself. Saved me some work Smile

Why are you creating a new jsmin object inside itself? Couldn't you just do this?

Code:
public static function minify($js) {

    $this->input       = str_replace("\r\n", "\n", $input);
    $this->inputLength = strlen($this->input);
    
    $return = $this->min();

    // Housekeeping
    $this->input       = null;
    $this->inputLength = null;

    return $return;

  }

(And remove that from the constructor)


JSMin_PHP Library Port - El Forum - 01-28-2009

[eluser]tonydewan[/eluser]
I'm not sure why it was written that way, other than for the sake of brevity. You'd have to ask Ryan Grove (or perhaps Doug Crockford.) That is the way the class is written in the original JSMin PHP port. In porting it to CI, I only added the log_message function call and set a default value for $js in the constructor.

Your solution should work, assuming you match variable names (changing $js to $input):

Code:
public static function minify($input) {

    $this->input       = str_replace("\r\n", "\n", $input);
    $this->inputLength = strlen($this->input);
    
    $return = $this->min();

    // Housekeeping
    $this->input       = null;
    $this->inputLength = null;

    return $return;

  }

I still prefer the way it was originally written, if only because it's cleaner. Thanks for the comment!


JSMin_PHP Library Port - El Forum - 01-28-2009

[eluser]tonydewan[/eluser]
I've used this library in a full fledged asset management library called Carabiner. Check it out!


JSMin_PHP Library Port - El Forum - 06-16-2010

[eluser]morehawes[/eluser]
A big thanks for porting this - very useful! Smile


JSMin_PHP Library Port - El Forum - 06-17-2010

[eluser]Philipp GĂ©rard[/eluser]
yummy yummy! Looks promising, will be implemented in my current project. Thank you!


JSMin_PHP Library Port - El Forum - 08-07-2011

[eluser]runrun[/eluser]
I got a string like so

Lego (trademarked in capitals as LEGO) is a line of <a title="Construction toy" href="#">construction toys</a> manufactured by the <a title="Lego Group" href="#">Lego Group</a>, a privately held company based in <a title="Billund, Denmark" href="#">Billund, Denmark</a>. The company's flagship product, Lego, consists of colorful interlocking plastic bricks and an accompanying array of gears, <a title="Minifigure" href="#">minifigures</a> and various other parts. Lego bricks can be assembled and connected in many ways, to construct such objects as vehicles, buildings, and even working robots. Anything constructed can then be taken apart again, and the pieces used to make other objects. The toys were originally designed in the 1940s in Denmark[1] and have achieved an international appeal, with an extensive subculture that supports Lego movies, games, video games, competitions, and four Lego themed amusement parks.

When I try to minify it with php jsmin, I got "500 internal server error". What's special about that string that jsmin can't parse ?