Skip to content

Instantly share code, notes, and snippets.

@AnadarProSvcs
Created December 17, 2023 19:22
Show Gist options
  • Save AnadarProSvcs/031c9991708543e88f7660da1a38b4ec to your computer and use it in GitHub Desktop.
Save AnadarProSvcs/031c9991708543e88f7660da1a38b4ec to your computer and use it in GitHub Desktop.
Function to resize an uploaded image to a maximum width/height depending on orientation
function resize_uploaded_image($file_path, $max_width, $max_height) {
$image = wp_get_image_editor($file_path);
if (!is_wp_error($image)) {
$size = $image->get_size();
$current_width = $size['width'];
$current_height = $size['height'];
// Determine the orientation of the image
$orientation = $current_width > $current_height ? 'landscape' : 'portrait';
// Logic for resizing based on orientation
if ($orientation == 'landscape' && $current_width > $max_width) {
// Resize landscape image
$image->resize($max_width, null, false);
} elseif ($orientation == 'portrait' && $current_height > $max_height) {
// Resize portrait image
$image->resize(null, $max_height, false);
}
// Save the image
$image->save($file_path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment