Error in getting image using wp_remote_get () function

I am using wp_remote_get

to extract image from url and attach it to post as preferred image. I was able to do this initially using some help from this post and the images were successfully configured as the image shown and displayed in the backend, but now the images are set as a recognized image successfully, but only the image name is displayed (as if the image file path was broken ).
When I go to the image path using ftp, I can see the image file, but when I try to open the image, it says an unsupported format.
Below is the code I used to extract images

$upload_dir = wp_upload_dir();            
$image_url = $one_post->images[0];
$image_data = wp_remote_get($image_url);
//Get image and set unique file name
$filename = $new_post_id."_".$one_post->ID."_".basename($image_url);    
if (wp_mkdir_p($upload_dir['path'])) {
     $file = $upload_dir['path'] . '/' . $filename;
     } else {
       $file = $upload_dir['basedir'] . '/' . $filename;
     }
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
           'post_mime_type' => $wp_filetype['type'],
           'post_title' => sanitize_file_name($filename),
           'post_content' => '',
           'post_status' => 'inherit',
      );            
$attach_id = wp_insert_attachment($attachment, $file, $new_post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($new_post_id, $attach_id);

      

If I open the image file with a text editor I see something like below

ArrayÿØÿà JFIF

      

Is there a mistake in the encoding?
Please correct me what I am doing wrong.

+3


source to share


1 answer


As I mentioned in my comment, you are seeing this problem because of the way you use wp_remote_get()

and file_put_contents()

.

I can also see that you are duplicating some of the WordPress functionality. In my example below, I rewrote your code to take advantage of existing WordPress features.



$image_url = $one_post->images[0];

$tmp = download_url( $image_url );

// fix filename for query strings
preg_match( '/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $image_url, $matches );

$file_array = array(
    'name'     => $new_post_id . '_' . $one_post->ID . '_' . basename( $matches[0] ),
    'tmp_name' => $tmp
);

// Check for download errors
if ( is_wp_error( $tmp ) ) {
    @unlink( $file_array['tmp_name'] );
    return false;
}

$id = media_handle_sideload( $file_array, $new_post_id );

// Check for handle sideload errors.
if ( is_wp_error( $id ) ) {
    @unlink( $file_array['tmp_name'] );
    return false;
}

// Set post thumbnail.
set_post_thumbnail( $new_post_id, $id );

      

Based on the example provided on the Codex page for media_handle_sideload()

: https://codex.wordpress.org/Function_Reference/media_handle_sideload

+3


source







All Articles