I’m working on adding the possibility to add some custom_post_type from the front end. I can link Images to them, but apparently they are not resized. And I really need the special sizes that I have define on my functions.php.
Here is what I do :
if(!empty($_FILES['IMAGE'])){
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$file = $_FILES['IMAGE'];
$upload = wp_handle_upload( $file, array('test_form' => false));
if(!isset($upload['error']) && isset($upload['file'])){
$title = $file['name'];
$filetype = wp_check_filetype( basename($upload['file'], null ));
$ext = strrchr($title,'.');
$title =($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
//$url_img = wp_get_attachment_url( $attach_key );
$attach_key = '_thumbnail_id';
$attach_id = wp_insert_attachment($attachment,$upload['file']);
$existing_download = (int) get_post_meta($new_eta_ID, $attach_key, true);
if(is_numeric($existing_download)){
wp_delete_attachment( $existing_download );
}
update_post_meta($new_eta_ID, $attach_key, $attach_id,true);
}
}
So it uploads the selected files, but it’s not resizing. How can I do that ?
Thanks !
The following code is taken from my “Dynamic image resize”-plugin.
The plugin takes an ID as well as a string into account. I didn’t delete those lines, as they might be usable for later visitors – check the link anyway.
Retrieve the image by ID or URl
Check for Errors
and abort if no Attachment was found. This would mean the upload didn’t end as expected.
Check already existing image sizes
In case there already was an image of the size we need. This might be the case if the user tries to upload the same image multiple times (which you’d have to check to avoid duplicates).
Process the resize
Now we’re sure we got an attachment and need to resize. We as well update the
post
andattachment
post type meta data.Now everything should be fine and ready to go on.
If you don’t need everything, just drop what you don’t need, but make sure you don’t leave out the needed error checks. Better safe than sorry.