function test_is_image_negative()
 {
     // these are actually image files but aren't recognized or usable by php
     $files = array('test-image.pct', 'test-image.tga', 'test-image.sgi');
     foreach ($files as $file) {
         $this->assertFalse(file_is_valid_image(DIR_TESTDATA . '/images/' . $file), "file_is_valid_image({$file}) should return false");
     }
 }
Esempio n. 2
0
 /**
  * Callback used by loadImages()
  *
  * @param SplFileInfo $file File info object passed by recurseCallback() in loadImages()
  * @internal
  */
 public static final function _doLoadImages($file)
 {
     // Check if the filename is a valid image
     if (!isset($file) || !file_is_valid_image($file)) {
         return;
     }
     // Try and split it into the size and name parts
     if (preg_match('#.+\\/(\\d+)\\/(.+)\\..+#', $file, $matches) !== 1) {
         return;
     }
     list(, $size, $name) = $matches;
     $result = array($size => plugin_basename($file));
     self::$images[$name] = isset(self::$images[$name]) ? self::$images[$name] + $result : $result;
 }
Esempio n. 3
0
 /**
  * Imports an image into the specified Gallery
  *
  * @since 1.0.11
  * @param string $file Path or URL to the file
  * @param int $id ID of the Gallery (image set)
  * @param string $title Optional title of the image
  * @param string $desc Optional description of the image
  * @param string $alttext Optional alternative text for the image
  * @param array $extra_post_data Optional array containing additional post data
  * @return int|bool Returns the ID of the imported image, or `false` on error
  */
 public static function importImage($file, $id, $title = '', $desc = '', $alttext = '', $extra_post_data = array())
 {
     $result = false;
     $temp_file = false;
     if ($id && $file) {
         // Determine if we need to download the file first
         if (preg_match('#http[s]?:\\/\\/#i', $file)) {
             // It's a URL, dowload it
             $temp_file = download_url($file);
             if (!file_is_valid_image($temp_file)) {
                 // Downloaded file is not a valid image, invalidate $temp_file
                 @unlink($temp_file);
                 $temp_file = false;
             }
         } else {
             // It's not a URL, make a copy of the orignal file if possible (as side-loading deletes it otherwise)
             if (@is_readable($file) && file_is_valid_image($file)) {
                 $temp_file = trailingslashit(sys_get_temp_dir()) . 'bgm' . mt_rand(10000, 99999) . basename($file);
                 if (!copy($file, $temp_file)) {
                     // Invalidate $temp_file if we could not create a temporary copy of the image file
                     @unlink($temp_file);
                     $temp_file = false;
                 }
             }
         }
         if ($temp_file) {
             $post_data = array();
             if ($title) {
                 $post_data['post_title'] = $title;
             }
             if ($desc) {
                 $post_data['post_content'] = $desc;
             }
             // Allow extra post data to overwrite/add to existing post data
             $post_data = array_merge($post_data, $extra_post_data);
             // Sideload the image
             $id = media_handle_sideload(array('name' => basename($file), 'tmp_name' => $temp_file), $id, null, $post_data);
             if (!is_wp_error($id)) {
                 $result = $id;
                 // Add the alt text
                 if ($alttext) {
                     update_post_meta($id, '_wp_attachment_image_alt', $alttext);
                 }
             }
         }
         @unlink($temp_file);
         // Ensure the temp file cleaned up
     }
     return $result;
 }
