How to upload imagick resource to media in wordpress

    $filePath = wp_get_attachment_url( $id );
$tempfilename = explode("/", $filePath);
$temppdffile = end($tempfilename);
$cuurentPath = plugin_dir_path( __FILE__ ).$temppdffile;
$im = new Imagick();
$im->setResolution(300, 300);
$im->readImage( $cuurentPath.'[0]'); 
$im->setImageFormat('jpg');

I have above code.. i have successsfully generated image.. $im But i have to save image to.. media in wordpress as a featured image ? Any idea.. tried every thing

Solutions

I'm not too familiar with Imagick, but if you can get a URL for the image you've created with it, then the rest is fairly straightforward using WordPress's media_sideload_image() function. Here's a basic example that would work from wp-admin area(like settings page). Note that it looks like you'd use your $id variable in place of my $post_id :

// load image from URL into media library, set a post as its parent, and return new attachment's id
$attachment_id = media_sideload_image( $image_url, $post_id, $img_title, 'id'); 
if( !is_wp_error( $attachment_id ) ) { // if no error returned...
    // ...Set above loaded image as the featured image of the current post
    set_post_thumbnail($post_id, $attachment_id);
}

Note that Codex says third param of media_sideload_image sets description of the image, but it sets image title . Also, they just added the fourth parameter for returning the attachment id in 4.8, so hopefully you're up to date.

More Info:

  • https://codex.wordpress.org/Function_Reference/media_sideload_image
  • https://codex.wordpress.org/Function_Reference/set_post_thumbnail

Also note that if you're implementing this in the front end you'll need to include the following three files in your script so that media_sideload_image can handle the uploading, etc it does for us:

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

Similar questions

Wordpress Media Upload blocking file type from uploads.php, but not media-upload.php
I have a website running Wordpress 5.2.4 (latest at the moment). I'm trying to set it up to allow .dwg file uploads in the media library from a File Upload field in an ACF repeater. I received the error below: "Sorry, this file type is not supported for security reasons" .dwg files were not allowed by Wordpress, so we added it as a Mime Type in fun...
PHP - Upload specified collection of values to a resource identified by specified URI
Just finished an automatic newsletter-subscriber module written in C#. Now I have to translate it in PHP so I can use it with my wordpress sites as well. I'm not that good in PHP as most of the time I'm writting .NET MVC applications. In C# I came up with the following solution: Now I'm looking for a similar method as WebClient.UploadValues but for...
How to get thumbnail image for media library all the resources? I have resource id (attachment_id)
I have attachment id and from that I want to display its thumbnail in case of all type of attachments video,image,audio. So how can i get that? I want the 150X150 px image. wordpress already have this but i dont know how to retrive it. So can any one tell me this? I used this but it is giving image of big size. can any one tell me which funciton of...
Give users a maximum upload capacity; limit the number of files a user can upload OR limit the number of files per upload
I'm using the Media Library on the front end of my website and I'd like to stop users from being able to spam my server by uploading an unlimited number of files. As such, I'd like to do one or maybe all of the below: None of these are bullet-proof but they'd hopefully make such "spamming" a difficulty. Thanks in advance,
How to upload video file using media upload in wordpress?
I have a post type which is used to upload banners in wordpress. Using custom fields for name, image/video and url. The default media upload works fine to upload image/videos, but my problem is I cannot get the video base url to the custom field. Here is my code that is not able to get the video url. It only gives me video file name.
Wordpress media upload error: "An error occurred in the upload. Please try again later"
I get this error An error occurred in the upload Please try again later" pretty often uploading media from front-end, but not every time and that makes this thing more difficult. If i log in first time from new browser then i get this error, but if i try more from other profiles i can do uploads, and after a while it appears again. I these times wh...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .