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');
  • Just out of curiosity is there a reason why you need to store the thumbnails within a separate folder and then convert them to Wordpress attachments? Would it not be easier to upload the images from your plugin using inbuilt Wordpress functionality and save yourself some time? I could be missing the bigger picture here though, just curious. If it helps this link explains how to use the media uploader in a plugin: webmaster-source.com/2010/01/08/… - then just use image sizes for different sized images. Feb 15, 2012 at 2:19

2 Answers 2


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:

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';

# Function reference
# image_resize ( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )

$wpUploadPath = wp_upload_dir();
$fileName = preg_replace('/^.*?\/(\d{4})\/(\d\d)\/(.*)$/', $wpUploadPath['basedir'].'/$1/$2/$3', $file);

image_resize( $fileName, 200, 200, true, '200x200' );

Give that a whirl and let me know how you make out!

  • I changed my wp_upload_dir to be /uploads/ , so it wont be in wp-content. I'll give this a whirl , though
    – tr3online
    Feb 15, 2012 at 0:40
  • Yeah, that didn't work. the $destpath was /home/mysite/public_html/uploads/2012/02/bill-poucher-prof2.jpeg when echoed. I invoked image_resize( $file, $max_w, $max_h, $crop, '-200x200', $destpath);. No file was generated :\
    – tr3online
    Feb 15, 2012 at 0:45
  • The first parameter is the source, which I left confusing. I'll update my code. Feb 15, 2012 at 1:11
  • Hmmm. Still no file is being generated. I wish I knew what was happening :|
    – tr3online
    Feb 15, 2012 at 6:12
  • This code does work for me, so you can at least rule out the code itself. That leaves you 3 possible scenarios: (1) you don't have GD installed. Can your server create thumbs on newly uploaded images? (2) The directory /home/mysite/public_html/uploads/2012/02/ is not writeable. Does your web server have write permissions to that dir? (3) The image is working but it isn't going where you think it's going. image_resize() returns the path to the file it generates, so echo that and see what it says. Good luck! Feb 15, 2012 at 14:23

If you have images on your server that you want to add to WordPress, and then create intermediate Thumbnail sizes:

  1. Add them to the WP database as attachment post-types, e.g. via the Add From Server Plugin.
  2. You may then need to have WordPress regenerate intermediate image sizes for those images, e.g. via the Regenerate Thumbnails Plugin.

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.

  • I'm using gravity forms to handle the file submission. The only problem is gravity forms stores it's files in some odd directory. They have a post-submit-form hook that I want to tap in to, grab the uploaded image, and then re-size it to be used as a users avatar.
    – tr3online
    Feb 15, 2012 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.