CSS minify with PHP extension

How can I minify a .php file with CSS content?
I am currently getting error 400 .

I usually call minify as follows

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

EDIT

The answer is to change the minimum source code, but what change should I make?

In other words .. this call should work and be handled like CSS.

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

Perhaps with an optional announcement?

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

EDIT

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

+6
source share
4 answers

Your CSS + PHP script only outputs CSS after it is requested from the server and is parsed by PHP. Minify reads files directly from the server, skipping the HTTP request. Therefore, I see two ways:

  • Less optimal [?]: Make minify loads CSS as follows:

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

Update:

Your repo example contains a weird file name that won't let me pull / click accordingly, so here report.php has changed:

 <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> 
+7
source

No, you cannot easily do this, since minimization is highly dependent on file extensions (css, js ,?). For example, it is used to determine which HTTP headers are sent to the client (application / x-javascript, text / css ,?), Which minifier class to use, this file is safe for parsing, etc.

But I am pretty sure that this situation can be avoided. Could you explain why exactly you want to do this?

If you insist on doing it this way, I can offer some dirty hacks to make it work, but this requires changing the minimal source code, so I don't know if this is really a good idea.

Upd:

This source cannot be changed: it has a 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 the setting, since any user can view any php file from these directories.

+1
source

Using CSS Min , you can freely do anything, and you can also “process” your stlysheet in a php script and then minimize it on the fly: its DEAD just do it, and guess what, it's just a SINGLE FILE.

Another way: do not use any PHP script to process or perform some logical actions in your css file, instead you could separate a small css file and then load whatever you want by creating a new cache file or simply combining and outputting the tag links.

However, if you now have something similar in your common.php (php file / script that outputs css, yes?)

 <?php $style = ''; $bodyStyle = 'body { background-color: #000; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #fff; }'; // I assumed you are about proccesing something here.. // ... // Then you merged all style into one string, and output it as css file $style = $bodyStyle + $otherStyle + $whateverStyle; header('Content-Type: text/css'); echo $style; ?> 

And you still want your application to bloat and make your code more unreadable (wait, there is still ...), also want to change the Minify class / lib to minimize and cache pseudo-css-php, then you need to “crack” source as follows:

  • lib / Minify / Controller / Base.php: 135, change to:

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

  • lib / Minify / Controller / MinApp.php: 75, change to:

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

  • lib / Minify / Source.php, change a few things:

Add one variable as the PHP flag, in, after line 41, maybe

 /** * @var bool */ public $isPHP = FALSE; 

In the same file, on line: 67, add the condition:

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

Finally, replace the getContent () function with:

 public function getContent() { if($this->isPHP) { include($this->filepath); } else { $content = (null !== $this->filepath) ? file_get_contents($this->filepath) : ((null !== $this->_content) ? $this->_content : call_user_func($this->_getContentFunc, $this->_id) ); } // remove UTF-8 BOM if present return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) ? substr($content, 3) : $content; } 
  • You also need to modify your common.php file in Minify, so now your common.php should look like this:

You need to put the whole stylesheet in a string and assign it to the variable $content

 <?php //$style = ''; $bodyStyle = 'body { background-color: #000; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #fff; }'; // I assumed you are about proccesing something here.. // ... // Then you merged all style into one string, and output it as css file // $style = $bodyStyle + $otherStyle + $whateverStyle; // header('Content-Type: text/css'); // echo $style; $content = $bodyStyle + $otherStyle + $whateverStyle; ?> 
0
source

Yes, there is one, and it works very well:

https://github.com/c9s/pecl-cssmin

The API is pretty simple:

 <?php echo cssmin("body { .... } .rule { } .rule2 { color: #fff; }"); 
0
source

Source: https://habr.com/ru/post/896057/


All Articles