【问题标题】:how to resize or crop image during upload in Wordpress如何在 Wordpress 中上传期间调整图像大小或裁剪图像
【发布时间】:2017-06-25 08:53:22
【问题描述】:

我是 wordpress 新手,在上传期间如何处理调整大小或裁剪图像时遇到问题。

我的自定义 wordpress 插件上已有此代码:

function my_handle_attachment($file_handler,$post_id,$set_thu=false) {

  // check to make sure its a successful upload
  if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');

  $attach_id = media_handle_upload( $file_handler, $post_id );
  if ( is_numeric( $attach_id ) ) {
    update_post_meta( $post_id, '_my_file_upload', $attach_id );
  }
  return $attach_id;
}


if ( $_FILES ) { 
    $files = $_FILES["img"];  
    foreach ($files['name'] as $key => $value) {            
        if ($files['name'][$key]) { 
            $file = array( 
                'name' => $files['name'][$key],
                'type' => $files['type'][$key], 
                'tmp_name' => $files['tmp_name'][$key], 
                'error' => $files['error'][$key],
                'size' => $files['size'][$key]
            ); 
            $_FILES = array ("my_file_upload" => $file); 
            foreach ($_FILES as $file => $array) {              
                $newupload = my_handle_attachment($file,$pid); 
                // newly uploaded files
            }
        } 
    } 
}

好吧,我尝试了add_image_size( 'mysize', 300, 300, true );,但我希望调整原始图像的大小。

我的问题是,在上传过程中,图像将被调整大小或裁剪为 300X300。有可能吗?

【问题讨论】:

  • 为什么要调整原始图像的大小?您可以访问调用该特定尺寸的“mysize”。对于 WP 中设置的每个尺寸,它都会创建具有该尺寸的原始图像的副本...
  • 感谢您的回复@buxbeatz 我没有包括在内,我对尺寸进行了分类。我想要的是尺寸应该特定于可以调整/裁剪的特定尺寸。例如:我有一个大小为 300x250、400x350 和 500x450 的类别。我想要的是,一旦上传的图片设置为 300x250 类别,我不想要尺寸为 400x350 和 500x450 的图片副本。因为如果图片在每个类别大小上都有一个副本,我最终会存储大量图片。

标签: php wordpress


【解决方案1】:

为什么不在照片编辑应用程序中裁剪想要的图像,然后上传。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2015-02-22
    • 2020-09-15
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多