定制 reeteshjee/minifyone 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

reeteshjee/minifyone

最新稳定版本:1.0.0

Composer 安装命令:

composer require reeteshjee/minifyone

包简介

A PHP library to combine and minify CSS/JS files into a single file for fewer HTTP requests.

README 文档

README

MinifyOne is a simple and lightweight PHP library to combine and minify multiple CSS or JavaScript files into a single optimized file.
It helps reduce the number of HTTP requests, improving page load speed and performance.

🚀 Features

  • ✅ Combine multiple CSS or JS files into one.
  • ✅ Basic minification to reduce file size.
  • ✅ Caching based on file hash — no unnecessary reprocessing.
  • ✅ Option to directly serve the file with correct headers.
  • ✅ Easy to integrate in any PHP project.
  • ✅ Works with Composer for easy installation.

📦 Installation

Install via Composer:

composer require reeteshjee/minifyone

Or manually include the src/MinifyOne.php file in your project.

🛠 Usage

1️⃣ Combine Files and Get File Path

require 'vendor/autoload.php';

use MinifyOne\MinifyOne;

// Create instance (output directory, enable/disable minify)
$minify = new MinifyOne(__DIR__ . '/cache', true);

// Combine JavaScript files
$combinedJs = $minify->combine(['assets/js/file1.js', 'assets/js/file2.js'], 'js');
echo "Combined JS: $combinedJs";

// Combine CSS files
$combinedCss = $minify->combine(['assets/css/style1.css', 'assets/css/style2.css'], 'css');
echo "Combined CSS: $combinedCss";

This will:

  1. Combine the files.
  2. Minify content (if enabled).
  3. Store them in the specified cache folder.
  4. Return the path to the combined file.

2️⃣ Combine and Serve Directly

If you want to serve the combined file immediately in a request:

require 'vendor/autoload.php';

use MinifyOne\MinifyOne;

$minify = new MinifyOne(__DIR__ . '/cache');

// Output combined CSS directly to the browser
$minify->combineAndServe([
    'assets/css/style1.css',
    'assets/css/style2.css'
], 'css');

This will:

  • Send the correct Content-Type header.
  • Output the minified content.
  • Send long cache headers (max-age=31536000).

3️⃣ Example with HTML

You can generate a combined file and use it in HTML:

require 'vendor/autoload.php';

use MinifyOne\MinifyOne;

$minify = new MinifyOne(__DIR__ . '/cache');

// Generate combined JS file
$combinedJs = $minify->combine([
    'assets/js/jquery.js',
    'assets/js/app.js'
], 'js');

// Generate combined CSS file
$combinedCss = $minify->combine([
    'assets/css/reset.css',
    'assets/css/main.css'
], 'css');
?>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="<?= str_replace(__DIR__, '', $combinedCss) ?>">
</head>
<body>
    <script src="<?= str_replace(__DIR__, '', $combinedJs) ?>"></script>
</body>
</html>

⚙ Constructor Options

new MinifyOne($outputDir = __DIR__ . '/../cache', $minify = true);
  • $outputDir → Directory to save the combined files.
  • $minify → Enable or disable minification.

📌 Methods

Method Description Example
combine(array $files, string $type) Combines files and returns output file path. $minify->combine(['a.css', 'b.css'], 'css');
combineAndServe(array $files, string $type) Combines files and outputs directly with proper headers. $minify->combineAndServe(['a.js', 'b.js'], 'js');

🛡 Notes & Recommendations

  • This is a basic minifier. For advanced compression, integrate with matthiasmullie/minify.
  • Ensure your $outputDir is writable by the web server.
  • File hash ensures that if files change, a new combined file is generated automatically.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-11