本文整理汇总了PHP中_wp_relative_upload_path函数的典型用法代码示例。如果您正苦于以下问题:PHP _wp_relative_upload_path函数的具体用法?PHP _wp_relative_upload_path怎么用?PHP _wp_relative_upload_path使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_wp_relative_upload_path函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update_attached_file
/**
* Update attachment file path based on attachment ID.
*
* Used to update the file path of the attachment, which uses post meta name
* '_wp_attached_file' to store the path of the attachment.
*
* @since 2.1.0
* @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
*
* @param int $attachment_id Attachment ID
* @param string $file File path for the attachment
* @return bool False on failure, true on success.
*/
function update_attached_file($attachment_id, $file)
{
if (!get_post($attachment_id)) {
return false;
}
$file = apply_filters('update_attached_file', $file, $attachment_id);
$file = _wp_relative_upload_path($file);
return update_post_meta($attachment_id, '_wp_attached_file', $file);
}
示例2: insert_attachment
function insert_attachment($file, $id)
{
$dirs = wp_upload_dir();
$filetype = wp_check_filetype($file);
$attachment = array('guid' => $dirs['baseurl'] . '/' . _wp_relative_upload_path($file), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment, $file, $id);
//$attach_data=wp_generate_attachment_metadata($attach_id,$file);
//wp_update_attachment_metadata($attach_id,$attach_data);
return $attach_id;
}
示例3: hack_wp_generate_attachment_metadata
function hack_wp_generate_attachment_metadata($metadata, $attachment_id)
{
if (!isset($metadata['file'])) {
return $metadata;
}
$attachment = get_post($attachment_id);
$uploadPath = wp_upload_dir();
$file = path_join($uploadPath['basedir'], $metadata['file']);
$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) = wp_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'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
global $_wp_additional_image_sizes;
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = array('width' => '', 'height' => '', 'crop' => FALSE);
if (isset($_wp_additional_image_sizes[$s]['width'])) {
$sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
} else {
$sizes[$s]['width'] = get_option("{$s}_size_w");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['height'])) {
$sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$sizes[$s]['height'] = get_option("{$s}_size_h");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['crop'])) {
$sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']);
} else {
$sizes[$s]['crop'] = get_option("{$s}_crop");
}
// For default sizes set in options
}
foreach ($sizes as $size => $size_data) {
$resized = hack_image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop'], $size);
if ($resized) {
$metadata['sizes'][$size] = $resized;
}
}
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata($file);
if ($image_meta) {
$metadata['image_meta'] = $image_meta;
}
}
return $metadata;
}
示例4: wp_generate_attachment_metadata
/**
* 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 wp_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];
// Make the file path relative to the upload dir
$metadata['file'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
global $_wp_additional_image_sizes;
foreach ( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
else
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
else
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
else
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
if ( $sizes ) {
$editor = wp_get_image_editor( $file );
if ( ! is_wp_error( $editor ) )
$metadata['sizes'] = $editor->multi_resize( $sizes );
} else {
$metadata['sizes'] = array();
}
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata( $file );
if ( $image_meta )
$metadata['image_meta'] = $image_meta;
}
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
}
示例5: get_media_item
function get_media_item($media_id)
{
$media_item = get_post($media_id);
if (!$media_item || is_wp_error($media_item)) {
return new WP_Error('unknown_media', 'Unknown Media', 404);
}
$response = array('id' => strval($media_item->ID), 'date' => (string) $this->format_date($media_item->post_date_gmt, $media_item->post_date), 'parent' => $media_item->post_parent, 'link' => wp_get_attachment_url($media_item->ID), 'title' => $media_item->post_title, 'caption' => $media_item->post_excerpt, 'description' => $media_item->post_content, 'metadata' => wp_get_attachment_metadata($media_item->ID));
if (defined('IS_WPCOM') && IS_WPCOM && is_array($response['metadata']) && !empty($response['metadata']['file'])) {
remove_filter('_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10);
$response['metadata']['file'] = _wp_relative_upload_path($response['metadata']['file']);
add_filter('_wp_relative_upload_path', 'wpcom_wp_relative_upload_path', 10, 2);
}
$response['meta'] = (object) array('links' => (object) array('self' => (string) $this->get_media_link($this->api->get_blog_id_for_output(), $media_id), 'help' => (string) $this->get_media_link($this->api->get_blog_id_for_output(), $media_id, 'help'), 'site' => (string) $this->get_site_link($this->api->get_blog_id_for_output())));
return (object) $response;
}
示例6: wp_die
if ($value['size'] > $max_attach_size) {
wp_die(sprintf(__("File size exceeds %dMB. Most email providers will reject emails with attachments larger than %dMB. Please decrease the file size and try again.", 'visual-form-builder'), $size), '', array('back_link' => true));
}
// Options array for the wp_handle_upload function. 'test_form' => false
$upload_overrides = array('test_form' => false);
// We need to include the file that runs the wp_handle_upload function
require_once ABSPATH . 'wp-admin/includes/file.php';
// Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
$uploaded_file = wp_handle_upload($value, $upload_overrides);
// If the wp_handle_upload call returned a local path for the image
if (isset($uploaded_file['file'])) {
// Retrieve the file type from the file name. Returns an array with extension and mime type
$wp_filetype = wp_check_filetype(basename($uploaded_file['file']), null);
// Return the current upload directory location
$wp_upload_dir = wp_upload_dir();
$media_upload = array('guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path($uploaded_file['file']), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($uploaded_file['file'])), 'post_content' => '', 'post_status' => 'inherit');
// Insert attachment into Media Library and get attachment ID
$attach_id = wp_insert_attachment($media_upload, $uploaded_file['file']);
// Include the file that runs wp_generate_attachment_metadata()
require_once ABSPATH . 'wp-admin/includes/image.php';
// Setup attachment metadata
$attach_data = wp_generate_attachment_metadata($attach_id, $uploaded_file['file']);
// Update the attachment metadata
wp_update_attachment_metadata($attach_id, $attach_data);
$attachments['vfb-' . $field->field_id] = $uploaded_file['file'];
$data[] = array('id' => $field->field_id, 'slug' => $field->field_key, 'name' => $field->field_name, 'type' => $field->field_type, 'options' => $field->field_options, 'parent_id' => $field->field_parent, 'value' => $uploaded_file['url']);
$body .= sprintf('<tr>
<td><strong>%1$s: </strong></td>
<td><a href="%2$s">%2$s</a></td>
</tr>' . "\n", stripslashes($field->field_name), $uploaded_file['url']);
}
示例7: ajax_wpshop_add_entity
/**
* Add new entity element from anywhere
*/
function ajax_wpshop_add_entity()
{
global $wpdb;
check_ajax_referer('wpshop_add_new_entity_ajax_nonce', 'wpshop_ajax_nonce');
$attributes = array();
/** Get the attribute to create */
$attribute_to_reload = null;
if (!empty($_POST['attribute']['new_value_creation']) && is_array($_POST['attribute']['new_value_creation'])) {
foreach ($_POST['attribute']['new_value_creation'] as $attribute_code => $value) {
$query = $wpdb->prepare('SELECT * FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s', $attribute_code);
$attribute_def = $wpdb->get_row($query);
if ($value != "") {
if ($attribute_def->data_type_to_use == 'internal') {
$attribute_default_value = unserialize($attribute_def->default_value);
if ($attribute_default_value['default_value'] == WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS) {
$user_id = wp_create_user(sanitize_user($value), wp_generate_password(12, false));
$query = $wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_type = %s AND post_author = %d", WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, $user_id);
$attribute_option_id = $wpdb->get_var($query);
} else {
$entity_args = array('post_type' => $attribute_default_value['default_value'], 'post_title' => $value, 'post_author' => function_exists('is_user_logged_in') && is_user_logged_in() ? get_current_user_id() : 'NaN', 'comment_status' => 'closed');
$attribute_option_id = wp_insert_post($entity_args);
}
} else {
$wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'position' => 1, 'attribute_id' => $attribute_def->id, 'value' => $value, 'label' => $value));
$attribute_option_id = $wpdb->insert_id;
}
foreach ($_POST['attribute'] as $attribute => $val) {
foreach ($val as $k => $v) {
if ($k == $attribute_code) {
$_POST['attribute'][$attribute][$k] = $attribute_option_id;
}
}
}
}
}
}
/** Store send attribute into a new array for save purpose */
if (is_array($_POST['attribute'])) {
foreach ($_POST['attribute'] as $attribute_type => $attribute) {
foreach ($attribute as $attribute_code => $attribute_value) {
if (!isset($attributes[$attribute_code])) {
$attributes[$attribute_code] = $attribute_value;
}
}
}
}
/** Save the new entity into database */
$result = wpshop_entities::create_new_entity($_POST['entity_type'], $_POST['wp_fields']['post_title'], '', $attributes, array('attribute_set_id' => $_POST['attribute_set_id']));
$new_entity_id = $result[1];
if (!empty($new_entity_id)) {
/** Save address for current entity */
if (!empty($_POST['type_of_form']) && !empty($_POST['attribute'][$_POST['type_of_form']])) {
global $wpshop_account;
$result = wps_address::wps_address($_POST['type_of_form']);
update_post_meta($new_entity_id, '_wpshop_attached_address', $result['current_id']);
}
/** Make price calculation if entity is a product */
if ($_POST['entity_type'] == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) {
$wpshop_prices_attribute = unserialize(WPSHOP_ATTRIBUTE_PRICES);
$calculate_price = false;
foreach ($wpshop_prices_attribute as $attribute_price_code) {
if (array_key_exists($attribute_price_code, $attributes)) {
$calculate_price = true;
}
}
if ($calculate_price) {
wpshop_products::calculate_price($new_entity_id);
}
}
/** Add picture if a file has been send */
if (!empty($_FILES)) {
$wp_upload_dir = wp_upload_dir();
$final_dir = $wp_upload_dir['path'] . '/';
if (!is_dir($final_dir)) {
mkdir($final_dir, 0755, true);
}
foreach ($_FILES as $file) {
$tmp_name = $file['tmp_name']['post_thumbnail'];
$name = $file['name']['post_thumbnail'];
$filename = $final_dir . $name;
@move_uploaded_file($tmp_name, $filename);
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array('guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path($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, $new_entity_id);
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);
add_post_meta($new_entity_id, '_thumbnail_id', $attach_id, true);
}
}
echo json_encode(array(true, __('Element has been saved', 'wpshop'), $attribute_to_reload, $new_entity_id));
} else {
echo json_encode(array(false, __('An error occured while adding your element', 'wpshop')));
}
die;
}
示例8: update_attached_file
/**
* Update attachment file path based on attachment ID.
*
* Used to update the file path of the attachment, which uses post meta name
* '_wp_attached_file' to store the path of the attachment.
*
* @since 2.1.0
*
* @param int $attachment_id Attachment ID.
* @param string $file File path for the attachment.
* @return bool True on success, false on failure.
*/
function update_attached_file($attachment_id, $file)
{
if (!get_post($attachment_id)) {
return false;
}
/**
* Filter the path to the attached file to update.
*
* @since 2.1.0
*
* @param string $file Path to the attached file to update.
* @param int $attachment_id Attachment ID.
*/
$file = apply_filters('update_attached_file', $file, $attachment_id);
if ($file = _wp_relative_upload_path($file)) {
return update_post_meta($attachment_id, '_wp_attached_file', $file);
} else {
return delete_post_meta($attachment_id, '_wp_attached_file');
}
}
示例9: wp_generate_attachment_metadata
/**
* 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 wp_generate_attachment_metadata($attachment_id, $file)
{
$attachment = get_post($attachment_id);
$metadata = array();
$support = false;
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];
// Make the file path relative to the upload dir
$metadata['file'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
global $_wp_additional_image_sizes;
$sizes = array();
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = array('width' => '', 'height' => '', 'crop' => false);
if (isset($_wp_additional_image_sizes[$s]['width'])) {
$sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
} else {
$sizes[$s]['width'] = get_option("{$s}_size_w");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['height'])) {
$sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$sizes[$s]['height'] = get_option("{$s}_size_h");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['crop'])) {
$sizes[$s]['crop'] = intval($_wp_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);
if ($sizes) {
$editor = wp_get_image_editor($file);
if (!is_wp_error($editor)) {
$metadata['sizes'] = $editor->multi_resize($sizes);
}
} else {
$metadata['sizes'] = array();
}
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata($file);
if ($image_meta) {
$metadata['image_meta'] = $image_meta;
}
} elseif (preg_match('#^video/#', get_post_mime_type($attachment))) {
$metadata = wp_read_video_metadata($file);
$support = current_theme_supports('post-thumbnails', 'attachment:video') && post_type_supports('attachment:video', 'thumbnail');
} elseif (preg_match('#^audio/#', get_post_mime_type($attachment))) {
$metadata = wp_read_audio_metadata($file);
$support = current_theme_supports('post-thumbnails', 'attachment:audio') && post_type_supports('attachment:audio', 'thumbnail');
}
if ($support && !empty($metadata['image']['data'])) {
$ext = '.jpg';
switch ($metadata['image']['mime']) {
case 'image/gif':
$ext = '.gif';
break;
case 'image/png':
$ext = '.png';
break;
}
$basename = str_replace('.', '-', basename($file)) . '-image' . $ext;
$uploaded = wp_upload_bits($basename, '', $metadata['image']['data']);
if (false === $uploaded['error']) {
$attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => '');
$sub_attachment_id = wp_insert_attachment($attachment, $uploaded['file']);
$attach_data = wp_generate_attachment_metadata($sub_attachment_id, $uploaded['file']);
wp_update_attachment_metadata($sub_attachment_id, $attach_data);
update_post_meta($attachment_id, '_thumbnail_id', $sub_attachment_id);
}
}
// remove the blob of binary data from the array
unset($metadata['image']['data']);
return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id);
}
示例10: process
//.........这里部分代码省略.........
// [/featured image]
// [attachments]
if (!empty($uploads) and false === $uploads['error'] and !empty($attachments[$i]) and (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_attachments'])) {
// 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';
if (!is_array($attachments[$i])) {
$attachments[$i] = array($attachments[$i]);
}
foreach ($attachments[$i] as $attachment) {
if ("" == $attachment) {
continue;
}
$atchs = str_getcsv($attachment, $this->options['atch_delim']);
if (!empty($atchs)) {
foreach ($atchs as $atch_url) {
if (empty($atch_url)) {
continue;
}
$atch_url = str_replace(" ", "%20", trim($atch_url));
$attachment_filename = wp_unique_filename($uploads['path'], urldecode(basename(parse_url(trim($atch_url), PHP_URL_PATH))));
$attachment_filepath = $uploads['path'] . '/' . sanitize_file_name($attachment_filename);
$request = get_file_curl(trim($atch_url), $attachment_filepath);
if ((is_wp_error($request) or $request === false) and !@file_put_contents($attachment_filepath, @file_get_contents(trim($atch_url)))) {
$logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Attachment file %s cannot be saved locally as %s', 'pmxi_plugin'), trim($atch_url), $attachment_filepath));
is_wp_error($request) and $logger and call_user_func($logger, sprintf(__('<b>WP Error</b>: %s', 'pmxi_plugin'), $request->get_error_message()));
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
unlink($attachment_filepath);
// delete file since failed upload may result in empty file created
} elseif (!($wp_filetype = wp_check_filetype(basename($attachment_filename), null))) {
$logger and call_user_func($logger, sprintf(__('<b>WARNING</b>: Can\'t detect attachment file type %s', 'pmxi_plugin'), trim($atch_url)));
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
} else {
$attachment_data = array('guid' => $uploads['baseurl'] . '/' . _wp_relative_upload_path($attachment_filepath), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($attachment_filepath)), 'post_content' => '', 'post_status' => 'inherit', 'post_author' => $post_author[$i]);
$attach_id = $this->options['is_fast_mode'] ? pmxi_insert_attachment($attachment_data, $attachment_filepath, $pid) : wp_insert_attachment($attachment_data, $attachment_filepath, $pid);
if (is_wp_error($attach_id)) {
$logger and call_user_func($logger, __('<b>WARNING</b>', 'pmxi_plugin') . ': ' . $pid->get_error_message());
$logger and PMXI_Plugin::$session['pmxi_import']['warnings'] = ++PMXI_Plugin::$session->data['pmxi_import']['warnings'];
} else {
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $attachment_filepath));
do_action('pmxi_attachment_uploaded', $pid, $attid, $image_filepath);
}
}
}
}
}
}
// [/attachments]
// [custom taxonomies]
if (!empty($taxonomies)) {
foreach ($taxonomies as $tx_name => $txes) {
// Skip updating product attributes
if (PMXI_Admin_Addons::get_addon('PMWI_Plugin') and strpos($tx_name, "pa_") === 0) {
continue;
}
if (empty($articleData['ID']) or $this->options['update_all_data'] == "yes" or $this->options['update_all_data'] == "no" and $this->options['is_update_categories']) {
if (!empty($articleData['ID'])) {
if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "all_except" and !empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and in_array($tx_name, $this->options['taxonomies_list'])) {
continue;
}
if ($this->options['update_all_data'] == "no" and $this->options['update_categories_logic'] == "only" and (!empty($this->options['taxonomies_list']) and is_array($this->options['taxonomies_list']) and !in_array($tx_name, $this->options['taxonomies_list']) or empty($this->options['taxonomies_list']))) {
continue;
}
}
$assign_taxes = array();
if ($this->options['update_categories_logic'] == "add_new" and !empty($existing_taxonomies[$tx_name][$i])) {
示例11: wp_generate_attachment_metadata
/**
* Generate post thumbnail attachment meta data.
*
* @since 2.1.0
*
* @global array $_wp_additional_image_sizes
*
* @param int $attachment_id Attachment Id to process.
* @param string $file Filepath of the Attached image.
* @return mixed Metadata for attachment.
*/
function wp_generate_attachment_metadata($attachment_id, $file)
{
$attachment = get_post($attachment_id);
$metadata = array();
$support = false;
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];
// Make the file path relative to the upload dir.
$metadata['file'] = _wp_relative_upload_path($file);
// Make thumbnails and other intermediate sizes.
global $_wp_additional_image_sizes;
$sizes = array();
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = array('width' => '', 'height' => '', 'crop' => false);
if (isset($_wp_additional_image_sizes[$s]['width'])) {
$sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
} else {
$sizes[$s]['width'] = get_option("{$s}_size_w");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['height'])) {
$sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$sizes[$s]['height'] = get_option("{$s}_size_h");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['crop'])) {
$sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];
} else {
$sizes[$s]['crop'] = get_option("{$s}_crop");
}
// For default sizes set in options
}
/**
* Filter the image sizes automatically generated when uploading an image.
*
* @since 2.9.0
* @since 4.4.0 Added the `$metadata` argument.
*
* @param array $sizes An associative array of image sizes.
* @param array $metadata An associative array of image metadata: width, height, file.
*/
$sizes = apply_filters('intermediate_image_sizes_advanced', $sizes, $metadata);
if ($sizes) {
$editor = wp_get_image_editor($file);
if (!is_wp_error($editor)) {
$metadata['sizes'] = $editor->multi_resize($sizes);
}
} else {
$metadata['sizes'] = array();
}
// Fetch additional metadata from EXIF/IPTC.
$image_meta = wp_read_image_metadata($file);
if ($image_meta) {
$metadata['image_meta'] = $image_meta;
}
} elseif (wp_attachment_is('video', $attachment)) {
$metadata = wp_read_video_metadata($file);
$support = current_theme_supports('post-thumbnails', 'attachment:video') || post_type_supports('attachment:video', 'thumbnail');
} elseif (wp_attachment_is('audio', $attachment)) {
$metadata = wp_read_audio_metadata($file);
$support = current_theme_supports('post-thumbnails', 'attachment:audio') || post_type_supports('attachment:audio', 'thumbnail');
}
if ($support && !empty($metadata['image']['data'])) {
// Check for existing cover.
$hash = md5($metadata['image']['data']);
$posts = get_posts(array('fields' => 'ids', 'post_type' => 'attachment', 'post_mime_type' => $metadata['image']['mime'], 'post_status' => 'inherit', 'posts_per_page' => 1, 'meta_key' => '_cover_hash', 'meta_value' => $hash));
$exists = reset($posts);
if (!empty($exists)) {
update_post_meta($attachment_id, '_thumbnail_id', $exists);
} else {
$ext = '.jpg';
switch ($metadata['image']['mime']) {
case 'image/gif':
$ext = '.gif';
break;
case 'image/png':
$ext = '.png';
break;
}
$basename = str_replace('.', '-', basename($file)) . '-image' . $ext;
$uploaded = wp_upload_bits($basename, '', $metadata['image']['data']);
if (false === $uploaded['error']) {
$image_attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => '');
/**
* Filter the parameters for the attachment thumbnail creation.
*
//.........这里部分代码省略.........
示例12: generate_thumb_missing
/**
* Regenerate missing image dimensions for a particular attachment
* @param (int) attachment ID
* @return (array) new metadata for actual image
**/
public function generate_thumb_missing($att_id)
{
// Fetch the attachment
$att_raw = get_posts(array('include' => $att_id, 'post_type' => 'attachment', 'post_mime_type' => 'image'));
$att = $att_raw[0];
$file = get_attached_file($att_id);
// COMPLETE THIS SHIT.
$metadata = array();
if (preg_match('!^image/!', get_post_mime_type($att)) && file_is_displayable_image($file)) {
$imagesize = getimagesize($file);
$metadata['width'] = $imagesize[0];
$metadata['height'] = $imagesize[1];
list($uwidth, $uheight) = wp_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'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
global $_wp_additional_image_sizes;
foreach (get_intermediate_image_sizes() as $s) {
$sizes[$s] = array('width' => '', 'height' => '', 'crop' => FALSE);
if (isset($_wp_additional_image_sizes[$s]['width'])) {
$sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']);
} else {
$sizes[$s]['width'] = get_option("{$s}_size_w");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['height'])) {
$sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']);
} else {
$sizes[$s]['height'] = get_option("{$s}_size_h");
}
// For default sizes set in options
if (isset($_wp_additional_image_sizes[$s]['crop'])) {
$sizes[$s]['crop'] = intval($_wp_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);
// Only generate image if it does not already exist
$att_meta = wp_get_attachment_metadata($att_id);
foreach ($sizes as $size => $size_data) {
// Size already exists
if (isset($att_meta['sizes'][$size]) && file_exists($this->imageTrySize($file, $size_data['width'], $size_data['height']))) {
$metadata['sizes'][$size] = $att_meta['sizes'][$size];
} else {
// Generate new image
$resized = image_make_intermediate_size($file, $size_data['width'], $size_data['height'], $size_data['crop']);
if ($resized) {
$metadata['sizes'][$size] = $resized;
}
}
}
// Get image meta and update database manually
$metadata['image_meta'] = wp_read_image_metadata($file);
update_post_meta($att_id, "_wp_attachment_metadata", $metadata);
}
return $att;
}
示例13: dwsf_add_attachments
/**
* Upload images and create attachments
*
* @param $url string Url of image
*/
private function dwsf_add_attachments($post_id, $url, $caption)
{
$url = str_replace('https', 'http', $url);
// get upload directory
$upload_dir = wp_upload_dir();
$pathinfo = pathinfo($url);
$ext = $pathinfo['extension'];
$image_name = time() . '.' . $ext;
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $image_name;
} else {
$file = $upload_dir['basedir'] . '/' . $image_name;
}
//http://profile.ak.fbcdn.net/hprofile-ak-snc4/174867_6427302910_1109589_s.jpg
@copy($url, $file);
@unlink($url);
$wp_filetype = wp_check_filetype(basename($file), null);
$attachment = array('guid' => $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file), 'post_mime_type' => $wp_filetype['type'], 'post_title' => $caption, 'post_content' => $caption, 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
// 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';
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
示例14: ajax_multi_upload
public function ajax_multi_upload()
{
$postID = isset($_REQUEST["postID"]) ? intval($_REQUEST["postID"]) : 0;
check_ajax_referer('pe_theme_multi_upload');
$status = wp_handle_upload($_FILES['async-upload'], array('test_form' => true, 'action' => 'pe_theme_multi_upload'));
$filename = $status["file"];
$type = $status["type"];
//$wp_filetype = wp_check_filetype(basename($filename), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array('guid' => $wp_upload_dir['baseurl'] . "/" . _wp_relative_upload_path($filename), 'post_mime_type' => $type, 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment, $filename, $postID);
// 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';
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
//$post = wp_get_single_post($attach_id);
$post = get_post($attach_id);
$post->meta = $attach_data;
$tags = "Gallery {$postID}";
wp_set_post_terms($attach_id, $tags, PE_MEDIA_TAG, false);
$images = array($post);
$this->createPreviewThumbs($images);
header("Content-Type: application/json");
echo json_encode(array("images" => $images, "upload" => wp_upload_dir()));
die;
}
示例15: scalia_generate_thumbnail_src
function scalia_generate_thumbnail_src($attachment_id, $size)
{
if (in_array($size, array_keys(scalia_image_sizes()))) {
$filepath = get_attached_file($attachment_id);
$thumbFilepath = $filepath;
$image = wp_get_image_editor($filepath);
if (!is_wp_error($image) && $image) {
$thumbFilepath = $image->generate_filename($size);
if (!file_exists($thumbFilepath)) {
$scalia_image_sizes = scalia_image_sizes();
if (!is_wp_error($image) && isset($scalia_image_sizes[$size])) {
$image->resize($scalia_image_sizes[$size][0], $scalia_image_sizes[$size][1], $scalia_image_sizes[$size][2]);
$image = $image->save($image->generate_filename($size));
} else {
$thumbFilepath = $filepath;
}
}
}
$image = wp_get_image_editor($thumbFilepath);
if (!is_wp_error($image) && $image) {
$upload_dir = wp_upload_dir();
$sizes = $image->get_size();
return array($upload_dir['baseurl'] . '/' . _wp_relative_upload_path($thumbFilepath), $sizes['width'], $sizes['height']);
}
}
return wp_get_attachment_url($attachment_id, $size);
}