I have a problem when using the command wp_generate_attachment_metadata.
I don’t have a problem when dealing with image files e.g. PNG or JPEG.
However, the command is failing for non image files like Audio or Video.
It was able to insert the attachment in the “wp_posts” table, but it does not populate the corresponding data in “wp_postmeta” table.
Please help, below are my code that is failing after successfully doing a wp_insert_attachment:
if ($attach_data = wp_generate_attachment_metadata( $attach_id, $file_path)) {
wp_update_attachment_metadata($attach_id, $attach_data);
}
Thanks in advance.
]]>I receive a empty array when i use wp_generate_attachment_metadata and this function do not generates the intermediate images.
I am adding images using wp_handle_uploads and this works fine. This generates a attach_id that I use with the function wp_generate_attachment_metadata.
I get no errors or warnings in the error_reporting log.
I am using WMPU(multisite) wordpress and I no longer know what to do…
$i=0;
$imgpath =wp_upload_dir();
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach ($images as $key) {
//$images[] <-- images url in the default upload dir
if($key == 'error')continue;
$path = $imgpath['path'].'/'.basename($key);
$attachment = array(
'guid' => $key, //url
'post_mime_type' => image_type_to_mime_type(exif_imagetype($key)),
'post_title' => sanitize_file_name(basename($key)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment,$path, $pid );
$attach_dat = wp_generate_attachment_metadata( $attach_id, $path);
if(!empty($attach_dat))wp_update_attachment_metadata($attach_id, $attach_dat);
if($i==0) set_post_thumbnail( $pid, $attach_id );
$i++;
}
That was coded in a non template-file, where i load WP with
require_once($_SERVER['DOCUMENT_ROOT']."/test/site0/wp-config.php");
I not sure it influences in something.
add_filter('wp_generate_attachment_metadata','updateExif_uploaded_image');
function updateExif_uploaded_image($image_data)
{
// do stuff ...
wp_update_attachment_metadata( $attachment_id, $image_data);
return $image_data;
}
While “do stuff” works, I’m having trouble actually updating/saving the meta data as from what I can see, the attachment id is not passed into this function.
From /wp-admin/includes/image.php
I can see that the ID is passed as a parameter but how do I get that from my function?
I tried function updateExif_uploaded_image($image_data, $attachment_id)
but this caused an error with incorrect number of parameters.
Thanks in advance
]]>$filename = get_user_meta($userID, 'image_profil',true);
$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
Why the $attach_data return an empty array ?
I installed Cycle2 for jQuery (excellent plugin!) and went ahead to add an additional image size in functions.php and the black and white filter such as explained by Otto and c.bavota here and here.
The trouble I found is when trying to display the black and white version via:
get_the_post_thumbnail()
Using get_the_post_thumbnail( 'thumbnail' );
works well as intended.
Using get_the_post_thumbnail( 'thumbnail-bw' );
on the other hand did not.
It displayed the regular colored Thumbnail.
I read further down in the comments, and kind of understood from Otto that WP does not support adding a new file size that has the exact size as another (in my situation: the thumbnail).
But in the /wp-content/uploads/ folder, there IS an extra version of the file with “-bw” appended to the filename before the extension.
I thought I’d trick WP into displaying it, and toyed around with the following code to force the thumbnail_name-bw.jpg url into an img tag via the following:
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $thumbnail->ID ) ) {
$img_id = get_post_thumbnail_id( $thumbnail->ID );
$img_data = wp_get_attachment_image_src( $img_id, 'thumbnail' );
$img_thumb_url = $img_data[0];
$ext = pathinfo( $img_thumb_url, PATHINFO_EXTENSION );
$img_dir = wp_upload_dir();
//$bw_img_thumb_url = $img_dir[ 'url' ] . '/' . basename( $img_thumb_url, ".$ext" ) . '-bw.' . $ext;
$bw_img_thumb_url = 'https://69.16.204.204/~kaosarch/wp-content/uploads/slide-default-320x126-bw.jpg';
$default_img = get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
$bw_img = '<img class="bw" scr="' . $bw_img_thumb_url . '" />';
} else {
$default_img = '<img src="' . get_template_directory_uri() . '/images/thumb-default.png" />';
$bw_img = '<img class="bw" src="' . get_template_directory_uri() . '/images/thumb-default-bw.png" />';
}
I use the variables I get to build my carousel.
And HERE is what I have not yet understood:
In the PAGE SOURCE, both img tags are correctly generated, the urls of both thumbnails version are generated without errors, but the one for the thumbnail displays the thumbnail, and its url in the source view is clickable, while the one for black and white thumbnail displays nothing in the browser, and the url in the source is not clickable!
If I copy this url, and paste in IN the browser address box, it goes right to the image I want!
And via FTP, I can quite see the black and white image in the uploads folder, near it’s siblings.
Is this…. wizardry? black magic? voodoo?
—————–
For reference, my
functions.php code:
add_action('after_setup_theme','bw_images_size');
function bw_images_size() {
$crop = get_option('thumbnail_crop')==1 ? true : false;
add_image_size('thumbnail-bw', get_option('thumbnail_size_w')+1, get_option('thumbnail_size_h'), $crop);
}
add_filter('wp_generate_attachment_metadata','bw_images_filter');
function bw_images_filter($meta) {
$file = wp_upload_dir();
$file = trailingslashit($file['path']).$meta['sizes']['thumbnail-bw']['file'];
list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
$image = wp_load_image($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
switch ($orig_type) {
case IMAGETYPE_GIF:
$file = str_replace(".gif", "-bw.gif", $file);
imagegif( $image, $file );
break;
case IMAGETYPE_PNG:
$file = str_replace(".png", "-bw.png", $file);
imagepng( $image, $file );
break;
case IMAGETYPE_JPEG:
$file = str_replace(".jpg", "-bw.jpg", $file);
imagejpeg( $image, $file );
break;
}
return $meta;
}
-> to create the black and white thumbnail
And to display it (the whole carousel code):
/*
Cusom function to list categories and retrieve a random image
from a random post within each cat >> For bottom page CAROUSEL
We want to build the following line foreach cat:
<div class="slide"><div class="image"><a href="(the_category_permalink)"><img src="(a_random_thumbnail_from_cat)" /></a></div><p>(cat_name)</p></div>
*/
function category_carousel($exclude = '') {
if( !empty( $exclude ) ) {
$exclude = 'exclude=' . $exclude;
}
$categories = get_categories( $exclude . '&hide_empty=0&order=desc' );
if( !empty( $categories ) ) {
$html = '<div id="carousel" class="cycle-slideshow" data-cycle-fx=carousel data-cycle-timeout="0" data-cycle-slides=".slide">';
foreach ($categories as $cat) {
$thumbnails = get_posts('numberposts=1&order=rand&cat=' . $cat->cat_ID );
if( $thumbnails ) {
foreach ($thumbnails as $thumbnail) {
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $thumbnail->ID ) ) {
$img_id = get_post_thumbnail_id( $thumbnail->ID );
$img_data = wp_get_attachment_image_src( $img_id, 'thumbnail' );
$img_thumb_url = $img_data[0];
$ext = pathinfo( $img_thumb_url, PATHINFO_EXTENSION );
$img_dir = wp_upload_dir();
//$bw_img_thumb_url = $img_dir[ 'url' ] . '/' . basename( $img_thumb_url, ".$ext" ) . '-bw.' . $ext;
$bw_img_thumb_url = 'https://69.16.204.204/~kaosarch/wp-content/uploads/slide-default-320x126-bw.jpg';
$default_img = get_the_post_thumbnail( $thumbnail->ID, 'thumbnail' );
$bw_img = '<img class="bw" scr="' . $bw_img_thumb_url . '" />';
} else {
$default_img = '<img src="' . get_template_directory_uri() . '/images/thumb-default.png" />';
$bw_img = '<img class="bw" src="' . get_template_directory_uri() . '/images/thumb-default-bw.png" />';
}
} /* foreach thumb */
} else {
$default_img = '<img src="' . get_template_directory_uri() . '/images/thumb-default.png" />';
$bw_img = '<img class="bw" src="' . get_template_directory_uri() . '/images/thumb-default-bw.png" />';
}
$html .= '<div class="slide"><a href="' . esc_url( get_category_link( $cat->cat_ID ) ) . '"><div class="image"><img class="attachment-thumbnail" scr="https://69.16.204.204/~kaosarch/wp-content/uploads/slide-default-320x126-bw.jpg" />' . $bw_img . $default_img . '</div><p>' . $cat->name . '</p></a></div>';
} /* foreac $cat */
$html .= '</div>';
return $html;
}
}
Thanks for alla nd any help in advance :S
]]>$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
So it seems that WordPress dies when trying to generate thumbnails. I am not able to upload the file manually via Media Library either. There I get
Fatal error: Allowed memory size exhausted in …/wp-includes/media.php
I was thinking it’s php memory limit causing it but the script does not fail when triggered by wp_cron.
I have tried setting WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT but neither seems to have an effect.
Any ideas?
Thanks in advance!
I am hoping to get some help with generation of thumbnails for an image in the upload folder.
I’ve read every post on wp_insert_attachment(), wp_generate_attachment_metadata(), and wp_update_attachment_metadata() and tried everything, but I cannot get the thumbnails to generate, and I think it’s because something is going wrong with the image_make_intermediate_size() function.
I believe I’ve traced the error to the image.php file and wp_generate_attachment_metadata() function, which calls:
$resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
echo $resized;
I put in the echo, and even though I’ve verified that $file, $size_data are all specified and valid arguments, the image_make_intermediate_size() function never creates the thumbnail or returns “false” either, for that matter.
I’ve checked directory permissions, they are 755 on that folder. I’m using standard code to call the thumbnail creation functions:
// Create post object
$my_post = array(
'post_title' => 'Title.',
'post_content' => 'This is my post.',
'post_status' => 'inherit',
'post_author' => $post_user,
'post_parent' => $post_parent,
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'guid' => $filepath,
);
$attach_id = wp_insert_attachment( $my_post, $filepath, $post_parent );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
Any ideas why the image_make_intermediate_size() function seems not to be working? Thanks!
Shawn
First time here writing, but I’ve read several threads for great help in developing sites using WordPress So, I hope I can get some help as well.
The problem I have is that I’m more or less trashing the built-in image upload function of WP. It doesn’t give me the image sizes I want and such stuff, so I’ve made a function that renames the image file after I’ve uploaded it from the “large” size name (in my case “img_01_-530×351.jpg”, to “img_01_.jpg”).
That however makes a file “missing” according to the database entry which thinks there still should be a img_01_-530×351.jpg file, which now is renamed to img_01_.jpg.
I hope you understand..
Anyhow, to fix that I’d like to go through the “_wp_attachment_metadata” post for the image in the database. This works perfectly when I load an image with an older post_id or meta_id. But if, as I’ve tried for a few hours now, try to run it directly after I rename the file (above) through the following call;
add_filter( 'wp_generate_attachment_metadata', 'rceRenameImage' );
It results in not finding the post “_wp_attachment_metadata” for the just uploaded image. I’ve figured this out after sitting and banging my head against my desk for the last two hours, that the actual “_wp_attachment_metadata” post in the database for the image is created AFTER “wp_generate_attachment_metadata” returns to normal function in the WP core.
So everything that happens when I call the above add_filter happens AFTER the post “_wp_attached_file” is created for the image, but BEFORE “_wp_attachment_metadata”.
This results in that the function is unable to find this post and therefore I can’t update it.
Long post, I hope you all understand what I’m trying to do here.
To sum it up, I’d like to change the “_wp_attachment_metadata” post in the database after I’ve uploaded an image.
And it doesn’t work, that’s why I’m here
So, I’m wondering.. is there something that is called after the function call “wp_generate_attachment_metadata” in WP that I can hook into?
The reason I’m changing most of the upload function is that I like the GUI part of the whole upload process, but the background functions doesn’t really give me what I want
The ideal would be that I could specify the exact 2 sizes and names for every image that is uploaded and remove the whole “choose size” idea in the upload manager. But without changing the core files that I’d like to skip, it doesn’t seem to be doable.
Well well, I hope you can help me out Thanks in advance!
Here a simplified version of the code that I am using which is basically the one provided in the wp_insert_attachment codex page:
$filename = 'https://www.mysite.com/wp-content/uploads/2012/01/Earth-Western-Hemisphere.jpg';
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'ID' => 705,
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_excerpt' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, 704 );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . 'wp-admin/includes/image.php');
//update_attached_file( $attach_id, $filename );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
Any help would be greatly appreciated!
Thank you,
Anton