本文整理汇总了PHP中media_handle_sideload函数的典型用法代码示例。如果您正苦于以下问题:PHP media_handle_sideload函数的具体用法?PHP media_handle_sideload怎么用?PHP media_handle_sideload使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了media_handle_sideload函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: media_sideload_image_1
function media_sideload_image_1($file, $post_id, $desc = null, $return = 'html')
{
if (!empty($file)) {
// Set variables for storage, fix file filename for query strings.
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
$file_array = array();
$file_array['name'] = basename($matches[0]);
// Download file to temp location.
$file_array['tmp_name'] = download_url($file);
// If error storing temporarily, return the error.
if (is_wp_error($file_array['tmp_name'])) {
return $file_array['tmp_name'];
}
// Do the validation and storage stuff.
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
/*$fullsize_path = get_attached_file( $id ); // Full path
if (function_exists('ewww_image_optimizer')) {
ewww_image_optimizer($fullsize_path, $gallery_type = 4, $converted = false, $new = true, $fullsize = true);
}*/
$src = wp_get_attachment_url($id);
}
if (!empty($src)) {
//update_post_meta($post_id, 'image_value', $src);
set_post_thumbnail($post_id, $id);
//update_post_meta($post_id, 'Thumbnail', $src);
return get_post_meta($post_id, 'image_value', true);
} else {
return new WP_Error('image_sideload_failed');
}
}
示例2: fabim_upload_action
function fabim_upload_action()
{
$image = $_POST['image'];
//Get the base-64 string from data
$filteredData = substr($_POST['image'], strpos($_POST['image'], ",") + 1);
//Decode the string
$unencodedData = base64_decode($filteredData);
$originalName = basename(parse_url('image_marker.png', PHP_URL_PATH));
$tempName = tempnam('/tmp', 'php_files');
$tempName = realpath($tempName);
file_put_contents($tempName, $unencodedData);
$data = array();
$data['name'] = $originalName;
$data['type'] = 'image/png';
$data['tmp_name'] = $tempName;
$data['error'] = 0;
$data['size'] = strlen($unencodedData);
$_FILES['image'] = array('name' => $originalName, 'type' => 'image/jpeg', 'tmp_name' => $tempName, 'error' => 0, 'size' => strlen($unencodedData));
$movefile = media_handle_sideload($_FILES['image'], 0);
if ($movefile && !isset($movefile['error'])) {
die('Done');
} else {
_e('Failed to recreating image, please try again', 'fabric-marker');
}
}
示例3: image
public static function image($post_id)
{
$images = array();
$xml = simplexml_load_file('https://unsplash.com/rss');
foreach ($xml->channel->item as $item) {
foreach ($item->image->url as $url) {
$images[] = (string) $url;
}
}
shuffle($images);
$url = $images[0];
$tmp = download_url($url);
$file_array = array('name' => 'new test image', 'tmp_name' => $tmp);
// Check for download errors
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
}
$image_id = media_handle_sideload($file_array, $post_id);
// Check for handle sideload errors.
if (is_wp_error($image_id)) {
@unlink($file_array['tmp_name']);
}
if (is_wp_error($image_id)) {
return $image_id;
}
}
示例4: wp_rp_upload_attachment
/**
* Cron - Thumbnail extraction
*/
function wp_rp_upload_attachment($url, $post_id)
{
/* Parts copied from wp-admin/includes/media.php:media_sideload_image */
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$tmp = download_url($url);
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $url, $matches);
$file_array['name'] = sanitize_file_name(urldecode(basename($matches[0])));
$file_array['tmp_name'] = $tmp;
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
return false;
}
$post_data = array('guid' => $url, 'post_title' => 'rp_' . $file_array['name']);
$attachment_id = media_handle_sideload($file_array, $post_id, null, $post_data);
if (is_wp_error($attachment_id)) {
@unlink($file_array['tmp_name']);
return false;
}
$attach_data = wp_get_attachment_metadata($attachment_id);
$platform_options = wp_rp_get_platform_options();
$min_width = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_WIDTH : WP_RP_THUMBNAILS_WIDTH;
$min_height = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_HEIGHT : WP_RP_THUMBNAILS_HEIGHT;
if (!$attach_data || $attach_data['width'] < $min_width || $attach_data['height'] < $min_height) {
wp_delete_attachment($attachment_id);
return false;
}
return $attachment_id;
}
示例5: largo_media_sideload_image
/**
* Similar to `media_sideload_image` except that it simply returns the attachment's ID on success
*
* @param (string) $file the url of the image to download and attach to the post
* @param (integer) $post_id the post ID to attach the image to
* @param (string) $desc an optional description for the image
*
* @since 0.5.2
*/
function largo_media_sideload_image($file, $post_id, $desc = null)
{
if (!empty($file)) {
include_once ABSPATH . 'wp-admin/includes/image.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/media.php';
// Set variables for storage, fix file filename for query strings.
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
$file_array = array();
$file_array['name'] = basename($matches[0]);
// Download file to temp location.
$file_array['tmp_name'] = download_url($file);
// If error storing temporarily, return the error.
if (is_wp_error($file_array['tmp_name'])) {
return $file_array['tmp_name'];
}
// Do the validation and storage stuff.
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
}
return $id;
}
}
示例6: setFeaturedImage
function setFeaturedImage($post_id, $url, $featuredImageTitle)
{
// Download file to temp location and setup a fake $_FILE handler
// with a new name based on the post_id
$tmp_name = download_url($url);
// echo $tmp_name;
$file_array['name'] = $post_id . '-thumb.jpg';
// new filename based on slug
$file_array['tmp_name'] = $tmp_name;
// If error storing temporarily, unlink
if (is_wp_error($tmp_name)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do validation and storage . Make a description based on the Post_ID
$attachment_id = media_handle_sideload($file_array, $post_id, 'Thumbnail for ' . $post_id);
// If error storing permanently, unlink
if (is_wp_error($attachment_id)) {
$error_string = $attachment_id->get_error_message();
@unlink($file_array['tmp_name']);
return;
}
// Set as the post attachment
$post_result = add_post_meta($post_id, '_thumbnail_id', $attachment_id, true);
// echo $post_result);
}
示例7: create_file
private function create_file($name, $vararray)
{
$string = $this->generate_csv_string($vararray);
$upload_dir = wp_upload_dir();
$file = $upload_dir['path'] . "/" . $name . ".csv";
file_put_contents($file, $string);
$url = $upload_dir['url'] . "/" . $name . ".csv";
$tmp = download_url($url);
$post_id = $this->options[$this->page_id_field];
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png|csv)/i', $url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
print_r($tmp);
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink
if (is_wp_error($id)) {
print_r($id);
@unlink($file_array['tmp_name']);
return $id;
}
$src = wp_get_attachment_url($id);
echo $src;
}
示例8: load_thumbnail
public function load_thumbnail($post_id, $url, $desc = null)
{
if (!empty($url)) {
// Download file to temp location
$tmp = download_url($url);
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
if (isset($matches[0])) {
$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'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
if (!is_wp_error($id)) {
return $id;
}
// If error storing permanently, unlink
@unlink($file_array['tmp_name']);
}
return false;
}
示例9: soundpress_add_track_details_to_post
/**
* Add track details when a post is saved.
*
* @param int $post_id The post ID.
* @param post $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function soundpress_add_track_details_to_post($post_id, $post, $update)
{
$trackurl = get_post_meta($post_id, 'soundpress_soundcloud_url', true);
if (wp_is_post_revision($post_id)) {
return;
}
if ($trackurl) {
$track_details = soundcloud_remote_get($trackurl);
if (is_object($track_details)) {
// Check for thumbnail. If not present, get the board Image
if (!has_post_thumbnail($post_id)) {
$thumbnailurl = $track_details->artwork_url;
if (empty($thumbnailurl)) {
$thumbnailurl = $track_details->user->avatar_url;
}
$tmp = download_url($thumbnailurl);
if (is_wp_error($tmp)) {
}
$desc = get_the_title($post_id);
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png)/i', $thumbnailurl, $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'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
set_post_thumbnail($post_id, $id);
}
// Check for Description
if ("" == get_the_content()) {
$postcontentarray = array('ID' => $post_id, 'post_content' => $track_details->description);
// Update the post into the database
wp_update_post($postcontentarray);
}
// Get The Duration
$durationseconds = $track_details->duration / 1000;
update_post_meta($post_id, 'podcast_duration', esc_attr($durationseconds));
if (TRUE == $track_details->downloadable) {
$download_url = esc_attr($track_details->download_url);
update_post_meta($post_id, 'podcast_download_url', $download_url);
}
}
}
}
示例10: insert_attachment
function insert_attachment($image, $post_id, $setthumb = 'false')
{
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
// use image exif/iptc data for title and caption defaults if possible
$array = array('name' => basename($image), 'type' => 'image/jpeg', 'tmp_name' => $image, 'error' => 0, 'size' => filesize($image));
$imageId = media_handle_sideload($array, $post_id);
if ($setthumb) {
update_post_meta($post_id, '_thumbnail_id', $imageId);
}
return $imageId;
}
示例11: after_save
public function after_save()
{
if (is_array($this->file_upload)) {
//do the upload
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$media_id = media_handle_sideload($this->file_upload['file'], $this->id, $this->content, array('post_status' => 'inherit'));
if (is_wp_error($media_id)) {
//todo log
//return $media_id;
} else {
//all good, store the value now
update_post_meta($this->id, '_file', $media_id);
}
}
}
示例12: loadImg
function loadImg($url)
{
// во фронтэнде нужны эти файлы
require_once ABSPATH . "wp-admin" . '/includes/image.php';
require_once ABSPATH . "wp-admin" . '/includes/file.php';
require_once ABSPATH . "wp-admin" . '/includes/media.php';
$file_array = array();
$tmp = download_url($url);
// корректируем умя файла в строках запроса.
$nameR = end(explode('.', end(explode('/', $url))));
$file_array['name'] = rand(100000, 999999) . '.' . $nameR;
$file_array['tmp_name'] = $tmp;
$id = media_handle_sideload($file_array, 0);
// удалим временный файл
@unlink($file_array['tmp_name']);
return $id;
}
示例13: sideload_icon
/**
* Get a fresh icon image from grabicon.com and cache it.
*/
public function sideload_icon()
{
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$grab_base_url = 'http://grabicon.com/icon';
$home_url_parts = parse_url(home_url());
$grab_url = add_query_arg(array('size' => $this->size, 'domain' => $home_url_parts['host'], 'origin' => $home_url_parts['host'], 'reset' => 'true', 'key' => self::$api_key), $grab_base_url);
$file_info = array('name' => 'prompt-site-icon-' . $this->size . '.png', 'tmp_name' => download_url($grab_url, 5));
if (is_wp_error($file_info['tmp_name'])) {
return;
}
$id = media_handle_sideload($file_info, 0);
if (!is_wp_error($id)) {
$this->attachment_id = $id;
}
}
示例14: wprss_media_sideload_image
/**
* An enhanced version of WP's media_sideload_image function.
*
* If media_sideload_image fails, the file is downloaded manually
* as an image, inserted as an attachment, and attached to the post.
*
* @since 3.5.1
*/
function wprss_media_sideload_image($file, $post_id, $desc = null)
{
try {
if (!empty($file)) {
// Download file to temp location
$tmp = download_url($file);
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
if (count($matches) > 0) {
$file_array['name'] = basename($matches[0]);
} else {
preg_match('/[\\/\\?\\=\\&]([^\\/\\?\\=\\&]*)[\\?]*$/i', $file, $matches2);
if (count($matches2) > 1) {
$file_array['name'] = $matches2[1] . '.png';
} else {
@unlink($tmp);
return "<img src='{$file}' alt='' />";
}
}
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return "<img src='{$file}' alt='' />";
}
$src = wp_get_attachment_url($id);
}
// Finally check to make sure the file has been saved, then return the html
if (!empty($src)) {
$alt = isset($desc) ? esc_attr($desc) : '';
$html = "<img src='{$src}' alt='{$alt}' />";
return $html;
}
} catch (Exception $e) {
return "<img src='{$file}' alt='' />";
}
}
示例15: upload
public function upload($url)
{
$tmp = download_url($url);
$file_array = ['name' => basename($url), 'tmp_name' => $tmp];
// Check for download errors
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
return false;
}
$id = media_handle_sideload($file_array, 0);
// Check for handle sideload errors.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return false;
}
$attachment_url = wp_get_attachment_url($id);
return $attachment_url;
// Do whatever you have to here
}