Esempio n. 4
0
 /**
  * Import a directoy into the specified gallery
  *
  * @param string $directory The directory to import
  * @param int $gallery_id the Gallery ID (Image Set)
  * @param mixed $main The reference to the Main class object
  */
 protected static function importDir($directory, $gallery_id, $main)
 {
     $iterator = new \Pf4wp\Storage\IgnorantRecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS);
     foreach ($iterator as $fileinfo) {
         if ($fileinfo->isFile() && file_is_valid_image($fileinfo)) {
             if (!Images::importImage($fileinfo, $gallery_id)) {
                 $main->addDelayedNotice(sprintf(__('Unable to import <em>%s</em> into Image Set', $main->getName()), $fileinfo), true);
             }
         }
     }
 }
 /**
  * First function to adding a post thumbnail
  * 
  * @todo create_localmedia_fromlocalimages() needs to be used when image is already local
  * @param mixed $overwrite_existing, if post already has a thumbnail do we want to overwrite it or leave it
  */
 public function create_post_thumbnail($post_id, $image_url, $overwrite_existing = false)
 {
     global $wpdb;
     if (!file_is_valid_image($image_url)) {
         return false;
     }
     // if post has existing thumbnail
     if ($overwrite_existing == false) {
         if (get_post_meta($post_id, '_thumbnail_id', true) || get_post_meta($post_id, 'skip_post_thumb', true)) {
             return false;
         }
     }
     // call action function to create the thumbnail in wordpress gallery
     $thumbid = self::create_localmedia_fromhttp($image_url, $post_id);
     // or from create_localmedia_fromlocalimages()
     // update post meta with new thumbnail
     if (is_numeric($thumbid)) {
         update_post_meta($post_id, '_thumbnail_id', $thumbid);
     } else {
         return false;
     }
 }
 public function download_image_attach_to_gallery($gallery_id, $image_url, $title = '', $caption = '', $description = '', $alt_text = '')
 {
     global $maxgalleria;
     $result = 0;
     $download_success = true;
     // Download the image into a temp file
     $temp_file = download_url($image_url);
     // Parse the url and use the temp file to form the file array (used in media_handle_sideload below)
     preg_match('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $image_url, $matches);
     $file_array['name'] = basename($matches[0]);
     $file_array['tmp_name'] = $temp_file;
     // If we got an error or the file is not a valid image, delete the temp file
     if (is_wp_error($temp_file) || !file_is_valid_image($temp_file)) {
         @unlink($temp_file);
         $download_success = false;
     }
     if ($download_success) {
         // Get the next menu order value for the gallery
         $menu_order = $maxgalleria->common->get_next_menu_order($gallery_id);
         // Set post data; the empty post_date ensures it gets today's date
         $post_data = array('post_date' => '', 'post_parent' => $gallery_id, 'post_title' => $title, 'post_excerpt' => $caption, 'post_content' => $description, 'menu_order' => $menu_order, 'ancestors' => array());
         // Sideload the image to create its attachment to the gallery
         $attachment_id = media_handle_sideload($file_array, $gallery_id, $description, $post_data);
         if (!is_wp_error($attachment_id)) {
             $result = $attachment_id;
             // Add the alt text
             update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt_text);
         }
         // Delete the temp file
         @unlink($temp_file);
     }
     return $result;
 }
Esempio n. 7
0
 /** Images Iframe */
 public static function showIframeImages(Main $owner)
 {
     if (!defined('IFRAME_REQUEST')) {
         define('IFRAME_REQUEST', true);
     }
     if (!isset($owner->gallery->ID)) {
         die;
     }
     // Something didn't go quite right
     // Only if Javascript is disabled will we get here, which adds a image to the gallery directly
     if (!empty($_POST) && isset($_POST['_nonce'])) {
         if (!wp_verify_nonce($_POST['_nonce'], 'image-upload')) {
             wp_die(__('You do not have permission to do that [nonce].', $owner->getName()));
         }
         // Check if there's a valid image, and if so, let the Media Library handle the upload
         if (!empty($_FILES) && $_FILES['upload_file']['error'] == 0 && file_is_valid_image($_FILES['upload_file']['tmp_name'])) {
             media_handle_upload('upload_file', $owner->gallery->ID);
         }
     }
     iframe_header();
     $items_per_page = isset($_GET['pp']) ? $_GET['pp'] : 30;
     $page_num = isset($_GET['paged']) ? $_GET['paged'] : 1;
     // Grab the total amount of items (images) and figure out how many pages that is
     $total_items = $owner->images->getCount($owner->gallery->ID);
     if (($total_pages = ceil($total_items / $items_per_page)) < 1) {
         $total_pages = 1;
     }
     // Get a valid page number
     if ($page_num > $total_pages) {
         $page_num = $total_pages;
     } else {
         if ($page_num < 1) {
             $page_num = 1;
         }
     }
     // Grab the images
     $images = $owner->images->get($owner->gallery->ID, array('orderby' => 'menu_order', 'order' => 'asc', 'numberposts' => $items_per_page, 'offset' => ($page_num - 1) * $items_per_page));
     // The page links (for non-JS browsers)
     $page_links = paginate_links(array('base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => __('&laquo;'), 'next_text' => __('&raquo;'), 'total' => $total_pages, 'current' => $page_num));
     $vars = array('images' => $images, 'current_page' => $page_num, 'page_links' => $page_links, 'nonce' => wp_nonce_field('image-upload', '_nonce', false, false));
     $owner->template->display('gallery_image.html.twig', $vars);
     iframe_footer();
     die;
 }