YUICompressor or similar in PHP?

12,774

Solution 1

Yes there is, it's called minify.

The only thing in to worry about in the way of complexity is setting up a group, and there's really nothing to it. Edit the groupsConfig.php file if you want multiple JS/CSS in one <script> or <link> statement:

return array(
     'js-common' => array('//js/jquery/jquery-1.3.2.min.js', '//js/common.js', '//js/visuals.js',
'//js/jquery/facebox.js'),
     'css-common' => array('//css/main.css', '//css/layout.css','//css/facebox.css')
);

To include the above 'js-common' group, do this:

<script type="text/javascript" src="/min/g=js-common"></script>

Solution 2

(i know i was looking for the exact same thing not knowing how to deal directly with the jar file using php - that's how i ended up here so i'm sharing what i found)

Minify is a huge library with tons of functionalities. However the minifying part is a very tiny class : http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/YUICompressor.php

& very very easy to use :

//set the path to the jar file
Minify_YUIcompressor::$jarFile=_ROOT.'libs/java/yuicompressor.jar';
//set the path to a writable temp folder
Minify_YUIcompressor::$tempDir=_ROOT.'temp/';

//minify
$yourcssminified=Minify_YUIcompressor::minifyCss($yourcssstringnotminified,$youroptions)

same process for js, if you need more functionalities just pick from the library & read the source to see how you can make direct call from your app.

I didn't read the question well, since minify is based on using the jar files, the op can't use it anyway with his server config

Minify also include other minifying methods than yui, for example:

http://code.google.com/p/minify/source/browse/trunk/min/lib/JSMinPlus.php?r=443&spec=svn468

Share:
12,774
Alan Plum
Author by

Alan Plum

Updated on June 18, 2022

Comments

  • Alan Plum
    Alan Plum about 2 years

    I've been using yuicompressor.jar on my test server for on-the-fly minimisation of changed JavaScript files. Now that I have deployed the website to the public server, I noticed that the server's policies forbid the use of exec() or its equivalents, so no more java execution for me.

    Is there a decent on-the-fly JS compressor implemented in PHP? The only thing resembling this that I was able to find was Minify, but it's more of a full-blown compression solution with cache and everything. I want to keep the files separate and have the minimised files follow my own naming conventions, so Minify is a bit too complex for this purpose.

    The tool, like yuicompressor, should be able to take either a filename or JavaScript as input and should either write to a file or output the compressed JavaScript.

    EDIT: To clarify, I'm looking for something that does not have to be used as a standalone (i.e. it can be called from a function, rather than sniffing my GET variables). If I just wanted a compressor, Minify would obviously be a good choice.

    EDIT2: A lot has changed in the five years since I asked this question. Today I would strongly recommend separating the front-end workflow from the server code. There are plenty of good tools for JS development around and except for the most trivial jQuery enhancements it's a better idea to have a full workflow with automated bundling, testing and linting in place and just deploy the minified bundles rather than the raw files.

  • Alan Plum
    Alan Plum about 15 years
    As I said. I can't find a way to run minify from within an application. Heck, it even expects certain GET params to be set rather than taking a filename as argument. It's probably good, but not what I'm looking for.
  • karim79
    karim79 about 15 years
    Minify is not complex. You can have a specific file minified simply by using something like <script type="text/javascript" src="/min/f=somefile"></script>
  • Alan Plum
    Alan Plum about 15 years
    It's complex in terms of its "API". Actually it doesn't really have one. It's a standalone tool that reads superglobals for input. If you can point me to a wrapper that lets me use Minify from within an application, it would be a valid option.
  • karim79
    karim79 about 15 years
    @Alan - Minify is based on a bunch of classes, all of which can be used from within your application. For example, this: code.google.com/p/minify/source/browse/trunk/min/lib/Minify/‌​… I'm not digging for rep as I've maxed out for the day. Just trying to help :)
  • Alan Plum
    Alan Plum about 15 years
    No allegations made. Maybe I misunderstand Minify, but the fact it comes with a huge config tool and that the controllers the main class use fetch their input arguments from GET made me think it's a pure standalone tool. If I'm mistaken, I guess I'll have to hack the source a bit to figure out how the actual compression is handled and see how I can interface with that part directly.
  • karim79
    karim79 about 15 years
    @Alan - the JS compression bit is based on JSMin - crockford.com/javascript/jsmin.html. The 'configurator' is not a necessity, it's just a convenience tool or 'starter kit'. The only thing that requires work is declaring a groups array which I have already presented in my answer. Look into it, is my advice.
  • Alan Plum
    Alan Plum about 15 years
    Thanks for pointing that out. I just realised that if you take away the CSS and HTML compressors and the YUICompressor bridge, you might as well use JSMin directly. JSMin seems to do pretty much what I want, so I'll go and use that or Minify's own version of it. Thanks.
  • Aust
    Aust over 10 years
    Your first link didn't work for me. I used code.google.com/p/minify/source/browse/min/lib/Minify/…