本文整理汇总了PHP中wp_update_attachment_metadata函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_update_attachment_metadata函数的具体用法?PHP wp_update_attachment_metadata怎么用?PHP wp_update_attachment_metadata使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_update_attachment_metadata函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle_upload
/**
* Upload
* Ajax callback function
*
* @return string Error or (XML-)response
*/
static function handle_upload()
{
check_admin_referer('rwmb-upload-images_' . $_REQUEST['field_id']);
$post_id = 0;
if (is_numeric($_REQUEST['post_id'])) {
$post_id = (int) $_REQUEST['post_id'];
}
// You can use WP's wp_handle_upload() function:
$file = $_FILES['async-upload'];
$file_attr = wp_handle_upload($file, array('test_form' => true, 'action' => 'plupload_image_upload'));
$attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit');
// Adds file as attachment to WordPress
$id = wp_insert_attachment($attachment, $file_attr['file'], $post_id);
if (!is_wp_error($id)) {
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file']));
// Save file ID in meta field
if (isset($_REQUEST['field_id'])) {
add_post_meta($post_id, $_REQUEST['field_id'], $id, false);
}
$response = new WP_Ajax_Response();
$response->add(array('what' => 'rwmb_image_response', 'data' => self::img_html($id)));
$response->send();
}
exit;
}
示例2: upload_file
/**
* @param strin $file_url
*
* @return int
*/
private function upload_file($file_url)
{
if (!filter_var($file_url, FILTER_VALIDATE_URL)) {
return false;
}
$contents = @file_get_contents($file_url);
if ($contents === false) {
return false;
}
$upload = wp_upload_bits(basename($file_url), null, $contents);
if (isset($upload['error']) && $upload['error']) {
return false;
}
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']);
$id = wp_insert_attachment($attachment, $upload['file']);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
return $id;
}
示例3: themify_make_image_size
/**
* Creates new image size.
*
* @uses get_attached_file()
* @uses image_make_intermediate_size()
* @uses wp_update_attachment_metadata()
* @uses get_post_meta()
* @uses update_post_meta()
*
* @param $attachment_id
* @param $width
* @param $height
* @param $meta
* @param $original_src
*
* @return array
*/
function themify_make_image_size($attachment_id, $width, $height, $meta, $original_src)
{
setlocale(LC_CTYPE, get_locale() . '.UTF-8');
$attached_file = get_attached_file($attachment_id);
if (apply_filters('themify_image_script_use_large_size', true) && isset($meta['sizes']['large']['file'])) {
$attached_file = str_replace($meta['file'], trailingslashit(dirname($meta['file'])) . $meta['sizes']['large']['file'], $attached_file);
}
$resized = image_make_intermediate_size($attached_file, $width, $height, true);
if ($resized && !is_wp_error($resized)) {
// Save the new size in meta data
$key = sprintf('resized-%dx%d', $width, $height);
$meta['sizes'][$key] = $resized;
$src = str_replace(basename($original_src), $resized['file'], $original_src);
wp_update_attachment_metadata($attachment_id, $meta);
// Save size in backup sizes so it's deleted when original attachment is deleted.
$backup_sizes = get_post_meta($attachment_id, '_wp_attachment_backup_sizes', true);
if (!is_array($backup_sizes)) {
$backup_sizes = array();
}
$backup_sizes[$key] = $resized;
update_post_meta($attachment_id, '_wp_attachment_backup_sizes', $backup_sizes);
// Return resized image url, width and height.
return array('url' => esc_url($src), 'width' => $width, 'height' => $height);
}
// Return resized image url, width and height.
return array('url' => $original_src, 'width' => $width, 'height' => $height);
}
示例4: custom_media_sideload_image
/**
* media_sideload_image, but returns ID
* @param string $image_url [description]
* @param boolean $post_id [description]
* @return [type] [description]
*/
function custom_media_sideload_image($image_url = '', $post_id = false)
{
require_once ABSPATH . 'wp-admin/includes/file.php';
$tmp = download_url($image_url);
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $image_url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
$time = current_time('mysql');
$file = wp_handle_sideload($file_array, array('test_form' => false), $time);
if (isset($file['error'])) {
return new WP_Error('upload_error', $file['error']);
}
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\\.[^.]+$/', '', basename($file));
$parent = (int) absint($post_id) > 0 ? absint($post_id) : 0;
$attachment = array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $parent, 'post_title' => $title, 'post_content' => '');
$id = wp_insert_attachment($attachment, $file, $parent);
if (!is_wp_error($id)) {
require_once ABSPATH . 'wp-admin/includes/image.php';
$data = wp_generate_attachment_metadata($id, $file);
wp_update_attachment_metadata($id, $data);
}
return $id;
}
示例5: it_regenerate_wp_images
function it_regenerate_wp_images()
{
return false;
set_time_limit(0);
echo "<pre>";
echo "Regenerating thumbnails...\n";
$args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null);
$attachments = get_posts($args);
if ($attachments) {
echo count($attachments) . " images were found.\n";
foreach ($attachments as $attachment) {
$full_size_path = get_attached_file($attachment->ID);
if (false === $full_size_path || !file_exists($full_size_path)) {
echo "Image ID " . $attachment->ID . " was not found.\n";
continue;
}
$meta_data = wp_generate_attachment_metadata($attachment->ID, $full_size_path);
if (is_wp_error($meta_data)) {
echo "Image ID " . $attachment->ID . " raised an error: " . $mata_data->get_error_message() . "\n";
continue;
}
if (empty($meta_data)) {
echo "Image ID " . $attachment->ID . " failed with unknown reason\n";
continue;
}
wp_update_attachment_metadata($attachment->ID, $meta_data);
}
echo "Done.";
}
echo "</pre>";
}
示例6: dswoddil_site_icon_upload
/**
* Upload theme default site icon
*
* @return void
*/
function dswoddil_site_icon_upload()
{
$image_name = 'site-icon';
if (!function_exists('wp_update_attachment_metadata')) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
if (!dswoddil_get_attachment_by_post_name('dswoddil-' . $image_name)) {
$site_icon = get_template_directory() . '/img/' . $image_name . '.png';
// create $file array with the indexes show below
$file['name'] = $site_icon;
$file['type'] = 'image/png';
// get image size
$file['size'] = filesize($file['name']);
$file['tmp_name'] = $image_name . '.png';
$file['error'] = 1;
$file_content = file_get_contents($site_icon);
$upload_image = wp_upload_bits($file['tmp_name'], null, $file_content);
// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($upload_image['file']), null);
$attachment = array('guid' => $upload_image['file'], 'post_mime_type' => $filetype['type'], 'post_title' => 'dswoddil-' . $image_name, 'post_content' => '', 'post_status' => 'inherit');
//insert wordpress attachment of uploaded image to get attachmen ID
$attachment_id = wp_insert_attachment($attachment, $upload_image['file']);
//generate attachment thumbnail
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $upload_image['file']));
add_post_meta($attachment_id, '_wp_attachment_context', $image_name);
}
}
示例7: circleflip_make_missing_intermediate_size
/** generate new image sizes for old uploads
*
* @param mixed $out
* @param int $id
* @param string|array $size
* @return mixed
*/
function circleflip_make_missing_intermediate_size($out, $id, $size)
{
$skip_sizes = array('full', 'large', 'medium', 'thumbnail', 'thumb');
if (is_array($size) || in_array($size, $skip_sizes)) {
return $out;
}
// the size exists and the attachment doesn't have a pre-generated file of that size
// or the intermediate size defintion changed ( during development )
if (circleflip_intermediate_size_exists($size) && (!circleflip_image_has_intermediate_size($id, $size) || circleflip_has_intermediate_size_changed($id, $size))) {
// get info registerd by add_image_size
$size_info = circleflip_get_intermediate_size_info($size);
// get path to the original file
$file_path = get_attached_file($id);
// resize the image
$resized_file = image_make_intermediate_size($file_path, $size_info['width'], $size_info['height'], $size_info['crop']);
if ($resized_file) {
// we have a resized image, get the attachment metadata and add the new size to it
$metadata = wp_get_attachment_metadata($id);
$metadata['sizes'][$size] = $resized_file;
if (wp_update_attachment_metadata($id, $metadata)) {
// update succeded, call image_downsize again , DRY
return image_downsize($id, $size);
}
} else {
// failed to generate the resized image
// call image_downsize with `full` to prevent infinit loop
return image_downsize($id, 'full');
}
}
// pre-existing intermediate size
return $out;
}
示例8: import
public function import($attachment)
{
$saved_image = $this->_return_saved_image($attachment);
if ($saved_image) {
return $saved_image;
}
// Extract the file name and extension from the url
$filename = basename($attachment['url']);
if (function_exists('file_get_contents')) {
$options = ['http' => ['user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64; rv:49.0) Gecko/20100101 Firefox/49.0']];
$context = stream_context_create($options);
$file_content = file_get_contents($attachment['url'], false, $context);
} else {
$file_content = wp_remote_retrieve_body(wp_safe_remote_get($attachment['url']));
}
if (empty($file_content)) {
return false;
}
$upload = wp_upload_bits($filename, null, $file_content);
$post = ['post_title' => $filename, 'guid' => $upload['url']];
$info = wp_check_filetype($upload['file']);
if ($info) {
$post['post_mime_type'] = $info['type'];
} else {
// For now just return the origin attachment
return $attachment;
//return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'elementor' ) );
}
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
update_post_meta($post_id, '_elementor_source_image_hash', $this->_get_hash_image($attachment['url']));
$new_attachment = ['id' => $post_id, 'url' => $upload['url']];
$this->_replace_image_ids[$attachment['id']] = $new_attachment;
return $new_attachment;
}
示例9: process_attachment
function process_attachment($post, $url)
{
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
//if ( preg_match( '|^/[\w\W]+$|', $url ) )
// $url = rtrim( $this->base_url, '/' ) . $url;
global $url_remap;
$upload = fetch_remote_file($url, $post);
if (is_wp_error($upload)) {
return $upload;
}
if ($info = wp_check_filetype($upload['file'])) {
$post['post_mime_type'] = $info['type'];
} else {
return new WP_Error('attachment_processing_error', __('Invalid file type', 'wordpress-importer'));
}
$post['guid'] = $upload['url'];
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
if (preg_match('!^image/!', $info['type'])) {
$parts = pathinfo($url);
$name = basename($parts['basename'], ".{$parts['extension']}");
// PATHINFO_FILENAME in PHP 5.2
$parts_new = pathinfo($upload['url']);
$name_new = basename($parts_new['basename'], ".{$parts_new['extension']}");
$url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
}
return $post_id;
}
示例10: add_image
static function add_image($image_url)
{
if (empty($image_url)) {
return FALSE;
}
// Add Featured Image to Post
$upload_dir = wp_upload_dir();
// Set upload folder
$image_data = file_get_contents($image_url);
// Get image data
$filename = basename($image_url);
// Create image file name
// Check folder permission and define file location
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents($file, $image_data);
// Check image file type
$wp_filetype = wp_check_filetype($filename, NULL);
// Set attachment data
$attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit');
// Create the attachment
$attach_id = wp_insert_attachment($attachment, $file);
// Include image.php
require_once ABSPATH . 'wp-admin/includes/image.php';
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
// Assign metadata to attachment
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
示例11: wpua_avatar_upload
public function wpua_avatar_upload($file)
{
$filetype = wp_check_filetype($file->name);
$media_upload = array();
$media_upload['file'] = array('name' => $file->name, 'type' => $filetype['type'], 'tmp_name' => $file->path, 'error' => 0, 'size' => filesize($file->path));
$media_file = wp_handle_upload($media_upload['file'], array('test_form' => false, 'test_upload' => false, 'action' => 'custom_action'));
if ($media_file['file']) {
$url = $media_file['url'];
$filepath = $media_file['file'];
if ($image_meta = @wp_read_image_metadata($filepath)) {
if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
$title = $image_meta['title'];
}
}
$attachment = array('guid' => $url, 'post_mime_type' => $filetype['type'], 'post_title' => $title);
$attachment_id = wp_insert_attachment($attachment, $filepath);
if (!is_wp_error($attachment_id)) {
$this->delete_attachment_by_user($this->user_id);
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $filepath));
update_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $this->user_id);
$arr = wp_get_attachment_image_src($attachment_id, 'full');
$this->avatar_url = $arr[0];
$this->avatar_filename = basename($filepath);
$this->resource = $attachment_id;
$saved = $this->save();
if (!$saved) {
$this->delete_attachment($attachment_id);
return $saved;
}
return $saved;
}
} else {
return WP_Error('file_upload_problem', __("Media avatar could't uploading please check you have right permission for uploads folder.", 'wp-user-avatar-pro'));
}
}
示例12: regenerateThumbnails
public function regenerateThumbnails($image_id)
{
require_once 'wp-admin/includes/image.php';
$fullsizepath = get_attached_file($image_id);
$metadata = wp_generate_attachment_metadata($image_id, $fullsizepath);
wp_update_attachment_metadata($image_id, $metadata);
}
示例13: atp_plupload_action
function atp_plupload_action()
{
// check ajax noonce
$imgid = $_POST["imgid"];
check_ajax_referer($imgid . 'pluploadan');
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
// handle file upload
$filename = $_FILES[$imgid . 'async-upload']['name'];
$status = wp_handle_upload($_FILES[$imgid . 'async-upload'], array('test_form' => true, 'action' => 'plupload_action'));
if (!isset($status['file'])) {
continue;
}
$file_name = $status['file'];
$name_parts = pathinfo($file_name);
$name = trim(substr($filename, 0, -(1 + strlen($name_parts['extension']))));
$attachment = array('post_mime_type' => $status['type'], 'guid' => $status['url'], 'post_parent' => $post_id, 'post_title' => $name, 'post_content' => '');
$id = wp_insert_attachment($attachment, $file_name, $post_id);
if (!is_wp_error($id)) {
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_name));
$new[] = $id;
}
// send the uploaded file url in response
$upload_path = wp_get_attachment_url($id, true);
if (preg_match_all('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $upload_path, $matches)) {
$image_attributes = wp_get_attachment_image_src($id, 'thumbnail');
// returns an array
$uplaod_url = $image_attributes[0];
} else {
$uplaod_url = $upload_path;
}
$imagetest = array('url' => $status['url'], 'link' => get_edit_post_link($id), 'audioid' => $id, 'name' => $name, 'img' => $uplaod_url);
echo json_encode($imagetest);
//print_r ($imagetest);
exit;
}
示例14: post_attachment
static function post_attachment($download, $product_id)
{
// $filename should be the path to a file in the upload directory.
// example $filename = 'woocommerce_uploads/2015/07/aka_Matrix42_Tool_CleanDatabase_7.2.1.20150625.aka.zip';
$download = json_decode($download);
$filename = $download->file;
$web_url = $download->web_url;
$wp_upload_dir = wp_upload_dir();
$file = $wp_upload_dir['path'] . '/' . $filename;
//Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($file), null);
$attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($file), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file)), 'post_content' => '', 'post_status' => 'inherit');
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $file, $product_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, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
// Get all existing post downloads and
$files = array();
$wc_product = wc_get_product($product_id);
$untyped_array_of_downloads = $wc_product->get_files();
foreach ($untyped_array_of_downloads as $download_key => $download_value) {
$download_name = $download_value['name'];
$download_url = $download_value['file'];
$files[md5($download_url)] = array('name' => $download_name, 'file' => $download_url);
}
// Extend the existing post downloads by the new one
$files[md5($download_url)] = array('name' => $filename, 'file' => $web_url);
// Update post meta (_downloadable_files)
update_post_meta($product_id, '_downloadable_files', $files);
return 1;
}
示例15: create_png
function create_png()
{
$attachment = array('guid' => 'http://example.org/wp-content/uploads/2014/12/logo.png', 'post_mime_type' => 'image/png', 'post_title' => 'PNG', 'post_status' => 'inherit');
$img = wp_insert_attachment($attachment, 'logo.png');
wp_update_attachment_metadata($img, array('width' => 2448, 'height' => 3264, 'file' => '2014/12/logo.png', 'sizes' => array('thumbnail' => array('file' => 'logo-480x640.png', 'width' => 480, 'height' => 640), 'medium' => array('file' => 'logo-600x800.png', 'width' => 600, 'height' => 800), 'large' => array('file' => 'logo-1024x1365.png', 'width' => 1024, 'height' => 1365))));
return $img;
}