Example #1
0
/**
 * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 *
 * @since 2.6.0
 *
 * @param array $file_array Array similar to a {@link $_FILES} upload array
 * @param int $post_id The post ID the media is associated with
 * @param string $desc Description of the sideloaded file
 * @param array $post_data allows you to overwrite some of the attachment
 * @return int|object The ID of the attachment or a nxt_Error on failure
 */
function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array())
{
    $overrides = array('test_form' => false);
    $file = nxt_handle_sideload($file_array, $overrides);
    if (isset($file['error'])) {
        return new nxt_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\\.[^.]+$/', '', basename($file));
    $content = '';
    // use image exif/iptc data for title and caption defaults if possible
    if ($image_meta = @nxt_read_image_metadata($file)) {
        if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
            $title = $image_meta['title'];
        }
        if (trim($image_meta['caption'])) {
            $content = $image_meta['caption'];
        }
    }
    if (isset($desc)) {
        $title = $desc;
    }
    // Construct the attachment array
    $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data);
    // This should never be set as it would then overwrite an existing attachment.
    if (isset($attachment['ID'])) {
        unset($attachment['ID']);
    }
    // Save the attachment metadata
    $id = nxt_insert_attachment($attachment, $file, $post_id);
    if (!is_nxt_error($id)) {
        nxt_update_attachment_metadata($id, nxt_generate_attachment_metadata($id, $file));
    }
    return $id;
}
Example #2
0
/**
 * Generate post thumbnail attachment meta data.
 *
 * @since 2.1.0
 *
 * @param int $attachment_id Attachment Id to process.
 * @param string $file Filepath of the Attached image.
 * @return mixed Metadata for attachment.
 */
function nxt_generate_attachment_metadata($attachment_id, $file)
{
    $attachment = get_post($attachment_id);
    $metadata = array();
    if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) {
        $imagesize = getimagesize($file);
        $metadata['width'] = $imagesize[0];
        $metadata['height'] = $imagesize[1];
        list($uwidth, $uheight) = nxt_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
        $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
        // Make the file path relative to the upload dir
        $metadata['file'] = _nxt_relative_upload_path($file);
        // make thumbnails and other intermediate sizes
        global $_nxt_additional_image_sizes;
        foreach (get_intermediate_image_sizes() as $s) {
            $sizes[$s] = array('width' => '', 'height' => '', 'crop' => FALSE);
            if (isset($_nxt_additional_image_sizes[$s]['width'])) {
                $sizes[$s]['width'] = intval($_nxt_additional_image_sizes[$s]['width']);
            } else {
                $sizes[$s]['width'] = get_option("{$s}_size_w");
            }
            // For default sizes set in options
            if (isset($_nxt_additional_image_sizes[$s]['height'])) {
                $sizes[$s]['height'] = intval($_nxt_additional_image_sizes[$s]['height']);
            } else {
                $sizes[$s]['height'] = get_option("{$s}_size_h");
            }
            // For default sizes set in options
            if (isset($_nxt_additional_image_sizes[$s]['crop'])) {
                $sizes[$s]['crop'] = intval($_nxt_additional_image_sizes[$s]['crop']);
            } else {
                $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, $size_data['width'], $size_data['height'], $size_data['crop']);
            if ($resized) {
                $metadata['sizes'][$size] = $resized;
            }
        }
        // fetch additional metadata from exif/iptc
        $image_meta = nxt_read_image_metadata($file);
        if ($image_meta) {
            $metadata['image_meta'] = $image_meta;
        }
    }
    return apply_filters('nxt_generate_attachment_metadata', $metadata, $attachment_id);
}