Skip to content

Instantly share code, notes, and snippets.

@certainlyakey
Created October 28, 2017 19:37
Show Gist options
  • Select an option

  • Save certainlyakey/4b902f7ee24d29427841fe7d5b1550eb to your computer and use it in GitHub Desktop.

Select an option

Save certainlyakey/4b902f7ee24d29427841fe7d5b1550eb to your computer and use it in GitHub Desktop.
Limit minimum upload dimensions automatically by smallest width of height based on image sizes currently registered (Wordpress)
<?
// Limit upload size
function themeprefix_limit_upload_size($file) {
$img = getimagesize($file['tmp_name']);
$minimum = themeprefix_get_minimum_width_height_of_all_sizes();
$width = $img[0];
$height = $img[1];
if ($width < $minimum['width'] ) {
return array('name' => $file['name'], 'error' => 'Image dimensions are too small. Minimum width is ' . $minimum['width'] . ' pixels. Uploaded image width is ' . $width . ' pixels');
} elseif ($height < $minimum['height']) {
return array('name' => $file['name'], 'error' => 'Image dimensions are too small. Minimum height is ' . $minimum['height'] . ' pixels. Uploaded image height is ' . $height . ' pixels');
} else {
return $file;
}
}
add_filter('wp_handle_upload_prefilter','themeprefix_limit_upload_size');
// Get all image sizes along with width and height
function themeprefix_get_all_image_sizes() {
global $_wp_additional_image_sizes;
$default_image_sizes = get_intermediate_image_sizes();
foreach ($default_image_sizes as $size) {
$image_sizes[$size]['width'] = intval(get_option("{$size}_size_w"));
$image_sizes[$size]['height'] = intval(get_option( "{$size}_size_h"));
$image_sizes[ $size ]['crop'] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false;
}
if ( isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes) ) {
$image_sizes = array_merge($image_sizes, $_wp_additional_image_sizes);
}
return $image_sizes;
}
// Get minimum width and height from all the registered sizes
function themeprefix_get_minimum_width_height_of_all_sizes() {
$all_image_sizes = hram_get_all_image_sizes();
$all_widths = array();
$all_heights = array();
foreach ($all_image_sizes as $size_name => $dimensions) {
if ($dimensions['width'] != '0') {
$all_widths[] = $dimensions['width'];
}
if ($dimensions['height'] != '0') {
$all_heights[] = $dimensions['height'];
}
}
$min_dimensions = array('width' => min($all_widths), 'height' => min($all_heights));
return $min_dimensions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment