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.
Thanks!
Tre
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');
Looking at your code, it looks like the file wil be looked for in
.../wp-content/uploads/bill.jpeg
. You’ll need to include the yyyy/mm as well. I would use this:Give that a whirl and let me know how you make out!
If you have images on your server that you want to add to WordPress, and then create intermediate Thumbnail sizes:
If we knew what Plugin you’re using to upload images, and what the intended use of those images is, we could provide a more precise answer for your use case.