I'm trying to generate thumbnails on my server that are being uploaded by a plugin. It uploads all images to a directory. I'm trying to target these files and then create a thumbnail with them based on img_resize, but I am not having any lucky. Eventually I'll be inserting the thumbs into the media gallery and into user meta, but I can't even produce a thumbnail yet, so that is the first task at hand!
Here's my simple code thus far:
require_once(ABSPATH . '/wp-admin/includes/media.php'); require_once(ABSPATH . '/wp-admin/includes/image.php'); $file = 'http://www.mysite.org/uploads/uploaddir/2012/02/bill.jpeg'; $max_w = 200; $max_h = 200; $crop = true; $filePathInfo = pathinfo($file); $fileName = $filePathInfo['basename']; $wpUploadPath = wp_upload_dir(); $destpath = $wpUploadPath['basedir'].'/'.$fileName; image_resize( $file, $max_w, $max_h, $crop );
I tried using the
$destpath as well and nothing is happening. No thumbnails are being generated. Am I thinking about this incorrectly?
Any light anyone could shed upon this situation would be greatly appreciated.
SOLVED NOTE: This is the code that finally worked for me. I was using the URI before, to define the image_resize. Not the absolute directory.
$wpUploadDir = wp_upload_dir(); $baseDir = $wpUploadDir['basedir']; $fileName = 'bill.jpg'; $filePath = $baseDir . "/" . $fileName; $wp_filetype = wp_check_filetype($filePath, null); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => $fileName, 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filePath ); require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $filePath ); wp_update_attachment_metadata( $attach_id, $attach_data ); image_resize($filePath, 200, 200, true, '200x200');