How to resize image from url WordPress

Viewed 30

I'm using a plugin (Really Simple CSV Importer) to import multiple post with feature image. I want to resize and rename the image before upload to media library with max_height = 800 and max_width = 800.

I have a function but the function is working only when i upload file manually from computer. But I'm using a csv file contain image url. Is there any way to integrate the function with the plugin. Function is not working with image url.

The function is below:

// Hook the function to the upload handler
// https://developer.wordpress.org/reference/hooks/wp_handle_upload/
add_filter('wp_handle_upload', 'resize_image_after_upload');

function resize_image_after_upload($image_data){
  // Set to null to disable that width/height resizing
  $max_width  = 800;
  $max_height = 800;

  // Check if there is a valid file
  if(empty($image_data['file']) || empty($image_data['type'])) {
    return $image_data;
  }

  // NOTE: We are not resizing any gifs, to avoid resizing animated gifs
  //      (which I think is the most common gif nowadays)
  $valid_types = array('image/png','image/jpeg','image/jpg', 'image/webp');
  if(!in_array($image_data['type'], $valid_types)) {
    return $image_data;
  }

  // Get image image_editor
  // https://developer.wordpress.org/reference/classes/wp_image_editor/
  $image_editor = wp_get_image_editor($image_data['file']);
  if(is_wp_error($image_editor)) {
    return $image_data;
  }

  // Check if the image editor supports the image type
  if(!$image_editor->supports_mime_type($image_data['type'])) {
    return $image_data;
  }

  // Perform resizing
  $sizes = $image_editor->get_size();
  if((isset($sizes['width']) && $sizes['width'] > $max_width)
    || (isset($sizes['height']) && $sizes['height'] > $max_height)) {
    // Resize, but do not crop
    $image_editor->resize($max_width, $max_height, false);

    // We will use the default recommended image quality
    // Change, if you want to set a custom quality
    //$image_editor->set_quality(90);

    $image_editor->save($image_data['file']);
  }

  return $image_data;
}

/////*Renaming attachment files to the post title*/

add_filter('media_handle_sideload', 'file_renamer');

function file_renamer( $filename ) {
    $info = pathinfo( $filename );
    $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    $name = basename( $filename, $ext );

    if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
        if($post = get_post($post_id)) {
            return $post->post_title . $ext;
        }
    }
    //$my_image_title = $post;
   // $file['name'] = $my_image_title  . - uniqid() . $ext; // uniqid method 
    $file['name'] = md5($name) . $ext; // md5 method
   // $file['name'] = base64_encode($name) . $ext; // base64 method

    return $filename;
  }
  
add_filter( 'big_image_size_threshold', '__return_false' );
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

    
 /* Automatically set the image Title, Alt-Text, Caption & Description upon upload*/
    add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
    function my_set_image_meta_upon_image_upload( $post_ID ) {
    
        // Check if uploaded file is an image, else do nothing
    
        if ( wp_attachment_is_image( $post_ID ) ) {
            
           // Get the parent post ID, if there is one
    
            if( isset($_REQUEST['post_id']) ) {
              $post_id = $_REQUEST['post_id'];
              } else {
              $post_id = false;
              }
    
                if ($post_id != false) {
            $my_image_title = get_the_title($post_id);
                } else {
            $my_image_title = get_post( $post_ID )->post_title;
                }
            
            // Sanitize the title:  remove hyphens, underscores & extra spaces:
            $my_image_title = preg_replace( '%\s*[_\s]+\s*%', ' ',  $my_image_title );
    
            // Sanitize the title:  capitalize first letter of every word (other letters lower case):
            //$my_image_title = ucwords( strtolower( $my_image_title ) );
            $my_image_title = ucwords( $my_image_title );
    
            // Create an array with the image meta (Title, Caption, Description) to be updated
            // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
            $my_image_meta = array(
                'ID'        => $post_ID,            // Specify the image (ID) to be updated
                'post_title'    => $my_image_title,     // Set image Title to sanitized title
                'post_excerpt'  => $my_image_title,     // Set image Caption (Excerpt) to sanitized title
                'post_content'  => $my_image_title,     // Set image Description (Content) to sanitized title
            );
    
            // Set the image Alt-Text
            update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
    
            // Set the image meta (e.g. Title, Excerpt, Content)
            wp_update_post( $my_image_meta );
    
        } 
    }

1 Answers
Related