• Resolved seanpaulfx

    (@seanpaulfx)


    Hi, how can I resize with remote upload.
    The below code is working fine for me but it is not able to resize the image nor reduce the size of the image.

    function on_save_postz($post_id)
    {
    $my_image_title = get_the_title($post_id);
    $img = get_post_meta( $post_id, 'image', true ); 
    $pathinfo = pathinfo($img);
    $image_url        = $vvw_img; // Define the image URL here
    $image_name       = wpartisan_sanitize_file_name($my_image_title).'.'.$pathinfo['extension'];
    $upload_dir       = wp_upload_dir(); // Set upload folder
    $image_data       = file_get_contents($image_url); // Get image data
    $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
    $filename         = basename( $unique_file_name ); // Create image file name
    
    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir['path'] ) ) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }
    
    // Create the image  file on the server
    file_put_contents( $file, $image_data );
    
    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );
    
    // Set attachment data
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title'     => sanitize_file_name( $filename ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );
    
    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    
    // Include image.php
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    
    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    
    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );
    
    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    }
    add_action('save_post', 'on_save_postz');

    If possible I would love to have this feature in the future.
    Thanks

    • This topic was modified 2 years, 11 months ago by seanpaulfx.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Remote Upload’ is closed to new replies.