Exemplo n.º 1
0
/**
 * Process an image with Smush.it.
 *
 * Returns an array of the $file $results.
 *
 * @param   string $file            Full absolute path to the image file
 * @returns array
 */
function wp_smushit($file)
{
    // dont't run on localhost
    if ('127.0.0.1' == $_SERVER['SERVER_ADDR']) {
        return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
    }
    // canonicalize path
    $file_path = realpath($file);
    // check that the file exists
    if (FALSE === file_exists($file_path) || FALSE === is_file($file_path)) {
        $msg = sprintf(__("Could not {$file_path} find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN), $file_path);
        return array($file, $msg);
    }
    // check that the file is writable
    if (FALSE === is_writable($file_path)) {
        $msg = sprintf(__("<span class='code'>%s</span> is not writable", WP_SMUSHIT_DOMAIN), $file_path);
        return array($file, $msg);
    }
    // check that the file is within the WP_CONTENT_DIR
    if (0 !== stripos(realpath($file_path), realpath(WP_CONTENT_DIR))) {
        $msg = sprintf(__("<span class='code'>%s</span> must be within the content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN), htmlentities($file_path), WP_CONTENT_DIR);
        return array($file, $msg);
    }
    // determine the public URL
    $file_url = str_replace(WP_CONTENT_DIR, WP_CONTENT_URL, $file);
    $data = wp_smushit_post($file_url);
    if (FALSE === $data) {
        return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
    }
    // make sure the response looks like JSON -- added 2008-12-19 when
    // Smush.it was returning PHP warnings before the JSON output
    if (strpos(trim($data), '{') != 0) {
        return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
    }
    // read the JSON response
    if (function_exists('json_decode')) {
        $data = json_decode($data);
    } else {
        $json = new Services_JSON();
        $data = $json->decode($data);
    }
    if (-1 === intval($data->dest_size)) {
        return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
    }
    if (!$data->dest) {
        $err = $data->error ? 'Smush.it error: ' . $data->error : 'unknown error';
        return array($file, __($err, WP_SMUSHIT_DOMAIN));
    }
    $processed_url = $data->dest;
    // The smush.it web service does not append the domain;
    // smushit.com web service does
    if (0 !== stripos($processed_url, 'http://')) {
        $processed_url = SMUSHIT_BASE_URL . $processed_url;
    }
    $temp_file = download_url($processed_url);
    if (is_wp_error($temp_file)) {
        @unlink($tmp);
        $results_msg = sprintf(__("Error downloading file (%s)", WP_SMUSHIT_DOMAIN), $temp_file->get_error_message());
        return array($file, $results_msg);
    }
    @rename($temp_file, $file_path);
    $savings = intval($data->src_size) - intval($data->dest_size);
    $savings_str = wp_smushit_format_bytes($savings, 1);
    $savings_str = str_replace(' ', '&nbsp;', $savings_str);
    $results_msg = sprintf(__("Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN), $data->percent, $savings_str);
    return array($file, $results_msg);
}
Exemplo n.º 2
0
/**
 * Process an image with Smush.it.
 *
 * Returns an array of the $file $results.
 *
 * @param   string $file            Full absolute path to the image file
 * @param   string $file_url        Optional full URL to the image file
 * @returns array
 */
function wp_smushit($file, $file_url = null)
{
    // don't run on localhost, IPv4 and IPv6 checks
    // if( in_array($_SERVER['SERVER_ADDR'], array('127.0.0.1', '::1')) )
    //	return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
    // canonicalize path - disabled 2011-02-1 troubleshooting 'Could not find...' errors.
    // From the PHP docs: "The running script must have executable permissions on
    // all directories in the hierarchy, otherwise realpath() will return FALSE."
    // $file_path = realpath($file);
    $file_path = $file;
    // check that the file exists
    if (FALSE === file_exists($file_path) || FALSE === is_file($file_path)) {
        $msg = sprintf(__("Could not find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN), $file_path);
        return array($file, $msg);
    }
    // check that the file is writable
    if (FALSE === is_writable($file_path)) {
        $msg = sprintf(__("<span class='code'>%s</span> is not writable", WP_SMUSHIT_DOMAIN), $file_path);
        return array($file, $msg);
    }
    // check that the file is within the WP_CONTENT_DIR
    $upload_dir = wp_upload_dir();
    $wp_upload_dir = $upload_dir['basedir'];
    $wp_upload_url = $upload_dir['baseurl'];
    if (0 !== stripos(realpath($file_path), realpath(ABSPATH))) {
        $msg = sprintf(__("<span class='code'>%s</span> must be within the content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN), htmlentities($file_path), $wp_upload_dir);
        return array($file, $msg);
    }
    if (!$file_url) {
        // determine the public URL
        $file_url = str_replace($wp_upload_dir, $wp_upload_url, $file);
    }
    $data = wp_smushit_post($file_url);
    if (FALSE === $data) {
        return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
    }
    // make sure the response looks like JSON -- added 2008-12-19 when
    // Smush.it was returning PHP warnings before the JSON output
    if (strpos(trim($data), '{') != 0) {
        return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
    }
    // read the JSON response
    if (function_exists('json_decode')) {
        $data = json_decode($data);
    } else {
        $json = new Services_JSON();
        $data = $json->decode($data);
    }
    if (-1 === intval($data->dest_size)) {
        return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
    }
    if (!$data->dest) {
        $err = $data->error ? 'Smush.it error: ' . $data->error : 'unknown error';
        $err .= " while processing <span class='code'>{$file_url}</span> (<span class='code'>{$file_path}</span>)";
        return array($file, __($err, WP_SMUSHIT_DOMAIN));
    }
    $processed_url = $data->dest;
    // The smush.it web service does not append the domain;
    // smushit.com web service does
    if (0 !== stripos($processed_url, 'http://')) {
        $processed_url = SMUSHIT_BASE_URL . $processed_url;
    }
    $temp_file = download_url($processed_url);
    if (is_wp_error($temp_file)) {
        @unlink($tmp);
        $results_msg = sprintf(__("Error downloading file (%s)", WP_SMUSHIT_DOMAIN), $temp_file->get_error_message());
        return array($file, $results_msg);
    }
    @rename($temp_file, $file_path);
    $savings = intval($data->src_size) - intval($data->dest_size);
    $savings_str = wp_smushit_format_bytes($savings, 1);
    $savings_str = str_replace(' ', '&nbsp;', $savings_str);
    $results_msg = sprintf(__("Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN), $data->percent, $savings_str);
    return array($file, $results_msg);
}