How does WP media uploader create the 3 different sized images, and how can I duplicate it

Hi @jaredwilli:

Dude! Valiant effort, and nice work. All-in-all it could be a great addition to WordPress.

You were so close, but you had somewhere between 5 and 10 little failed assumptions or code that looks like you started it but didn’t get back to it because it wasn’t working. I reworked your function only as much as I needed to correct it. The solution follows, and I’ll leave the side-by-side comparison to your or someone less burned-out. 🙂

function update_attachment() {
  global $post;
  wp_update_attachment_metadata( $post->ID, $_POST['a_image'] );
  if( !empty( $_FILES['a_image']['name'] )) { //New upload
    require_once( ABSPATH . 'wp-admin/includes/file.php' );

    $override['action'] = 'editpost';
    $file = wp_handle_upload( $_FILES['a_image'], $override );

    if ( isset( $file['error'] )) {
      return new WP_Error( 'upload_error', $file['error'] );
    }

    $file_type = wp_check_filetype($_FILES['a_image']['name'], array(
      'jpg|jpeg' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png',
    ));
    if ($file_type['type']) {
      $name_parts = pathinfo( $file['file'] );
      $name = $file['filename'];
      $type = $file['type'];
      $title = $_POST['a_title'] ? $_POST['a_title'] : $name;
      $content = $_POST['a_desc'];

      $post_id = $post->ID;
      $attachment = array(
        'post_title' => $title,
        'post_type' => 'attachment',
        'post_content' => $content,
        'post_parent' => $post_id,
        'post_mime_type' => $type,
        'guid' => $file['url'],
      );

      foreach( get_intermediate_image_sizes() as $s ) {
        $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
        $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
        $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
        $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
      }

      $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );

      foreach( $sizes as $size => $size_data ) {
        $resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
        if ( $resized )
          $metadata['sizes'][$size] = $resized;
      }

      $attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $post_id - for post_thumbnails*/);

      if ( !is_wp_error( $id )) {
        $attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
        wp_update_attachment_metadata( $attach_id, $attach_meta );
      }
      update_post_meta( $post->ID, 'a_image', $file['url'] );
    }
  }
}

Hey, why not splurge and get yourself a copy of PhpStorm? You could have easily solved this yourself, you were sooo close, if you could have just traced through the code like I now can. If you do, don’t waste your time on the very buggy XDEBUG and instead download Zend Debugger.

P.S. This is my former answer. I posted this before I realized what exactly Jared was asking. It is correct, but not related to his question. 🙂


I think what you are looking for is add_image_size():

add_image_size( $size_id, $width, $height, $crop );

For example:

add_image_size('headshot', 130, 150);
add_image_size('large-headshot', 260, 300);

By setting this WordPress will create those sizes automatically. What that what you needed?

Leave a Comment