예제 #1
0
function wr2x_delete_images($meta)
{
    if (!wr2x_is_image_meta($meta)) {
        return $meta;
    }
    $sizes = $meta['sizes'];
    if (!$sizes || !is_array($sizes)) {
        return $meta;
    }
    wr2x_log("* DELETE RETINA FOR ATTACHMENT '{$meta['file']}'");
    $originalfile = $meta['file'];
    $id = wr2x_get_attachment_id($originalfile);
    $pathinfo = pathinfo($originalfile);
    $uploads = wp_upload_dir();
    $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname'];
    foreach ($sizes as $name => $attr) {
        $pathinfo = pathinfo($attr['file']);
        $retina_file = $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
        if (file_exists(trailingslashit($basepath) . $retina_file)) {
            $fullpath = trailingslashit($basepath) . $retina_file;
            unlink($fullpath);
            do_action('wr2x_retina_file_removed', $id, $retina_file);
            wr2x_log("Deleted '{$fullpath}'.");
        }
    }
    // Remove full-size if there is any
    $pathinfo = pathinfo($originalfile);
    $retina_file = $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
    if (file_exists(trailingslashit($basepath) . $retina_file)) {
        $fullpath = trailingslashit($basepath) . $retina_file;
        unlink($fullpath);
        do_action('wr2x_retina_file_removed', $id, $retina_file);
        wr2x_log("Deleted '{$fullpath}'.");
    }
    return $meta;
}
예제 #2
0
function wr2x_wp_ajax_wr2x_replace()
{
    $tmpfname = wr2x_check_get_ajax_uploaded_file();
    $attachmentId = (int) $_POST['attachmentId'];
    $meta = wp_get_attachment_metadata($attachmentId);
    $current_file = get_attached_file($attachmentId);
    wr2x_delete_attachment($attachmentId);
    $pathinfo = pathinfo($current_file);
    $basepath = $pathinfo['dirname'];
    // Let's clean everything first
    if (wp_attachment_is_image($attachmentId)) {
        $sizes = wr2x_get_image_sizes();
        foreach ($sizes as $name => $attr) {
            if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file']) && file_exists(trailingslashit($basepath) . $meta['sizes'][$name]['file'])) {
                $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file'];
                $pathinfo = pathinfo($normal_file);
                $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
                // Test if the file exists and if it is actually a file (and not a dir)
                // Some old WordPress Media Library are sometimes broken and link to directories
                if (file_exists($normal_file) && is_file($normal_file)) {
                    unlink($normal_file);
                }
                if (file_exists($retina_file) && is_file($retina_file)) {
                    unlink($retina_file);
                }
            }
        }
    }
    if (file_exists($current_file)) {
        unlink($current_file);
    }
    // Insert the new file and delete the temporary one
    rename($tmpfname, $current_file);
    chmod($current_file, 0644);
    // Generate the images
    wp_update_attachment_metadata($attachmentId, wp_generate_attachment_metadata($attachmentId, $current_file));
    $meta = wp_get_attachment_metadata($attachmentId);
    wr2x_generate_images($meta);
    // Get the results
    $info = wr2x_retina_info($attachmentId);
    $results[$attachmentId] = wpr2x_html_get_basic_retina_info($attachmentId, $info);
    echo json_encode(array('success' => true, 'results' => $results, 'message' => __("Replaced successfully.", 'wp-retina-2x')));
    die;
}
예제 #3
0
function ewww_image_optimizer_resize_upload($file)
{
    // parts adapted from Imsanity (THANKS Jason!)
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    if (!$file) {
        return false;
    }
    //	ewwwio_debug_message( print_r( $_SERVER, true ) );
    if (!empty($_REQUEST['post_id']) || !empty($_REQUEST['action']) && $_REQUEST['action'] === 'upload-attachment' || !empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'media-new.php')) {
        $maxwidth = ewww_image_optimizer_get_option('ewww_image_optimizer_maxmediawidth');
        $maxheight = ewww_image_optimizer_get_option('ewww_image_optimizer_maxmediaheight');
        ewwwio_debug_message('resizing image from media library or attached to post');
    } else {
        $maxwidth = ewww_image_optimizer_get_option('ewww_image_optimizer_maxotherwidth');
        $maxheight = ewww_image_optimizer_get_option('ewww_image_optimizer_maxotherheight');
        ewwwio_debug_message('resizing images from somewhere else');
    }
    // allow other developers to modify the dimensions to their liking based on whatever parameters they might choose
    list($maxwidth, $maxheight) = apply_filters('ewww_image_optimizer_resize_dimensions', array($maxwidth, $maxheight));
    //check that options are not == 0
    if ($maxwidth == 0 && $maxheight == 0) {
        return false;
    }
    //check file type
    $type = ewww_image_optimizer_mimetype($file, 'i');
    if (strpos($type, 'image') === FALSE) {
        ewwwio_debug_message('not an image, cannot resize');
        return false;
    }
    //check file size (dimensions)
    list($oldwidth, $oldheight) = getimagesize($file);
    if ($oldwidth <= $maxwidth && $oldheight <= $maxheight) {
        ewwwio_debug_message('image too small for resizing');
        return false;
    }
    list($newwidth, $newheight) = wp_constrain_dimensions($oldwidth, $oldheight, $maxwidth, $maxheight);
    if (!function_exists('wp_get_image_editor')) {
        ewwwio_debug_message('no image editor function');
        return false;
    }
    remove_filter('wp_image_editors', 'ewww_image_optimizer_load_editor', 60);
    $editor = wp_get_image_editor($file);
    if (is_wp_error($editor)) {
        ewwwio_debug_message('could not get image editor');
        return false;
    }
    if (function_exists('exif_read_data') && $type === 'image/jpeg') {
        $exif = @exif_read_data($file);
        if (is_array($exif) && array_key_exists('Orientation', $exif)) {
            $orientation = $exif['Orientation'];
            switch ($orientation) {
                case 3:
                    $editor->rotate(180);
                    break;
                case 6:
                    $editor->rotate(-90);
                    break;
                case 8:
                    $editor->rotate(90);
                    break;
            }
        }
    }
    $resized_image = $editor->resize($newwidth, $newheight);
    if (is_wp_error($resized_image)) {
        ewwwio_debug_message('error during resizing');
        return false;
    }
    $new_file = $editor->generate_filename('tmp');
    $orig_size = filesize($file);
    $saved = $editor->save($new_file);
    if (is_wp_error($saved)) {
        ewwwio_debug_message('error saving resized image');
    }
    add_filter('wp_image_editors', 'ewww_image_optimizer_load_editor', 60);
    $new_size = ewww_image_optimizer_filesize($new_file);
    if ($new_size && $new_size < $orig_size) {
        // generate a retina file from the full original if they have WP Retina 2x Pro
        if (function_exists('wr2x_is_pro') && wr2x_is_pro()) {
            $full_size_needed = wr2x_getoption("full_size", "wr2x_basics", false);
            if ($full_size_needed) {
                // Is the file related to this size there?
                $retina_file = '';
                $pathinfo = pathinfo($file);
                $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
                if ($retina_file && !file_exists($retina_file) && wr2x_are_dimensions_ok($oldwidth, $oldheight, $newwidth * 2, $newheight * 2)) {
                    $image = wr2x_vt_resize($file, $newwidth * 2, $newheight * 2, false, $retina_file);
                }
            }
        }
        rename($new_file, $file);
        // store info on the current image for future reference
        global $wpdb;
        $already_optimized = ewww_image_optimizer_find_already_optimized($file);
        // if the original file has never been optimized, then just update the record that was created with the proper filename (because the resized file has usually been optimized)
        if (empty($already_optimized)) {
            $tmp_exists = $wpdb->update($wpdb->ewwwio_images, array('path' => $file, 'orig_size' => $orig_size), array('path' => $new_file));
            // if the tmp file didn't get optimized (and it shouldn't), then just insert a dummy record to be updated shortly
            if (!$tmp_exists) {
                $wpdb->insert($wpdb->ewwwio_images, array('path' => $file, 'orig_size' => $orig_size));
            }
            // otherwise, we delete the record created from optimizing the resized file, and update our records for the original file
        } else {
            $temp_optimized = ewww_image_optimizer_find_already_optimized($new_file);
            if (is_array($temp_optimized) && !empty($temp_optimized['id'])) {
                $wpdb->delete($wpdb->ewwwio_images, array('id' => $temp_optimized['id']), array('%d'));
            }
            // should not need this, as the image will get optimized shortly
            //ewww_image_optimizer_update_table( $file, $new_size, $orig_size );
        }
        return array($newwidth, $newheight);
    }
    if (file_exists($new_file)) {
        unlink($new_file);
    }
    return false;
}
예제 #4
0
function wr2x_delete_images($meta)
{
    if (!isset($meta['sizes'])) {
        return $meta;
    }
    $sizes = $meta['sizes'];
    if (!$sizes || !is_array($sizes)) {
        return $meta;
    }
    $originalfile = $meta['file'];
    $id = wr2x_get_attachment_id($originalfile);
    $pathinfo = pathinfo($originalfile);
    $uploads = wp_upload_dir();
    $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname'];
    foreach ($sizes as $name => $attr) {
        $pathinfo = pathinfo($attr['file']);
        $retina_file = $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
        if (file_exists(trailingslashit($basepath) . $retina_file)) {
            unlink(trailingslashit($basepath) . $retina_file);
            do_action('wr2x_retina_file_removed', $id, $retina_file);
        }
    }
    return $meta;
}
예제 #5
0
function wr2x_wp_ajax_wr2x_replace()
{
    if (!current_user_can('upload_files')) {
        echo json_encode(array('success' => false, 'message' => __("You do not have permission to upload files.", 'wp-retina-2x')));
        die;
    }
    $data = $_POST['data'];
    // Create the file as a TMP
    $tmpfname = tempnam(sys_get_temp_dir(), "wpx_");
    if ($tmpfname == FALSE) {
        $tmpdir = sys_get_temp_dir();
        if (!is_writable($tmpdir)) {
            echo json_encode(array('success' => false, 'message' => __("You don't have the rights to use a temporary directory.", 'wp-retina-2x')));
        } else {
            echo json_encode(array('success' => false, 'message' => __("The temporary directory could not be created.", 'wp-retina-2x')));
        }
        die;
    }
    $handle = fopen($tmpfname, "w");
    fwrite($handle, base64_decode($data));
    fclose($handle);
    // Check if it is an image
    $file_info = getimagesize($tmpfname);
    if (empty($file_info)) {
        unlink($tmpfname);
        echo json_encode(array('success' => false, 'message' => __("The file is not an image or the upload went wrong.", 'wp-retina-2x')));
        die;
    }
    $filedata = wp_check_filetype_and_ext($tmpfname, $_POST['filename']);
    if ($filedata["ext"] == "") {
        unlink($current_file);
        echo json_encode(array('success' => false, 'message' => __("You cannot use this file (wrong extension? wrong type?).", 'wp-retina-2x')));
        die;
    }
    $attachmentId = (int) $_POST['attachmentId'];
    $meta = wp_get_attachment_metadata($attachmentId);
    $current_file = get_attached_file($attachmentId);
    wr2x_delete_attachment($attachmentId);
    $pathinfo = pathinfo($current_file);
    $basepath = $pathinfo['dirname'];
    // Let's clean everything first
    if (wp_attachment_is_image($attachmentId)) {
        $sizes = wr2x_get_image_sizes();
        foreach ($sizes as $name => $attr) {
            if (isset($meta['sizes'][$name]) && isset($meta['sizes'][$name]['file']) && file_exists(trailingslashit($basepath) . $meta['sizes'][$name]['file'])) {
                $normal_file = trailingslashit($basepath) . $meta['sizes'][$name]['file'];
                $pathinfo = pathinfo($normal_file);
                $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension'];
                // Test if the file exists and if it is actually a file (and not a dir)
                // Some old WordPress Media Library are sometimes broken and link to directories
                if (file_exists($normal_file) && is_file($normal_file)) {
                    unlink($normal_file);
                }
                if (file_exists($retina_file) && is_file($retina_file)) {
                    unlink($retina_file);
                }
            }
        }
    }
    if (file_exists($current_file)) {
        unlink($current_file);
    }
    // Insert the new file and delete the temporary one
    rename($tmpfname, $current_file);
    chmod($current_file, 0644);
    // Generate the images
    wp_update_attachment_metadata($attachmentId, wp_generate_attachment_metadata($attachmentId, $current_file));
    $meta = wp_get_attachment_metadata($attachmentId);
    wr2x_generate_images($meta);
    // Get the results
    $info = wr2x_retina_info($attachmentId);
    $results[$attachmentId] = $info;
    echo json_encode(array('success' => true, 'results' => $results, 'message' => __("Replaced successfully.", 'wp-retina-2x')));
    die;
}