function compress_css()
{
    // make these variables available locally
    global $file_selector;
    global $file_props;
    // first, get the css that was sent, referenced or uploaded
    $cssfile = get_sent_css();
    // check if the user wants to see statistics
    if ($_REQUEST['opt_output_stats']) {
        // save the size of the code
        $start_size = strlen($cssfile);
        // save the current time
        $start = explode(' ', microtime());
    }
    // check if no css was found
    if (!$cssfile) {
        // just die
        exit("/* You didn't upload anything or the stylesheet is empty - Ro" . "bson */");
    }
    // temporarily change semicolons in web links
    $cssfile = str_replace('://', '[!semi-colon!]//', $cssfile);
    // remove html and css comments
    kill_comments($cssfile);
    // trim whitespace from the start and end
    $cssfile = trim($cssfile);
    // turn all rgb values into hex
    if ($_REQUEST['opt_rgb2hex']) {
        rgb2hex($cssfile);
    }
    // shorten colours
    if ($_REQUEST['opt_colours2hex']) {
        long_colours_to_short_hex($cssfile);
    }
    if ($_REQUEST['opt_hex2colours']) {
        long_hex_to_short_colours($cssfile);
    }
    // remove any extra measurements that aren't needed
    if ($_REQUEST['opt_remove_zeros']) {
        remove_zero_measurements($cssfile);
    }
    // seperate into selectors and properties
    sort_css($cssfile);
    // change font weight text into numbers
    if ($_REQUEST['opt_text_weights_to_numbers']) {
        font_weight_text_to_numbers($cssfile);
    }
    // check if any selectors are used twice and combine the properties
    if ($_REQUEST['opt_combine_identical_selectors']) {
        combine_identical_selectors();
    }
    // remove any properties which are declared twice in one rule
    if ($_REQUEST['opt_remove_overwritten_properties']) {
        remove_overwritten_properties();
    }
    // check if properties should be combined
    if ($_REQUEST['opt_combine_props_list']) {
        // for each rule in the file
        for ($n = 0; $n < count($file_props); $n++) {
            // attempt to combine the different parts
            combine_props_list($file_props[$n]);
        }
    }
    // for each rule in the file
    for ($n = 0; $n < count($file_props); $n++) {
        // run all the individual functions to reduce their size
        array_walk($file_props[$n], 'reduce_prop');
    }
    // remove all the properties that were blanked out earlier
    remove_empty_rules();
    // check if any rules are the same as other ones and remove the first ones
    if ($_REQUEST['opt_combine_identical_rules']) {
        combine_identical_rules();
    }
    // one final run through to remove all unnecessary parts of the arrays
    remove_empty_rules();
    $output = create_output();
    // turn back colons
    $output = str_replace('[!semi-colon!]//', '://', $output);
    $output = stripslashes($output);
    // check if the user wants to view stats
    if ($_REQUEST['opt_output_stats']) {
        echo '<h1>Statistics</h1><ul>';
        echo '<li>Original size: ' . round($start_size / 1024, 2) . ' kB (' . number_format($start_size) . ' B)</li>';
        echo '<li>Final size: ' . round(strlen(strip_tags($output)) / 1024, 2) . ' kB (' . number_format(strlen(strip_tags($output))) . ' B)</li>';
        echo '<li>Saved: ' . round(($start_size - strlen(strip_tags($output))) / 1024, 2) . ' kB (' . number_format($start_size - strlen(strip_tags($output))) . ' B)</li>';
        echo '<li>Reduction: ' . round(100 - strlen(strip_tags($output)) / $start_size * 100, 2) . '%</li>';
        echo '</ul>';
        $finish = explode(' ', microtime());
        // work out the differences between the times
        $seconds = $finish[1] - $start[1];
        $miliseconds = $finish[0] - $start[0];
        $duration = round($seconds + $miliseconds, 5);
        echo '<ul>';
        echo '<li>Duration: ' . $duration . ' seconds</li>';
        echo '</ul>';
        echo '<ul>';
        echo '<li>Rules: ' . count($file_selector) . '</li>';
        for ($n = 0; $n < count($file_selector); $n++) {
            $selectors += count($file_selector[$n]);
        }
        for ($n = 0; $n < count($file_props); $n++) {
            $props += count($file_props[$n]);
        }
        echo '<li>Selectors: ' . $selectors . '</li>';
        echo '<li>Properties: ' . $props . '</li>';
        echo '</ul>';
        echo '<h1>CSS</h1>';
    }
    if ($_REQUEST['upload_link']) {
        $_REQUEST['upload_style'] = NULL;
        if (!$_POST['upload_link']) {
            $_POST = $_GET;
        }
        foreach ($_POST as $key => $value) {
            if ($value) {
                if (!$url) {
                    $url .= '?' . $key . '=' . $value;
                } else {
                    $url .= '&' . $key . '=' . $value;
                }
            }
        }
        echo '<ul><li><a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $url . '">Link to this output</a>.</ul>';
    }
    echo $output;
}
Beispiel #2
0
function compress_css($cssfile)
{
    // make these variables available locally
    global $file_selector;
    global $file_props;
    global $cssCompressor;
    // first, get the css that was sent, referenced or uploaded
    // save the size of the code
    $start_size = strlen($cssfile);
    // save the current time
    $start = explode(' ', microtime());
    // temporarily change semicolons in web links
    $cssfile = str_replace('://', '[!semi-colon!]//', $cssfile);
    // remove html and css comments
    kill_comments($cssfile);
    // trim whitespace from the start and end
    $cssfile = trim($cssfile);
    // turn all rgb values into hex
    if ($cssCompressor['opt_rgb2hex']) {
        rgb2hex($cssfile);
    }
    // shorten colours
    if ($cssCompressor['opt_colours2hex']) {
        long_colours_to_short_hex($cssfile);
    }
    if ($cssCompressor['opt_hex2colours']) {
        long_hex_to_short_colours($cssfile);
    }
    // remove any extra measurements that aren't needed
    if ($cssCompressor['opt_remove_zeros']) {
        remove_zero_measurements($cssfile);
    }
    // seperate into selectors and properties
    sort_css($cssfile);
    // change font weight text into numbers
    if ($cssCompressor['opt_text_weights_to_numbers']) {
        font_weight_text_to_numbers($cssfile);
    }
    // check if any selectors are used twice and combine the properties
    if ($cssCompressor['opt_combine_identical_selectors']) {
        combine_identical_selectors();
    }
    // remove any properties which are declared twice in one rule
    if ($cssCompressor['opt_remove_overwritten_properties']) {
        remove_overwritten_properties();
    }
    // check if properties should be combined
    if ($cssCompressor['opt_combine_props_list']) {
        // for each rule in the file
        for ($n = 0; $n < count($file_props); $n++) {
            // attempt to combine the different parts
            combine_props_list($file_props[$n]);
        }
    }
    // for each rule in the file
    for ($n = 0; $n < count($file_props); $n++) {
        // run all the individual functions to reduce their size
        array_walk($file_props[$n], 'reduce_prop', $cssCompressor);
    }
    // remove all the properties that were blanked out earlier
    remove_empty_rules();
    // check if any rules are the same as other ones and remove the first ones
    if ($cssCompressor['opt_combine_identical_rules']) {
        combine_identical_rules();
    }
    // one final run through to remove all unnecessary parts of the arrays
    remove_empty_rules();
    $output = create_output();
    // turn back colons
    $output = str_replace('[!semi-colon!]//', '://', $output);
    $output = stripslashes($output);
    // gather statistics
    $stats['original_size'] = round($start_size / 1024, 2) . ' kB';
    $stats['final_size'] = round(strlen(strip_tags($output)) / 1024, 2) . ' kB';
    $stats['reduction'] = round(100 - strlen(strip_tags($output)) / $start_size * 100, 2) . '%';
    $finish = explode(' ', microtime());
    // work out the differences between the times
    $seconds = $finish[1] - $start[1];
    $miliseconds = $finish[0] - $start[0];
    $stats['duration'] = round($seconds + $miliseconds, 5);
    $stats['num_rules'] = count($file_selector);
    for ($n = 0; $n < count($file_selector); $n++) {
        $stats['num_selectors'] += count($file_selector[$n]);
    }
    for ($n = 0; $n < count($file_props); $n++) {
        $stats['num_props'] += count($file_props[$n]);
    }
    return array($output, $stats);
}