Creating a thumbnail from an image already on the server?

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:

Read More
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');

Related posts

Leave a Reply

2 comments

  1. 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})/(dd)/(.*)$/', $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!

  2. 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.