Home >Backend Development >PHP Tutorial >Automatic Asset Optimization with Munee

Automatic Asset Optimization with Munee

Lisa Kudrow
Lisa KudrowOriginal
2025-02-16 10:11:08451browse

Munee: A powerful PHP asset management tool, say goodbye to NodeJS

Munee is a PHP-based asset management tool that can compile LESS, SCSS or CoffeeScript, process images, compress CSS and JS, and dynamically cache assets on the server and client side. It supports PHP 5.3 and later. This tutorial will explain how to use Munee to simplify how assets are included in templates, as well as how they are installed, working, and using them. Munee provides an alternative to NodeJS for asset management of PHP applications.

Automatic Asset Optimization with Munee

Core advantages:

  • Omni-purpose asset management: Munee is able to compile LESS, SCSS and CoffeeScript, process images, compress CSS and JS, and implement server-side and client-side caching. It automates many tasks that would have to be done manually, saving time.
  • Dynamic processing: Munee processes or compresses these assets according to query string parameters by intercepting client requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files. It also enables server-side caching by storing compiled, compressed, and processed assets in separate directories.
  • Image Processing: In addition to compiling and compressing CSS and JS files, Munee can dynamically adjust image size, crop, color, and convert images to grayscale or negative effects. It can also replace missing images with default images and combine assets to reduce the total number of HTTP requests required to get all assets.
  • Easy to install and customize: Munee can be easily installed through Composer and provides API provisioning to manually optimize, process and compress assets with programs. This is a highly customizable tool that allows developers to specify the optimization techniques to use and adjust the compression level.

Why choose Munee?

Munee is designed to simplify asset management. It dynamically performs many tasks that we had to do manually before (i.e., when the client requests assets), thus saving time. Here are some reasons why you might want to use Munee:

  1. We often make small changes to CSS, LESS, SCSS, JavaScript, and CoffeeScript files. Each change requires compilation and compression. Munee will do these operations automatically.
  2. Many shared web hosting servers do not have gzip enabled. If you use Munee, it will use PHP to compress the file, ignoring this limitation.
  3. You don't need to worry about adding cache directives to .htaccess files. Munee will be responsible for server-side and client-side asset caching.
  4. Munee can process images dynamically and is very convenient for responsive web design. You do not need to maintain different image files for different sizes.

How Munee works:

After Munee is installed, it will automatically start cache assets on the server, send the correct client cache header, and start sending gzip compressed responses, as well as compilation output to requested LESS, SCSS, and CoffeeScript files.

To provide instructions for processing images or compressing CSS and JS files, you need to add query string parameters to the asset path.

To be able to process or compress assets, Munee needs to intercept client requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files. In order for Munee to intercept client requests for these assets, we need to add internal rewrite rules to the .htaccess file (Nginx description is also below).

Munee uses other third-party libraries (such as imagine, leaf, meenie, tedivm, etc.) to resize, process, compile and compress assets.

The query string parameters used to provide instructions to Munee are called filters.

How does Munee cache assets?

To implement client caching, it sets the Cache-Control: must-revalidate header when sending a response to the requested asset. It also reads the cache header in the HTTP request and sends a response or 304 Not Modified state based on whether there are latest assets in the client cache.

To implement server-side caching, it stores compiled, compressed, and processed assets in a separate directory.

It detects changes to the original asset at runtime. When it detects changes to the original asset, it updates the server cache and forces the client to use the latest file.

Installation of Munee:

Install with the following command:

<code class="language-bash">composer require meenie/munee</code>

If you try to install Munee on a shared hosting server, use composer require Munee manually on your local computer and upload the vendor directory to the hosting server.

Now we need to create a PHP file that is responsible for optimizing, processing, compiling and compressing assets using Munee. To do this, create a PHP file called munee.php:

<code class="language-php"><?php
require "vendor/autoload.php";

echo \Munee\Dispatcher::run(new \Munee\Request());</code>

Now we need to redirect asset requests for CSS, LESS, SCSS, JS, CoffeeScript and various image format files to munee.php. We can do this using the server's internal URL rewrite rules.

If you are using Apache, place this code in the .htaccess file in the directory where the munee.php file:

<code class="language-apache">RewriteEngine On
RewriteRule ^(.*\.(?:css|less|scss|js|coffee|jpg|png|gif|jpeg))$ munee.php?files=/ [L,QSA,NC]</code>

If you are using Nginx, you must modify the actual virtual host settings for URL rewrite rules based on this issue and this gist.

If .htaccess is disabled on your server, or you don't want to use .htaccess for rewriting, then you can manually pass the file path to munee.php instead of using the asset path in HTML.

For the rest of this tutorial, we will assume that you are using .htaccess.

All assets in the directory tree where Munee is installed will be optimized.

(The following content is a brief summary of the rest of the original text to avoid duplication and redundancy)

Compile SCSS, LESS and CoffeeScript: Simply point to these files in HTML and Munee will automatically handle the server-side compilation.

Compress CSS and JS files: Add the minify=true parameter to the asset path in HTML.

Processing images: Munee allows dynamic resizing, cropping, and shading of images, and supports grayscale, negative film conversion, and placeholders for missing images. Image size and cropping can be controlled using the resize filter, and an alternative to missing images can be configured in the placeholders array. Munee has built-in security mechanisms to prevent malicious image processing requests. munee.php

Combination Assets: Use comma-separated file paths in HTML to combine multiple CSS or JS files.

Munee API: Provides API provisioning applications to manually optimize assets.

Summary: Munee is ideal for dynamically managing assets.

FAQ (FAQ): (The original FAQ part has been summarized and core information is retained)

Munee is a standalone PHP library for automating a variety of web performance optimization tasks. Compared with other tools, it requires no additional software, is highly flexible and customizable, and supports a variety of file types and optimization technologies. It can handle image optimization, CSS and JavaScript optimizations, and supports custom optimization settings. The system requirements are PHP 5.3 or higher, as well as the GD library and LESS/SCSS compilation library.

The above is the detailed content of Automatic Asset Optimization with Munee. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn