resize( $width, $height, $crop ) ) ) return false; $resized_file = $editor->set_quality(79); $resized_file = $editor->generate_filename( 'w' . $width . 'h' . $height ); $resized_file = $editor->save($resized_file); if ( ! is_wp_error( $resized_file ) ) { $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path'] ); $img_url = $upload_url . $resized_rel_path; } else { return false; } } } // Okay, leave the ship. if ( true === $upscale ) remove_filter( 'image_resize_dimensions', array( $this, 'aq_upscale' ) ); // Return the output. if ( $single ) { // str return. $image = $img_url; } else { // array return. $image = array ( 0 => $img_url, 1 => $dst_w, 2 => $dst_h ); } // RETINA Support ---------------------------------------------------------------> //@2x image url $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}@2x.{$ext}"; //check if retina image exists if(file_exists($destfilename) && getimagesize($destfilename)) { // already exists, do nothing } else { // doesnt exist, lets create it $retina_width = $width*2; $retina_height = $height*2; $editor = wp_get_image_editor($img_path); $editor->set_quality(79); $editor->resize( $retina_width, $retina_height, $crop ); $filename = $editor->generate_filename( 'w' . $width . 'h' . $height . '@2x' ); $editor = $editor->save($filename); } return $image; } /** * Callback to overwrite WP computing of thumbnail measures */ function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) { if ( ! $crop ) return null; // Let the wordpress default function handle this. // Here is the point we allow to use larger image size than the original one. $aspect_ratio = $orig_w / $orig_h; $new_w = $dest_w; $new_h = $dest_h; if ( ! $new_w ) { $new_w = intval( $new_h * $aspect_ratio ); } if ( ! $new_h ) { $new_h = intval( $new_w / $aspect_ratio ); } $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); $crop_w = round( $new_w / $size_ratio ); $crop_h = round( $new_h / $size_ratio ); $s_x = floor( ( $orig_w - $crop_w ) / 2 ); $s_y = floor( ( $orig_h - $crop_h ) / 2 ); return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); } } } if(!function_exists('ut_resize')) { /** * This is just a tiny wrapper function for the class above so that there is no * need to change any code in your own WP themes. Usage is still the same :) */ function ut_resize( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) { $ut_resize = UT_Resize::getInstance(); return $ut_resize->process( $url, $width, $height, $crop, $single, $upscale ); } }