/** * Loads a post content template based off the post type and/or the post format. This functionality is * not feasible with the WordPress get_template_part() function, so we have to rely on some custom logic * and locate_template(). * * Note that using this function assumes that you're creating a content template to handle attachments. * This filter must be removed since we're bypassing the WP template hierarchy and focusing on templates * specific to the content. * * @since 1.6.0 * @access public * @return string */ function hybrid_get_content_template() { /* Set up an empty array and get the post type. */ $templates = array(); $post_type = get_post_type(); /* Assume the theme developer is creating an attachment template. */ if ('attachment' === $post_type) { remove_filter('the_content', 'prepend_attachment'); $mime_type = get_post_mime_type(); list($type, $subtype) = false !== strpos($mime_type, '/') ? explode('/', $mime_type) : array($mime_type, ''); $templates[] = "content-attachment-{$type}.php"; $templates[] = "content/attachment-{$type}.php"; } /* If the post type supports 'post-formats', get the template based on the format. */ if (post_type_supports($post_type, 'post-formats')) { /* Get the post format. */ $post_format = get_post_format() ? get_post_format() : 'standard'; /* Template based off post type and post format. */ $templates[] = "content-{$post_type}-{$post_format}.php"; $templates[] = "content/{$post_type}-{$post_format}.php"; /* Template based off the post format. */ $templates[] = "content-{$post_format}.php"; $templates[] = "content/{$post_format}.php"; } /* Template based off the post type. */ $templates[] = "content-{$post_type}.php"; $templates[] = "content/{$post_type}.php"; /* Fallback 'content.php' template. */ $templates[] = 'content.php'; $templates[] = 'content/content.php'; /* Allow devs to filter the content template hierarchy. */ $templates = apply_filters('hybrid_content_template_hierarchy', $templates); /* Apply filters and return the found content template. */ include apply_filters('hybrid_content_template', locate_template($templates, false, false)); }
function sfc_media_find_images($post, $content = '') { if (empty($content)) { $content = apply_filters('the_content', $post->post_content); } $images = array(); // we get the post thumbnail, put it first in the image list if (current_theme_supports('post-thumbnails') && has_post_thumbnail($post->ID)) { $thumbid = get_post_thumbnail_id($post->ID); $att = wp_get_attachment_image_src($thumbid, 'full'); if (!empty($att[0])) { $images[] = $att[0]; } } if (is_attachment() && preg_match('!^image/!', get_post_mime_type($post))) { $images[] = wp_get_attachment_url($post->ID); } // now search for images in the content itself if (preg_match_all('/<img\\s+(.+?)>/i', $content, $matches)) { foreach ($matches[1] as $match) { foreach (wp_kses_hair($match, array('http')) as $attr) { $img[strtolower($attr['name'])] = $attr['value']; } if (isset($img['src'])) { if (!isset($img['class']) || isset($img['class']) && false === straipos($img['class'], apply_filters('sfc_img_exclude', array('wp-smiley')))) { // ignore smilies if (!in_array($img['src'], $images) && strpos($img['src'], 'fbcdn.net') === false && strpos($img['src'], '/plugins/') === false) { $images[] = $img['src']; } } } } } return $images; }
public function wp_generate_attachment_metadata($metadata, $attachment_id) { $attachment = get_attached_file($attachment_id); if (!preg_match('!^image/!', get_post_mime_type($attachment_id)) || !file_is_displayable_image($attachment)) { return $metadata; } $image_sizes = Helper::get_image_sizes(); foreach ($image_sizes as $size_name => $size_attributes) { if (!empty($metadata['sizes'][$size_name])) { continue; } $image = wp_get_image_editor($attachment); if (is_wp_error($image)) { continue; } $resized = $image->resize($size_attributes['width'], $size_attributes['height'], $size_attributes['crop']); $image_size = $image->get_size(); if (!is_wp_error($resized) && !empty($image_size)) { $filename = wp_basename($image->generate_filename()); $extension = pathinfo($filename, PATHINFO_EXTENSION); $mime_type = $this->get_mime_type($extension); $metadata['sizes'][$size_name] = array('file' => $filename, 'width' => $image_size['width'], 'height' => $image_size['height'], 'mime-type' => $mime_type); } } return $metadata; }
/** * Check whether Image should be resized or not * * @param string $params * @param string $action * * @return bool */ private function should_resize($id = '') { //If resizing not enabled, or if both max width and height is set to 0, return if (!$this->resize_enabled || $this->max_w == 0 && $this->max_h == 0) { return false; } $file_path = get_attached_file($id); if (!empty($file_path)) { // Skip: if "noresize" is included in the filename, Thanks to Imsanity if (strpos($file_path, 'noresize') !== false) { return false; } //If file doesn't exists, return if (!file_exists($file_path)) { return false; } } //Check for a supported mime type global $wpsmushit_admin; //Get image mime type $mime = get_post_mime_type($id); $mime_supported = in_array($mime, $wpsmushit_admin->mime_types); //If type of upload doesn't matches the criteria return if (!empty($mime) && !($mime_supported = apply_filters('wp_smush_resmush_mime_supported', $mime_supported, $mime))) { return false; } //Check if already resized $resize_meta = get_post_meta($id, WP_SMUSH_PREFIX . 'resize_savings', true); if (!empty($resize_meta)) { return false; } return true; }
public function compress_attachment($metadata, $attachment_id) { $mime_type = get_post_mime_type($attachment_id); $tiny_metadata = new Tiny_Metadata($attachment_id); if ($this->compressor === null || strpos($mime_type, 'image/') !== 0) { return $metadata; } $path_info = pathinfo($metadata['file']); $upload_dir = wp_upload_dir(); $prefix = $upload_dir['basedir'] . '/' . $path_info['dirname'] . '/'; if (!$tiny_metadata->is_compressed()) { try { $response = $this->compressor->compress_file("{$prefix}{$path_info['basename']}"); $tiny_metadata->add_response($response); } catch (Tiny_Exception $e) { $tiny_metadata->add_exception($e); } } $settings = $this->settings->get_sizes(); foreach ($metadata['sizes'] as $size => $info) { if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) { try { $response = $this->compressor->compress_file("{$prefix}{$info['file']}"); $tiny_metadata->add_response($response, $size); } catch (Tiny_Exception $e) { $tiny_metadata->add_exception($e, $size); } } } $tiny_metadata->update(); return $metadata; }
function render_file($id = null) { if (!$id) { echo ""; return; } // vars $file_src = wp_get_attachment_url($id); preg_match("~[^/]*\$~", $file_src, $file_name); $class = "active"; ?> <ul class="hl"> <li data-mime="<?php echo get_post_mime_type($id); ?> "> <img class="acf-file-icon" src="<?php echo wp_mime_type_icon($id); ?> " alt=""/> </li> <li> <span class="acf-file-name"><?php echo $file_name[0]; ?> </span><br /> <a href="javascript:;" class="acf-file-delete"><?php _e('Remove File', 'acf'); ?> </a> </li> </ul> <?php }
/** * wp_generate_attachment_metadata() - Generate post Image attachment Metadata * * @package WordPress * @internal Missing Long Description * @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]; list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']); $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'"; $metadata['file'] = $file; // make thumbnails and other intermediate sizes $sizes = array('thumbnail', 'medium'); $sizes = apply_filters('intermediate_image_sizes', $sizes); foreach ($sizes as $size) { $resized = image_make_intermediate_size($file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop")); 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 apply_filters('wp_generate_attachment_metadata', $metadata); }
function grayscale_check_grayscale_image($metadata, $attachment_id) { global $_wp_additional_image_sizes; $attachment = get_post($attachment_id); if (preg_match('!image!', get_post_mime_type($attachment))) { require_once 'class-grayscale-image-editor.php'; foreach ($_wp_additional_image_sizes as $size => $size_data) { if (isset($size_data['grayscale']) && $size_data['grayscale']) { if (is_array($metadata['sizes']) && isset($metadata['sizes'][$size])) { $file = pathinfo(get_attached_file($attachment_id)); $filename = $file['dirname'] . '/' . $metadata['sizes'][$size]['file']; $metadata['sizes'][$size . '-gray'] = $metadata['sizes'][$size]; } else { // this size has no image attached, probably because the original is too small // create the grayscale image from the original file $file = wp_upload_dir(); $filename = $file['basedir'] . '/' . $metadata['file']; $metadata['sizes'][$size . '-gray'] = array('width' => $metadata['width'], 'height' => $metadata['height']); } $image = new Grayscale_Image_Editor($filename); $image->load(); $image->make_grayscale(); $result = $image->save($image->generate_filename('gray')); $metadata['sizes'][$size . '-gray']['file'] = $result['file']; } } } return $metadata; }
/** * Check if the attachment is a 'video'. * * @author Justin Tadlock <*****@*****.**> * @author Cherry Team <*****@*****.**> * @since 4.0.0 * @param int $post_id Attachment ID. * @return bool */ function cherry_attachment_is_video($post_id = null) { $post_id = null === $post_id ? get_the_ID() : $post_id; $mime_type = get_post_mime_type($post_id); list($type, $subtype) = false !== strpos($mime_type, '/') ? explode('/', $mime_type) : array($mime_type, ''); return 'video' === $type ? true : false; }
private function compress($metadata, $attachment_id) { $mime_type = get_post_mime_type($attachment_id); $tiny_metadata = new Tiny_Metadata($attachment_id, $metadata); if ($this->settings->get_compressor() === null || strpos($mime_type, 'image/') !== 0) { return array($tiny_metadata, null); } $success = 0; $failed = 0; $compressor = $this->settings->get_compressor(); $active_tinify_sizes = $this->settings->get_active_tinify_sizes(); $uncompressed_sizes = $tiny_metadata->get_uncompressed_sizes($active_tinify_sizes); foreach ($uncompressed_sizes as $uncompressed_size) { try { $tiny_metadata->add_request($uncompressed_size); $tiny_metadata->update(); $resize = $tiny_metadata->is_resizable($uncompressed_size) ? $this->settings->get_resize_options() : false; $response = $compressor->compress_file($tiny_metadata->get_filename($uncompressed_size), $resize); $tiny_metadata->add_response($response, $uncompressed_size); $tiny_metadata->update(); $success++; } catch (Tiny_Exception $e) { $tiny_metadata->add_exception($e, $uncompressed_size); $tiny_metadata->update(); $failed++; } } return array($tiny_metadata, array('success' => $success, 'failed' => $failed)); }
/** * Adds all routes to the existing HTTP API instance */ protected function __addRoutes() { // Regenerate all sizes for all images $this->http_api->post('regenerate-all(/)', function () { // Set time limit of 30 minutes set_time_limit(60 * 30); $response = []; $query = new \WP_Query(['fields' => 'ids', 'post_type' => 'attachment', 'post_status' => 'inherit', 'numberposts' => -1]); if ($query->posts) { foreach ($query->posts as $id) { if (strpos(get_post_mime_type($id), 'image/') !== false) { $image = new Image($id); $response[] = ['id' => $id, 'status' => $image->generateAllSizes()]; } } } return $response; }); // Returns status about all sizes or a single one, for a single image $this->http_api->get(':id/status(/:size)(/)', function ($id, $size = null) { $image = new Image($id); return $image->getStatus($size); }); // Generate all sizes for a single image $this->http_api->post(':id/generate-all(/)', function ($id) { $image = new Image($id); return $image->generateAllSizes(); }); // Generate a single image size $this->http_api->post(':id/generate/:size(/)', function ($id, $size) { $image = new Image($id); return $image->generateSize($size); }); }
/** * Checks if the current post has a mime type of 'video'. * * @since 1.6.0 * @access public * @param int $post_id * @return bool */ function hybrid_attachment_is_video($post_id = 0) { $post_id = empty($post_id) ? get_the_ID() : $post_id; $mime = get_post_mime_type($post_id); $mime_type = explode('/', $mime); return 'video' == array_shift($mime_type) ? true : false; }
/** * Body Class - Thanks to Theme Hyprid (http://themehybrid.com/) */ function stormbringer_body_class($classes = '') { global $wp_query; global $current_user; // User role $current_user->ID; $user = new WP_User($current_user->ID); // $user->roles foreach ($user->roles as $role) { $classes[] = 'role-' . $role; } /* Text direction (which direction does the text flow). */ $classes[] = 'wordpress'; $classes[] = get_bloginfo('text_direction'); $classes[] = get_locale(); /* Check if the current theme is a parent or child theme. */ $classes[] = is_child_theme() ? 'child-theme' : 'parent-theme'; /* Multisite check adds the 'multisite' class and the blog ID. */ if (is_multisite()) { $classes[] = 'multisite'; $classes[] = 'blog-' . get_current_blog_id(); } /* Date classes. */ $time = time() + get_option('gmt_offset') * 3600; $classes[] = strtolower(gmdate('\\yY \\mm \\dd \\hH l', $time)); /* Is the current user logged in. */ $classes[] = is_user_logged_in() ? 'logged-in' : 'logged-out'; /* WP admin bar. */ if (is_admin_bar_showing()) { $classes[] = 'admin-bar'; } /* Merge base contextual classes with $classes. */ $classes = array_merge($classes, stormbringer_get_context()); /* Singular post (post_type) classes. */ if (is_singular()) { /* Get the queried post object. */ $post = get_queried_object(); /* Checks for custom template. */ $template = str_replace(array("{$post->post_type}-template-", "{$post->post_type}-", '.php'), '', get_post_meta(get_queried_object_id(), "_wp_{$post->post_type}_template", true)); if (!empty($template)) { $classes[] = "{$post->post_type}-template-{$template}"; } /* Post format. */ if (current_theme_supports('post-formats') && post_type_supports($post->post_type, 'post-formats')) { $post_format = get_post_format(get_queried_object_id()); $classes[] = empty($post_format) || is_wp_error($post_format) ? "{$post->post_type}-format-standard" : "{$post->post_type}-format-{$post_format}"; } /* Attachment mime types. */ if (is_attachment()) { foreach (explode('/', get_post_mime_type()) as $type) { $classes[] = "attachment-{$type}"; } } } /* Paged views. */ if ((($page = $wp_query->get('paged')) || ($page = $wp_query->get('page'))) && $page > 1) { $classes[] = 'paged paged-' . intval($page); } return $classes; }
function bpmn_media_to_shortcode($html, $id, $attachment) { $mime = get_post_mime_type($id); if ($mime == 'application/bpmn+xml') { $html = '[bpmn url="' . $attachment['url'] . '"]'; } return $html; }
function bpmn_media_to_editor($html, $id, $attachment) { $mime = get_post_mime_type($id); if ($mime == 'application/bpmn+xml') { $html = bpmnio_file_html($attachment['url']); } return $html; }
function jd_is_svg($id) { if (!is_numeric($id)) { return false; } $id = (int) $id; return get_post_mime_type($id) == 'image/svg+xml' ? true : false; }
function batch_update_image_tags($is_update) { $total = 0; $created = 0; $updated = 0; $deleted = 0; $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null); //Get all attachment posts $attachments = get_posts($args); //if there are posts if ($attachments) { $sit_settings = get_option('sit_settings'); if ($sit_settings['enable_pdf']) { $pdf = true; $pdf_mine = 'pdf'; } $image_mime = 'image'; //Loop thru each attachment foreach ($attachments as $post) { //get post data ready,set title var to post title setup_postdata($post); $title = sanitize_text_field(get_the_title($post->ID)); $type = get_post_mime_type($post->ID); $tag = sanitize_text_field(get_post_meta($post->ID, '_wp_attachment_image_alt', true)); $tag_str = strval($tag); $tag_len = strlen($tag_str); //echo $type; if (strpos($type, $image_mime) !== false) { if ($is_update == True) { //if has post meta for alt tag, update it else add it. if (!add_post_meta($post->ID, '_wp_attachment_image_alt', $title, true)) { if (empty($tag) || $tag_len <= 2 && $tag_str !== $title) { update_post_meta($post->ID, '_wp_attachment_image_alt', $title); $updated++; } } else { $created++; //update counter } } else { //if has post meta for alt tag, update it else add it. if (!empty($tag)) { delete_post_meta($post->ID, '_wp_attachment_image_alt', $title); $deleted++; //update counter } //end add_post_meta } $total++; } } //end foreach } //end attachments $count = array('total' => $total, 'created' => $created, 'updated' => $updated, 'deleted' => $deleted); //count of files updated return $count; }
public static function getFeaturedImage() { $media_att_id = get_post_thumbnail_id(get_the_ID()); if ('image' == substr(get_post_mime_type($media_att_id), 0, 5)) { return wp_get_attachment_url($media_att_id); } else { return null; } }
function sp_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) = wp_shrink_dimensions($metadata['width'], $metadata['height']); $metadata['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'"; // Make the file path relative to the upload dir $metadata['file'] = $attachment->post_title; // make thumbnails and other intermediate sizes global $_wp_additional_image_sizes; $temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes)) { $temp_sizes = array_merge($temp_sizes, array_keys($_wp_additional_image_sizes)); } $temp_sizes = apply_filters('intermediate_image_sizes', $temp_sizes); foreach ($temp_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); 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 = wp_read_image_metadata($file); if ($image_meta) { $metadata['image_meta'] = $image_meta; } } return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id); }
/** * Returns HTML for Digital Download UI * * @param int $product_id * @return HTML */ function wpsc_select_product_file($product_id = null) { global $wpdb; $product_id = absint($product_id); $file_list = wpsc_uploaded_files(); $args = array('post_type' => 'wpsc-product-file', 'post_parent' => $product_id, 'numberposts' => -1, 'post_status' => 'all'); $attached_files = (array) get_posts($args); $output = '<table id="wpsc_digital_download_table" class="wp-list-table widefat posts select_product_file">'; $output .= '<thead>'; $output .= '<tr>'; $output .= '<th>' . _x('Title', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th>' . _x('Size', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th>' . _x('File Type', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th id="wpsc_digital_download_action_th">' . _x('Actions', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '</tr>'; $output .= '</thead>'; $output .= '<tfoot>'; $output .= '<tr>'; $output .= '<th>' . _x('Title', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th>' . _x('Size', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th>' . _x('File Type', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '<th id="wpsc_digital_download_action_th">' . _x('Actions', 'Digital download UI', 'wp-e-commerce') . '</th>'; $output .= '</tr>'; $output .= '</tfoot>'; $num = 0; $output .= '<tbody>'; $delete_nonce = _wpsc_create_ajax_nonce('delete_file'); foreach ((array) $attached_files as $file) { $file_dir = WPSC_FILE_DIR . $file->post_title; $file_size = 'http://s3file' == $file->guid ? __('Remote file sizes cannot be calculated', 'wp-e-commerce') : wpsc_convert_byte(filesize($file_dir)); $file_url = add_query_arg(array('wpsc_download_id' => $file->ID, '_wpnonce' => wp_create_nonce('wpsc-admin-download-file-' . $file->ID)), admin_url()); $deletion_url = wp_nonce_url("admin.php?wpsc_admin_action=delete_file&file_name={$file->post_title}&product_id={$product_id}&row_number={$num}", 'delete_file_' . $file->post_title); $class = !wpsc_is_odd($num) ? 'alternate' : ''; $file_type = get_post_mime_type($file->ID); $icon_url = wp_mime_type_icon($file_type); $output .= '<tr class="wpsc_product_download_row ' . $class . '">'; $output .= '<td style="padding-right: 30px;"><img src="' . $icon_url . '"><span>' . $file->post_title . '</span></td>'; $output .= '<td>' . $file_size . '</td>'; $output .= '<td>' . $file_type . '</td>'; $output .= '<td><a href="' . esc_url($file_url) . '">' . _x('Download', 'Digital download row UI', 'wp-e-commerce') . '</a><a data-file-name="' . esc_attr($file->post_title) . '" data-product-id="' . esc_attr($product_id) . '" data-nonce="' . esc_attr($delete_nonce) . '" class="file_delete_button" href="' . $deletion_url . '" >' . _x("Delete", "Digital download row UI", 'wp-e-commerce') . '</a></td>'; $output .= '</tr>'; $num++; } $output .= '</tbody>'; $output .= '</table>'; if (empty($attached_files)) { $output .= "<p class='no-item'>" . __('There are no files attached to this product. Upload a new file or select from other product files.', 'wp-e-commerce') . "</p>"; } $output .= "<div class='" . (is_numeric($product_id) ? 'edit_' : '') . "select_product_handle'></div>"; $output .= "<script type='text/javascript'>\r\n"; $output .= "var select_min_height = " . 25 * 3 . ";\r\n"; $output .= "var select_max_height = " . 25 * ($num + 1) . ";\r\n"; $output .= "</script>"; return $output; }
/** * * Processes the attachment for the given post id * * @param $postId * @author Tim Perry * */ protected function processAttachment($postId) { $mimeType = get_post_mime_type($postId); if ($mimeType != "application/pdf") { return; } $file = get_attached_file($postId); if ($outputPath = $this->createPdfThumb($file, $postId)) { $this->createAndAttachAttachment($outputPath, $postId); } }
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; }
public function image_downsize($out, $id, $size) { // we don't handle this if (is_array($size)) { return false; } $meta = wp_get_attachment_metadata($id); $wanted_width = $wanted_height = 0; if (empty($meta['file'])) { return false; } // get $size dimensions global $_wp_additional_image_sizes; if (isset($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes[$size])) { $wanted_width = $_wp_additional_image_sizes[$size]['width']; $wanted_height = $_wp_additional_image_sizes[$size]['height']; $wanted_crop = isset($_wp_additional_image_sizes[$size]['crop']) ? $_wp_additional_image_sizes[$size]['crop'] : false; } else { if (in_array($size, array('thumbnail', 'medium', 'large'))) { $wanted_width = get_option($size . '_size_w'); $wanted_height = get_option($size . '_size_h'); $wanted_crop = 'thumbnail' === $size ? (bool) get_option('thumbnail_crop') : false; } else { // unknown size, bail out return false; } } if (0 === absint($wanted_width) && 0 === absint($wanted_height)) { return false; } if ($intermediate = image_get_intermediate_size($id, $size)) { return false; } else { // image size not found, create it $attachment_path = get_attached_file($id); $image_editor = wp_get_image_editor($attachment_path); if (!is_wp_error($image_editor)) { $image_editor->resize($wanted_width, $wanted_height, $wanted_crop); $result_image_size = $image_editor->get_size(); $result_width = $result_image_size['width']; $result_height = $result_image_size['height']; $suffix = $result_width . 'x' . $result_height; $filename = $image_editor->generate_filename($suffix); $image_editor->save($filename); $meta['sizes'][$size] = array('file' => basename($filename), 'width' => $result_width, 'height' => $result_height, 'mime-type' => get_post_mime_type($id)); wp_update_attachment_metadata($id, $meta); } else { return false; } } return false; }
/** * Uses the mime type of an attachment to determine the class. It first splits by "/", then replaces the occurrences of * "." and "_" with "-" * * @param string $mime * @return string */ function suffusion_get_mime_type_class($mime = '') { if ($mime == '') { $mime = get_post_mime_type(); } $raw_mime_type = explode('/', $mime); $mime_type = array(); foreach ($raw_mime_type as $mime_type_component) { $mime_type[] = str_replace(array('.', '_'), '-', $mime_type_component); } $mime_type_class = implode(' ', $mime_type); return $mime_type_class; }
/** * Add video url field for attachments. * */ function presscore_attachment_fields_to_edit($fields, $post) { // hopefuly add new field only for images if (strpos(get_post_mime_type($post->ID), 'image') !== false) { $video_url = get_post_meta($post->ID, 'dt-video-url', true); $img_link = get_post_meta($post->ID, 'dt-img-link', true); $hide_title = get_post_meta($post->ID, 'dt-img-hide-title', true); $fields['dt-video-url'] = array('label' => _x('Video url', 'attachment field', 'the7mk2'), 'input' => 'text', 'value' => $video_url ? $video_url : '', 'show_in_edit' => true); $fields['dt-img-link'] = array('label' => _x('Image link', 'attachment field', 'the7mk2'), 'input' => 'text', 'value' => $img_link ? $img_link : '', 'show_in_edit' => true); $fields['dt-img-hide-title'] = array('label' => _x('Hide title', 'attachment field', 'the7mk2'), 'input' => 'html', 'html' => "<input id='attachments-{$post->ID}-dt-img-hide-title' type='checkbox' name='attachments[{$post->ID}][dt-img-hide-title]' value='1' " . checked($hide_title, true, false) . "/>", 'show_in_edit' => true); } return $fields; }
function bl_profile_update_listing_content() { global $wpdb, $post; $bl_id = is_numeric($_GET["bl_id"]) ? $_GET["bl_id"] : "0"; $data = get_option("bepro_listings"); //get information $item = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . BEPRO_LISTINGS_TABLE_NAME . " WHERE id = " . $bl_id); if (!$item) { return; } $post_data = get_post($item->post_id); //get categories $raw_categories = listing_types_by_post($item->post_id); $categories = array(); if ($raw_categories) { foreach ($raw_categories as $category) { $categories[$category->term_id] = $category->term_id; } } $args = array('numberposts' => -1, 'post_parent' => $item->post_id, 'post_type' => 'attachment'); //get images $attachments = get_posts($args); $thunmbnails = array(); $file_icons = array("application/pdf" => "document.png", "text/plain" => "text.png", "text/csv" => "spreadsheet.png", "application/rar" => "archive.png", "application/x-tar" => "archive.png", "application/zip" => "archive.png", "application/x-gzip" => "archive.png", "application/x-7z-compressed" => "archive.png", "application/msword" => "document.png", "application/vnd.oasis.opendocument.text" => "document.png", "application/vnd.oasis.opendocument.presentation" => "text.png", "application/vnd.oasis.opendocument.spreadsheet" => "interactive.png", "application/vnd.oasis.opendocument.graphics" => "interactive.png", "application/vnd.oasis.opendocument.chart" => "spreadsheet.png", "application/wordperfect" => "document.png", "video/x-ms-asf" => "video.png", "video/x-ms-wmv" => "video.png", "video/x-ms-wmx" => "video.png", "video/x-ms-wm" => "video.png", "video/avi" => "video.png", "video/divx" => "video.png", "video/x-flv" => "video.png", "video/quicktime" => "video.png", "video/mpeg" => "video.png", "video/mp4" => "video.png", "video/ogg" => "video.png", "video/webm" => "video.png", "video/x-matroska" => "video.png"); if ($attachments) { foreach ($attachments as $attachment) { $image = wp_get_attachment_image_src($attachment->ID, 'thumbnail', false); if (!$image) { $p_type = get_post_mime_type($attachment->ID); $f_type = empty($file_icons[$p_type]) ? "text.png" : $file_icons[$p_type]; $image[0] = get_bloginfo("wpurl") . "/wp-includes/images/crystal/" . $f_type; } $image[4] = $attachment->ID; $image[5] = basename(get_attached_file($attachment->ID)); $thunmbnails[] = $image; } } //get settings $data = get_option("bepro_listings"); $default_user_id = $data["default_user_id"]; $num_images = $data["num_images"]; $validate = $data["validate_form"]; $show_cost = $data["show_cost"]; $show_con = $data["show_con"]; $show_geo = $data["show_geo"]; $cat_drop = $data["cat_drop"]; $listing_url = "?bl_manage=1&bl_id="; $url = get_permalink($post->ID); echo "<p><a href='" . $url . "'>Cancel</a></p>"; require dirname(__FILE__) . '/templates/form.php'; }
/** * 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 ); }
/** * Loads the correct function for handling attachments. Checks the attachment mime type to call * correct function. Image attachments are not loaded with this function. The functionality for them * should be handled by the theme's attachment or image attachment file. * * Ideally, all attachments would be appropriately handled within their templates. However, this could * lead to messy template files. * * @since 0.5.0 * @uses get_post_mime_type() Gets the mime type of the attachment. * @uses wp_get_attachment_url() Gets the URL of the attachment file. */ function hybrid_attachment() { $file = wp_get_attachment_url(); $mime = get_post_mime_type(); $mime_type = explode('/', $mime); /* Loop through each mime type. If a function exists for it, call it. Allow users to filter the display. */ foreach ($mime_type as $type) { if (function_exists("hybrid_{$type}_attachment")) { $attachment = call_user_func("hybrid_{$type}_attachment", $mime, $file); } $attachment = apply_atomic("{$type}_attachment", $attachment); } echo apply_atomic('attachment', $attachment); }
function add_post_featured_image_as_rss_item_enclosure() { if (!has_post_thumbnail()) { return; } $thumbnail_size = apply_filters('rss_enclosure_image_size', 'thumbnail'); $thumbnail_id = get_post_thumbnail_id(get_the_ID()); $thumbnail = image_get_intermediate_size($thumbnail_id, $thumbnail_size); if (empty($thumbnail)) { return; } $upload_dir = wp_upload_dir(); printf('<enclosure url="%s" length="%s" type="%s" />', $thumbnail['url'], filesize(path_join($upload_dir['basedir'], $thumbnail['path'])), get_post_mime_type($thumbnail_id)); }
protected function __construct($post) { parent::__construct($post); // Set standard content $this->description =& $this->content; $this->caption =& $this->excerpt; // Retrieve post meta $this->alt = get_post_meta($this->id, '_wp_attachment_image_alt', true); $this->mime_type = get_post_mime_type($this->id); $this->link = wp_get_attachment_url($this->id); }