简体   繁体   中英

how to resize or crop image during upload in Wordpress

I am new with wordpress and I have issue on how to deal in resizing or cropping images during upload.

I have this code already on my customized wordpress plugin:

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
            }
        } 
    } 
}

well, I tried add_image_size( 'mysize', 300, 300, true ); but I want the original image to be resize.

My issue is that, during upload, the image will be resize or cropped like 300X300. Is it possible?

您为什么不在照片编辑应用程序中裁剪所需图像,然后将其上传。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM