php 根据当前注册的图像尺寸(Wordpress),根据最小高度自动限制最小上传尺寸

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 根据当前注册的图像尺寸(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;
}

以上是关于php 根据当前注册的图像尺寸(Wordpress),根据最小高度自动限制最小上传尺寸的主要内容,如果未能解决你的问题,请参考以下文章

PHP 在Wordpress中强制实施最小图像尺寸

php 删除WordPress全尺寸图像插入post +选项并添加最大尺寸以防止用户插入大量i

php 显示注册图像尺寸(默认值除外)

php 根据帖子类型更改WordPress图像属性。

如何在 WordPress 中根据视口大小提供不同大小的图像?

基于 Slider 图像的动态 wordpress 背景