<?php
// $filename should be the path to a file in the upload directory.
$filename = '/path/to/uploads/2013/03/filename.jpg';
// The ID of the post this attachment is for.
$parent_post_id = 37;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
// $result is always false
$result = wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
?>
]]>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
]]>$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 also added a filter to read Geo Location EXIF data from an image is present.
What I’d like to know now is how to add the geo meta data to the taxonmy “location” in that attachment ID.
Thoughts? I can supply function code if needed.
]]>move_uploaded_file($_FILES['media']['tmp_name'], $uploadFile)
The file is correctly copied to the desired folder, with the permission set to 644. After this step I make the desired thumbs and smaller versions of the image using:
image_resize( $resizefile, $tthumb ['w'], $tthumb ['h'], $tthumb ['c'])
The smaller copies of the image are correctly created and placed in ../wp-content/uploads/, with permision set to 666.
So far all goes fine. At this moment I try to add the image to the wp_post & wp_postmeta file using:
$wp_filetype = wp_check_filetype(basename($fileName), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_author' => '1',
'post_title' => $_POST['message'],
'post_name' => $_POST['message'],
'guid' => get_option ( 'siteurl' ) . '/wp-content/uploads/' . $fileName,
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $fileName, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fileName );
wp_update_attachment_metadata( $attach_id, $attach_data );
The attachment is correctly inserted into wp_post. Even the records made into wp_postmeta do exists. The only problem is the record with the “_wp_attachment_metadata” key. This one only contains “a:0:{}“, instead of the expected long string. Because of this “empty” string I’m not able to edit the image inside WordPress. I get the error “Image data does not exist. Please re-upload the image.”. Also WordPress thinks there are no thumbnails, which do exists.
Who can help me out? Why is there no metadata? Are my steps correct? Thanks in advance.
]]>After upload, ‘Full size’ reads 0x0 pixels and ‘Thumbnail’ and ‘Medium’ are not configured. I’m having trouble finding any helpful documentation on the functions I need to use. Here’s my script:
$file='wm4hd.jpg';
$filepath=$_SERVER['document_root'].'/wp-content/uploads/2009/04/'.$file;
// find TYPE
$wp_filetype = wp_check_filetype(basename($file), null );
extract($wp_filetype);
if (!$type) $type = "";
$title = 'wm4hd';
$content = '';
$url = 'https://www.example.com/wp-content/uploads/2009/04/'.$file;
$file = array(
'file' => $filepath,
'url' => $url,
'type' => $type);
$attachment = array(
'guid' => $url,
'post_mime_type' => $type,
'post_title' => $title,
'post_content' => $content,
'post_type' => 'attachment',
'post_parent' => 288
);
$id = wp_insert_attachment($attachment, $filepath, 288);
if (!is_wp_error($id)) {
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filepath));
echo "Imported: $filepath ($title)<br />";
}
]]>