/** * Calculates the new dimensions for a downsampled image. * * @since 2.0.0 * @deprecated 3.0.0 * @deprecated Use nxt_constrain_dimensions() * * @param int $width Current width of the image * @param int $height Current height of the image * @param int $wmax Maximum wanted width * @param int $hmax Maximum wanted height * @return mixed Array(height,width) of shrunk dimensions. */ function nxt_shrink_dimensions($width, $height, $wmax = 128, $hmax = 96) { _deprecated_function(__FUNCTION__, '3.0', 'nxt_constrain_dimensions()'); return nxt_constrain_dimensions($width, $height, $wmax, $hmax); }
/** * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. * * @since 2.9.0 * @uses nxt_constrain_dimensions() This function passes the widths and the heights. * * @param int $example_width The width of an example embed. * @param int $example_height The height of an example embed. * @param int $max_width The maximum allowed width. * @param int $max_height The maximum allowed height. * @return array The maximum possible width and height based on the example ratio. */ function nxt_expand_dimensions($example_width, $example_height, $max_width, $max_height) { $example_width = (int) $example_width; $example_height = (int) $example_height; $max_width = (int) $max_width; $max_height = (int) $max_height; return nxt_constrain_dimensions($example_width * 1000000, $example_height * 1000000, $max_width, $max_height); }
function vt_resize($attach_id = null, $img_url = null, $width, $height, $crop = false) { // this is an attachment, so we have the ID if ($attach_id) { $image_src = nxt_get_attachment_image_src($attach_id, 'full'); $file_path = get_attached_file($attach_id); // this is not an attachment, let's use the image url } else { if ($img_url) { $file_path = parse_url($img_url); $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path['path']; //$file_path = ltrim( $file_path['path'], '/' ); //$file_path = rtrim( ABSPATH, '/' ).$file_path['path']; $orig_size = getimagesize($file_path); $image_src[0] = $img_url; $image_src[1] = $orig_size[0]; $image_src[2] = $orig_size[1]; } } $file_info = pathinfo($file_path); // check if file exists $base_file = $file_info['dirname'] . '/' . $file_info['filename'] . '.' . $file_info['extension']; if (!file_exists($base_file)) { return; } $extension = '.' . $file_info['extension']; // the image path without the extension $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename']; $cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . $extension; // checking if the file size is larger than the target size // if it is smaller or the same size, stop right here and return if ($image_src[1] > $width) { // the file is larger, check if the resized version already exists (for $crop = true but will also work for $crop = false if the sizes match) if (file_exists($cropped_img_path)) { $cropped_img_url = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]); $vt_image = array('url' => $cropped_img_url, 'width' => $width, 'height' => $height); return $vt_image; } // $crop = false or no height set if ($crop == false or !$height) { // calculate the size proportionaly $proportional_size = nxt_constrain_dimensions($image_src[1], $image_src[2], $width, $height); $resized_img_path = $no_ext_path . '-' . $proportional_size[0] . 'x' . $proportional_size[1] . $extension; // checking if the file already exists if (file_exists($resized_img_path)) { $resized_img_url = str_replace(basename($image_src[0]), basename($resized_img_path), $image_src[0]); $vt_image = array('url' => $resized_img_url, 'width' => $proportional_size[0], 'height' => $proportional_size[1]); return $vt_image; } } // check if image width is smaller than set width $img_size = getimagesize($file_path); if ($img_size[0] <= $width) { $width = $img_size[0]; } // Check if GD Library installed if (!function_exists('imagecreatetruecolor')) { echo 'GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library'; return; } // no cache files - let's finally resize it $new_img_path = image_resize($file_path, $width, $height, $crop); $new_img_size = getimagesize($new_img_path); $new_img = str_replace(basename($image_src[0]), basename($new_img_path), $image_src[0]); // resized output $vt_image = array('url' => $new_img, 'width' => $new_img_size[0], 'height' => $new_img_size[1]); return $vt_image; } // default output - without resizing $vt_image = array('url' => $image_src[0], 'width' => $width, 'height' => $height); return $vt_image; }
/** * Calculated the new dimensions for a downsampled image. * * @since 2.0.0 * @see nxt_constrain_dimensions() * * @param int $width Current width of the image * @param int $height Current height of the image * @return mixed Array(height,width) of shrunk dimensions. */ function get_udims($width, $height) { return nxt_constrain_dimensions($width, $height, 128, 96); }
function nxt_save_image($post_id) { $return = new stdClass(); $success = $delete = $scaled = $nocrop = false; $post = get_post($post_id); @ini_set('memory_limit', apply_filters('admin_memory_limit', nxt_MAX_MEMORY_LIMIT)); $img = load_image_to_edit($post_id, $post->post_mime_type); if (!is_resource($img)) { $return->error = esc_js(__('Unable to create new image.')); return $return; } $fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0; $fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0; $target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : ''; $scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do']; if ($scale && $fwidth > 0 && $fheight > 0) { $sX = imagesx($img); $sY = imagesy($img); // check if it has roughly the same w / h ratio $diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2); if (-0.1 < $diff && $diff < 0.1) { // scale the full size image $dst = nxt_imagecreatetruecolor($fwidth, $fheight); if (imagecopyresampled($dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY)) { imagedestroy($img); $img = $dst; $scaled = true; } } if (!$scaled) { $return->error = esc_js(__('Error while saving the scaled image. Please reload the page and try again.')); return $return; } } elseif (!empty($_REQUEST['history'])) { $changes = json_decode(stripslashes($_REQUEST['history'])); if ($changes) { $img = image_edit_apply_changes($img, $changes); } } else { $return->error = esc_js(__('Nothing to save, the image has not changed.')); return $return; } $meta = nxt_get_attachment_metadata($post_id); $backup_sizes = get_post_meta($post->ID, '_nxt_attachment_backup_sizes', true); if (!is_array($meta)) { $return->error = esc_js(__('Image data does not exist. Please re-upload the image.')); return $return; } if (!is_array($backup_sizes)) { $backup_sizes = array(); } // generate new filename $path = get_attached_file($post_id); $path_parts = pathinfo($path); $filename = $path_parts['filename']; $suffix = time() . rand(100, 999); if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) { if ('thumbnail' == $target) { $new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}"; } else { $new_path = $path; } } else { while (true) { $filename = preg_replace('/-e([0-9]+)$/', '', $filename); $filename .= "-e{$suffix}"; $new_filename = "{$filename}.{$path_parts['extension']}"; $new_path = "{$path_parts['dirname']}/{$new_filename}"; if (file_exists($new_path)) { $suffix++; } else { break; } } } // save the full-size file, also needed to create sub-sizes if (!nxt_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) { $return->error = esc_js(__('Unable to save the image.')); return $return; } if ('nothumb' == $target || 'all' == $target || 'full' == $target || $scaled) { $tag = false; if (isset($backup_sizes['full-orig'])) { if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) { $tag = "full-{$suffix}"; } } else { $tag = 'full-orig'; } if ($tag) { $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']); } $success = update_attached_file($post_id, $new_path); $meta['file'] = _nxt_relative_upload_path($new_path); $meta['width'] = imagesx($img); $meta['height'] = imagesy($img); list($uwidth, $uheight) = nxt_constrain_dimensions($meta['width'], $meta['height'], 128, 96); $meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'"; if ($success && ('nothumb' == $target || 'all' == $target)) { $sizes = get_intermediate_image_sizes(); if ('nothumb' == $target) { $sizes = array_diff($sizes, array('thumbnail')); } } $return->fw = $meta['width']; $return->fh = $meta['height']; } elseif ('thumbnail' == $target) { $sizes = array('thumbnail'); $success = $delete = $nocrop = true; } if (isset($sizes)) { foreach ($sizes as $size) { $tag = false; if (isset($meta['sizes'][$size])) { if (isset($backup_sizes["{$size}-orig"])) { if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes["{$size}-orig"]['file'] != $meta['sizes'][$size]['file']) { $tag = "{$size}-{$suffix}"; } } else { $tag = "{$size}-orig"; } if ($tag) { $backup_sizes[$tag] = $meta['sizes'][$size]; } } $crop = $nocrop ? false : get_option("{$size}_crop"); $resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), $crop); if ($resized) { $meta['sizes'][$size] = $resized; } else { unset($meta['sizes'][$size]); } } } if ($success) { nxt_update_attachment_metadata($post_id, $meta); update_post_meta($post_id, '_nxt_attachment_backup_sizes', $backup_sizes); if ($target == 'thumbnail' || $target == 'all' || $target == 'full') { $file_url = nxt_get_attachment_url($post_id); if ($thumb = $meta['sizes']['thumbnail']) { $return->thumbnail = path_join(dirname($file_url), $thumb['file']); } else { $return->thumbnail = "{$file_url}?w=128&h=128"; } } } else { $delete = true; } if ($delete) { $delpath = apply_filters('nxt_delete_file', $new_path); @unlink($delpath); } imagedestroy($img); $return->msg = esc_js(__('Image saved')); return $return; }