function wr2x_generate_images($meta) { require 'wr2x_vt_resize.php'; global $_wp_additional_image_sizes; $sizes = wr2x_get_image_sizes(); if (!isset($meta['file'])) { return; } $originalfile = $meta['file']; $uploads = wp_upload_dir(); $pathinfo = pathinfo($originalfile); $original_basename = $pathinfo['basename']; $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname']; $ignore = wr2x_getoption("ignore_sizes", "wr2x_basics", array()); $issue = false; $id = wr2x_get_attachment_id($meta['file']); wr2x_log("* GENERATE RETINA FOR ATTACHMENT '{$meta['file']}'"); wr2x_log("Full-Size is {$original_basename}."); foreach ($sizes as $name => $attr) { $normal_file = ""; if (in_array($name, $ignore)) { wr2x_log("Retina for {$name} ignored (settings)."); continue; } // Is the file related to this size there? $pathinfo = null; $retina_file = null; if (isset($meta['sizes'][$name]) && isset($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']; } if ($retina_file && file_exists($retina_file)) { wr2x_log("Base for {$name} is '{$normal_file}'."); wr2x_log("Retina for {$name} already exists: '{$retina_file}'."); continue; } if ($retina_file) { $originalfile = trailingslashit($pathinfo['dirname']) . $original_basename; if (!file_exists($originalfile)) { wr2x_log("[ERROR] Original file '{$originalfile}' cannot be found."); return $meta; } // Maybe that new image is exactly the size of the original image. // In that case, let's make a copy of it. if ($meta['sizes'][$name]['width'] * 2 == $meta['width'] && $meta['sizes'][$name]['height'] * 2 == $meta['height']) { copy($originalfile, $retina_file); wr2x_log("Retina for {$name} created: '{$retina_file}' (as a copy of the full-size)."); } else { if (wr2x_are_dimensions_ok($meta['width'], $meta['height'], $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2)) { // Change proposed by Nicscott01, slighlty modified by Jordy (+isset) // (https://wordpress.org/support/topic/issue-with-crop-position?replies=4#post-6200271) $crop = isset($_wp_additional_image_sizes[$name]) ? $_wp_additional_image_sizes[$name]['crop'] : true; $customCrop = null; // Support for Manual Image Crop // If the size of the image was manually cropped, let's keep it. if (class_exists('ManualImageCrop') && isset($meta['micSelectedArea']) && isset($meta['micSelectedArea'][$name]) && isset($meta['micSelectedArea'][$name]['scale'])) { $customCrop = $meta['micSelectedArea'][$name]; } $image = wr2x_vt_resize($originalfile, $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2, $crop, $retina_file, $customCrop); } } if (!file_exists($retina_file)) { wr2x_log("[ERROR] Retina for {$name} could not be created. Full-Size is " . $meta['width'] . "x" . $meta['height'] . " but Retina requires a file of at least " . $meta['sizes'][$name]['width'] * 2 . "x" . $meta['sizes'][$name]['height'] * 2 . "."); $issue = true; } else { do_action('wr2x_retina_file_added', $id, $retina_file); wr2x_log("Retina for {$name} created: '{$retina_file}'."); } } else { if (empty($normal_file)) { wr2x_log("[ERROR] Base file for '{$name}' does not exist."); } else { wr2x_log("[ERROR] Base file for '{$name}' cannot be found here: '{$normal_file}'."); } } } // Checks attachment ID + issues if (!$id) { return $meta; } if ($issue) { wr2x_add_issue($id); } else { wr2x_remove_issue($id); } return $meta; }
function wr2x_generate_images($meta) { require 'wr2x_vt_resize.php'; $sizes = wr2x_get_image_sizes(); $originalfile = $meta['file']; $uploads = wp_upload_dir(); $pathinfo = pathinfo($originalfile); $original_basename = $pathinfo['basename']; $basepath = trailingslashit($uploads['basedir']) . $pathinfo['dirname']; $ignore = wr2x_getoption("ignore_sizes", "wr2x_basics", array()); $issue = false; $id = wr2x_get_attachment_id($meta['file']); wr2x_log("** RETINA INFO FOR ATTACHMENT '{$meta['file']}' **"); wr2x_log("- Original: {$original_basename}"); foreach ($sizes as $name => $attr) { if (in_array($name, $ignore)) { wr2x_log("- {$name} => IGNORED"); continue; } // Is the file related to this size there? $pathinfo = null; $retina_file = null; if (isset($meta['sizes'][$name]) && isset($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']; } if ($retina_file && file_exists($retina_file)) { wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => EXISTS"); continue; } if ($retina_file) { $originalfile = trailingslashit($pathinfo['dirname']) . $original_basename; if (!file_exists($originalfile)) { wr2x_log("- The Original File '{$originalfile}' cannot be found."); return $meta; } // Maybe that new image is exactly the size of the original image. // In that case, let's make a copy of it. if ($meta['sizes'][$name]['width'] * 2 == $meta['width'] && $meta['sizes'][$name]['height'] * 2 == $meta['height']) { wr2x_log("- {$name}: {$originalfile} -> {$retina_file} => COPY"); copy($originalfile, $retina_file); } else { if ($meta['sizes'][$name]['width'] * 2 <= $meta['width'] && $meta['sizes'][$name]['height'] * 2 <= $meta['height']) { $image = wr2x_vt_resize($originalfile, $meta['sizes'][$name]['width'] * 2, $meta['sizes'][$name]['height'] * 2, $retina_file); } } if (!file_exists($retina_file)) { wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => FAIL"); $issue = true; } else { do_action('wr2x_retina_file_added', $id, $retina_file); wr2x_log("- {$name}: {$normal_file} -> {$retina_file} => RESIZE"); } } else { wr2x_log("- {$name} => MISSING"); } } // Checks attachment ID + issues if (!$id) { return $meta; } if ($issue) { wr2x_add_issue($id); } else { wr2x_remove_issue($id); } return $meta; }
function wr2x_wp_ajax_wr2x_upload() { $tmpfname = wr2x_check_get_ajax_uploaded_file(); $attachmentId = (int) $_POST['attachmentId']; $meta = wp_get_attachment_metadata($attachmentId); $current_file = get_attached_file($attachmentId); $pathinfo = pathinfo($current_file); $basepath = $pathinfo['dirname']; $retinafile = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension']; if (file_exists($retinafile)) { unlink($retinafile); } // Insert the new file and delete the temporary one list($width, $height) = getimagesize($tmpfname); if (!wr2x_are_dimensions_ok($width, $height, $meta['width'] * 2, $meta['height'] * 2)) { echo json_encode(array('success' => false, 'message' => "This image has a resolution of {$width}×{$height} but your Full Size image requires a retina image of at least " . $meta['width'] * 2 . "x" . $meta['height'] * 2 . ".")); die; } require 'wr2x_vt_resize.php'; wr2x_vt_resize($tmpfname, $meta['width'] * 2, $meta['height'] * 2, null, $retinafile); chmod($retinafile, 0644); unlink($tmpfname); // Get the results $info = wr2x_retina_info($attachmentId); wr2x_update_issue_status($attachmentId); $results[$attachmentId] = wpr2x_html_get_basic_retina_info_full($attachmentId, $info); echo json_encode(array('success' => true, 'results' => $results, 'message' => __("Uploaded successfully.", 'wp-retina-2x'))); die; }
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; }