Home Backend Development PHP Tutorial Automatic Asset Optimization with Munee

Automatic Asset Optimization with Munee

Feb 16, 2025 am 10:11 AM

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:

composer require meenie/munee
Copy after login

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:

<?php
require "vendor/autoload.php";

echo \Munee\Dispatcher::run(new \Munee\Request());
Copy after login

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:

RewriteEngine On
RewriteRule ^(.*\.(?:css|less|scss|js|coffee|jpg|png|gif|jpeg))$ munee.php?files=/ [L,QSA,NC]
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles