Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS minify with PHP extension

Tags:

php

minify

How can I minify a .php file with CSS contents?
Currently I get a 400 error.

Normally I call minify like this

<link rel="stylesheet" type="text/css" 
       href="{$workspace}/min/f=workspace/css/common.css" />

EDIT

The answer is changing the minify source code, but what is the change I should make?

In other words.. this call should work and process as CSS..

<link rel="stylesheet" type="text/css" 
      href="{$workspace}/min/f=workspace/css/common.php" />

Maybe with an optional declaration?

<link rel="stylesheet" type="text/css" 
href="{$workspace}/min/f=workspace/css/common.php&type=css" />

EDIT

I created the project here @ https://github.com/into/less-less

like image 550
Kirk Strobeck Avatar asked Aug 28 '11 01:08

Kirk Strobeck


People also ask

How do I minify PHP HTML output?

Use the ob_start() Function With a Callback to Minify HTML Output of the PHP Page. You can use the ob_start() function with a callback to remove whitespaces before and after the tags, comments, and whitespace sequences.


2 Answers

Your CSS+PHP script outputs CSS only after it's requested from a server and parsed by PHP. Minify reads files directly from the server, skipping the HTTP request. So I see two paths:

  1. Less optimal [?]: make minify download the CSS like this:

    <link rel="stylesheet" type="text/css" href="{$workspace}/min/f=http://site.com/workspace/css/common.php" />
    
  2. Include Minify lib in your common.php file and use its classes (e.g. Minify_CSS) before output. Something like echo Minify_CSS::minify($css)

Update:

Your example repo contains a strange file name which wouldn't let me pull/push appropriately, so here's the changed report.php:

<pre>
<strong>LESS in</strong>
<?= file_get_contents('workspace/less/common.less') ?>
- - - - -
<strong>CSS out</strong>
<?
require 'workspace/php/lessc.inc.php';
$lc = new lessc();
$contents = file_get_contents( 'workspace/less/common.less' );
$css = $lc->parse( $contents );
echo $css;
?>
<strong>Minified</strong>
<?php
require 'workspace/min/lib/Minify/CSS/Compressor.php';
echo Minify_CSS_Compressor::process($css);
?>
</pre>
like image 110
Andrew Avatar answered Sep 29 '22 09:09

Andrew


No, you can't easily do it as minify heavily depends on file extensions (css,js,?). For example it is used to determine what HTTP headers send to client(application/x-javascript,text/css,?), what minifier class to use, is this file safe to parse etc.

But I'm almost certain that this situation can be avoided. Could you please describe why exactly you want to do this?

If you insist on doing it this way I can propose a few dirty hacks to make it work, but it requires changing minify's source code so I don't really know if that is a good idea.

Upd:

There is no nice way to change this source: it has really bad structure. In minify v2.1.3 you can simply change the following:

Path: lib/Minify/Controller/Base.php##Minify_Controller_Base::_fileIsSafe()

return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));

-->

return in_array(strrev($revExt), array('js', 'css', 'html', 'txt', 'php'));

Path: lib/Minify/Controller/MinApp.php##Minify_Controller_MinApp::setupSources()

preg_match('/^[^,]+\\.(css|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'])

-->

preg_match('/^[^,]+\\.(css|js|php)(?:,[^,]+\\.\\1)*$/', $_GET['f'])

Path: lib/Minify/##Minify_Source::__construct()

case 'css'  : $this->contentType = 'text/css';

-->

case 'php': case 'css': $this->contentType = 'text/css';

and everything will work, but you must set $min_serveOptions['minApp']['allowDirs'] in configuration carefully as any user may be able to view any php file from this directories.

like image 32
XzKto Avatar answered Sep 29 '22 09:09

XzKto