1

I want to put resized files in original file metadata.

//Original file
$aid = wp_insert_attachment($attachment, $file['file'], $post_id);
$a = wp_generate_attachment_metadata($aid, $file['file']);
//Croped images from original
$path_300x200 = path_to_file_300x200.jpg
$path_100x100 = path_to_file_100x100.jpg

crop_image($file['file'], $path_300x200, 300, 200, 85, true);
crop_image($file['file'], $path_100x100, 100, 100, 85, true);

$file1= getimagesize( $path_300x200 );
$info1 = array(
'file' => wp_basename( $path_300x200 ),
'width' => $file1[0],
'height' => $file1[1],
);
$metadata1['sizes']['300x200'] = $info1;
$a_300x200 = wp_generate_attachment_metadata($aid, $metadata1);
wp_update_attachment_metadata($aid, $a_300x200 );

$file2= getimagesize( $path_100x100 );
$info2 = array(
'file' => wp_basename( $path_100x100 ),
'width' => $file2[0],
'height' => $file2[1],
);
$metadata2['sizes']['100x100'] = $info2;
$a_200x200 = wp_generate_attachment_metadata($aid, $metadata2);
wp_update_attachment_metadata($aid, $a_100x100 );

I get in database:

_wp_attachment_metadata a:2:{s:5:"width";N;s:6:"height";N;}

What I am doing wrong?

UPDATE:

//Original file
$aid = wp_insert_attachment($attachment, $file['file'], $post_id);
$a = wp_generate_attachment_metadata($aid, $file['file']);
//Croped images from original
$path_300x200 = path_to_file_300x200.jpg
$path_100x100 = path_to_file_100x100.jpg

crop_image($file['file'], $path_300x200, 300, 200, 85, true);
crop_image($file['file'], $path_100x100, 100, 100, 85, true);

$file1= getimagesize( $path_300x200 );
$info1 = array(
'file' => wp_basename( $path_300x200 ),
'width' => $file1[0],
'height' => $file1[1],
);
$metadata['sizes']['300x200'] = $info1;


$file2= getimagesize( $path_100x100 );
$info2 = array(
'file' => wp_basename( $path_100x100 ),
'width' => $file2[0],
'height' => $file2[1],
);
$metadata['sizes']['100x100'] = $info2;

$a['sizes']=$metadata; 
//$a is generated original file metadata, and I added value to the key "sizes".

wp_update_attachment_metadata($aid, $a );

Is it right solution ?

The problem is that when I want to delete original photo it only deletes original photo, but not resized... any thoughts ?

3
  • As I found that wp_update_attachment_metadata erases all image metadata and adds what you update ? because now I get only with sizes info, how to add original file metadata ? – andys Dec 23 '13 at 20:05
  • I need only to update ['sizes'] in original file metadata... How can I make it ? – andys Dec 23 '13 at 20:19
  • I've been searching for this too. It seems odd that there's no mention in the Codex that this function replaces rather than adds to the existing metadata. I guess I'll have to read in existing metadata, check for an existing key of the same name, then add my own entry... – jfacemyer May 5 '14 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.