Example #1
0
 /**
  * Add images to database
  *
  * @class nggAdmin
  * @param int $galleryID
  * @param array $imageslist
  * @return array $image_ids Id's which are sucessful added
  */
 static function add_Images($galleryID, $imageslist)
 {
     global $wpdb, $ngg;
     $image_ids = array();
     if (is_array($imageslist)) {
         foreach ($imageslist as $picture) {
             // filter function to rename/change/modify image before
             $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID);
             // strip off the extension of the filename
             $path_parts = M_I18n::mb_pathinfo($picture);
             $alttext = !isset($path_parts['filename']) ? substr($path_parts['basename'], 0, strpos($path_parts['basename'], '.')) : $path_parts['filename'];
             // save it to the database
             $pic_id = nggdb::add_image($galleryID, $picture, '', $alttext);
             if (C_NextGen_Settings::get_instance()->imgBackup && !empty($pic_id)) {
                 $storage = C_Gallery_Storage::get_instance();
                 $storage->backup_image($pic_id);
             }
             if (!empty($pic_id)) {
                 $image_ids[] = $pic_id;
             }
             // add the metadata
             nggAdmin::import_MetaData($pic_id);
             // auto rotate
             nggAdmin::rotate_image($pic_id);
             // Autoresize image if required
             if ($ngg->options['imgAutoResize']) {
                 $imagetmp = nggdb::find_image($pic_id);
                 $sizetmp = @getimagesize($imagetmp->imagePath);
                 $widthtmp = $ngg->options['imgWidth'];
                 $heighttmp = $ngg->options['imgHeight'];
                 if ($sizetmp[0] > $widthtmp && $widthtmp || $sizetmp[1] > $heighttmp && $heighttmp) {
                     nggAdmin::resize_image($pic_id);
                 }
             }
             // action hook for post process after the image is added to the database
             $image = array('id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID);
             do_action('ngg_added_new_image', $image);
         }
     }
     // is_array
     // delete dirsize after adding new images
     delete_transient('dirsize_cache');
     do_action('ngg_after_new_images_added', $galleryID, $image_ids);
     return $image_ids;
 }
 public function get_params_from_name($name, $is_only_size_name = false)
 {
     $prefix_list = $this->object->_get_name_prefix_list();
     $id_prefix = $prefix_list['id'];
     $size_prefix = $prefix_list['size'];
     $flags_prefix = $prefix_list['flags'];
     $max_value_length = $prefix_list['max_value_length'];
     $size_name = null;
     $id_name = null;
     $params = array();
     if (!$is_only_size_name) {
         $extension = M_I18n::mb_pathinfo($name, PATHINFO_EXTENSION);
         if ($extension != null) {
             $extension = '.' . $extension;
         }
         $name = M_I18n::mb_basename($name, $extension);
     }
     $size_index = strrpos($name, $size_prefix);
     if ($size_index > 0 || $size_index === 0) {
         // check if name contains dynamic size/params info by looking for prefix
         $size_name = substr($name, $size_index);
     }
     if (!$is_only_size_name) {
         // name should contain the image id, search for prefix
         $id_index = strrpos($name, $id_prefix);
         if ($id_index > 0 || $id_index === 0) {
             if ($size_index > 0 && $size_index > $id_index) {
                 $id_name = substr($name, $id_index, $size_index - $id_index);
             } else {
                 $id_name = substr($name, $id_index);
             }
         }
     }
     // Double check we got a correct dynamic size/params string
     if (substr($size_name, 0, strlen($size_prefix)) == $size_prefix) {
         $flags = $prefix_list['flag'];
         // get the length of the flag id (the key in the $flags array) in the string (how many characters to consume)
         $flag_id_len = $prefix_list['flag_len'];
         $params_str = substr($size_name, strlen($size_prefix));
         $params_parts = explode('-', $params_str);
         // $param_part is a single param, separated by '-'
         foreach ($params_parts as $param_part) {
             // Parse WxHxQ - Q=quality
             $param_size = explode('x', $param_part);
             $param_size_count = count($param_size);
             if (substr($param_part, 0, strlen($flags_prefix)) == $flags_prefix) {
                 /* Set flags, using $flags keys as prefixes */
                 // move string pointer up (after the main flags prefix)
                 $param_flags = substr($param_part, strlen($flags_prefix));
                 $param_flags_len = strlen($param_flags);
                 $flags_todo = $flags;
                 while (true) {
                     // ensure we don't run into an infinite loop ;)
                     if (count($flags_todo) == 0 || strlen($param_flags) == 0) {
                         break;
                     }
                     // get the flag prefix (a key in the $flags array) using flag id length
                     $flag_prefix = substr($param_flags, 0, $flag_id_len);
                     // move string pointer up (after the single flag prefix)
                     $param_flags = substr($param_flags, $flag_id_len);
                     // get the length of the flag value in the string (how many characters to consume)
                     // flag value length is stored in a single hexadecimal character next to the flag prefix
                     $flag_value_len = min(hexdec(substr($param_flags, 0, 1)), min($max_value_length, strlen($param_flags) - 1));
                     // get the flag value
                     $flag_value = substr($param_flags, 1, $flag_value_len);
                     // move string pointer up (after the entire flag)
                     $param_flags = substr($param_flags, $flag_value_len + 1);
                     // make sure the flag is supported
                     if (isset($flags[$flag_prefix])) {
                         $flag_name = $flags[$flag_prefix];
                         if (is_numeric($flag_value)) {
                             // convert numerical flags to integers
                             $flag_value = intval($flag_value);
                         }
                         $params[$flag_name] = $flag_value;
                         if (isset($flags_todo[$flag_prefix])) {
                             unset($flags_todo[$flag_prefix]);
                         }
                     } else {
                     }
                 }
             } else {
                 if ($param_size_count == 2 || $param_size_count == 3) {
                     // Set W H Q
                     $params['width'] = intval($param_size[0]);
                     $params['height'] = intval($param_size[1]);
                     if (isset($param_size[2]) && intval($param_size[2]) > 0) {
                         $params['quality'] = intval($param_size[2]);
                     }
                 }
             }
         }
     }
     // Double check we got a correct id string
     if (substr($id_name, 0, strlen($id_prefix)) == $id_prefix) {
         // move string pointer up (after the prefix)
         $id_name = substr($id_name, strlen($id_prefix));
         // get the length of the image id in the string (how many characters to consume)
         $id_len = min(hexdec(substr($id_name, 0, 1)), min($max_value_length, strlen($id_name) - 1));
         // get the id based on old position and id length
         $image_id = intval(substr($id_name, 1, $id_len));
         if ($image_id > 0) {
             $params['image'] = $image_id;
         }
     }
     return $this->object->_get_params_sanitized($params);
 }
 /**
  * Gets the url of a particular-sized image
  * @param int|object $image
  * @param string $size
  * @returns array
  */
 public function get_image_url($image, $size = 'full', $check_existance = FALSE, $image_abspath = FALSE)
 {
     $retval = NULL;
     $fs = C_Fs::get_instance();
     $router = C_Router::get_instance();
     if (!$image_abspath) {
         $image_abspath = $this->object->get_image_abspath($image, $size, $check_existance);
     }
     if ($image_abspath) {
         // encode the filename: because filesystems will let you name things like%@this.jpg
         $parts = M_I18n::mb_pathinfo($image_abspath);
         $parts['basename'] = urlencode($parts['basename']);
         $image_abspath = $parts['dirname'] . DIRECTORY_SEPARATOR . $parts['basename'];
         $doc_root = $fs->get_document_root('gallery');
         if ($doc_root != null) {
             $doc_root = rtrim($doc_root, '/\\') . DIRECTORY_SEPARATOR;
         }
         // if docroot is "/" we would generate urls like /wp-contentpluginsnextgen-galleryetcetc
         if ($doc_root !== '/') {
             $request_uri = str_replace($doc_root, '', $image_abspath);
         } else {
             $request_uri = $image_abspath;
         }
         $request_uri = '/' . ltrim(str_replace('\\', '/', $request_uri), '/');
         $retval = $router->remove_url_segment('/index.php', $router->get_url($request_uri, FALSE, 'gallery'));
     }
     return apply_filters('ngg_get_image_url', $retval, $image, $size);
 }
 public function set_post_thumbnail($post, $image)
 {
     $attachment_id = null;
     // Get the post id
     $post_id = $post;
     if (is_object($post)) {
         if (property_exists($post, 'ID')) {
             $post_id = $post->ID;
         } elseif (property_exists($post, 'post_id')) {
             $post_id = $post->post_id;
         }
     } elseif (is_array($post)) {
         if (isset($post['ID'])) {
             $post_id = $post['ID'];
         } elseif (isset($post['post_id'])) {
             $post_id = $post['post_id'];
         }
     }
     // Get the image object
     if (is_int($image)) {
         $image = C_Image_Mapper::get_instance()->find($image);
     }
     // Do we have what we need?
     if ($image && is_int($post_id)) {
         $args = array('post_type' => 'attachment', 'meta_key' => '_ngg_image_id', 'meta_compare' => '==', 'meta_value' => $image->{$image->id_field});
         $upload_dir = wp_upload_dir();
         $basedir = $upload_dir['basedir'];
         $thumbs_dir = implode(DIRECTORY_SEPARATOR, array($basedir, 'ngg_featured'));
         $gallery_abspath = $this->object->get_gallery_abspath($image->galleryid);
         $image_abspath = $this->object->get_full_abspath($image);
         $target_path = null;
         $copy_image = TRUE;
         // Have we previously set the post thumbnail?
         if ($posts = get_posts($args)) {
             $attachment_id = $posts[0]->ID;
             $attachment_file = get_attached_file($attachment_id);
             $target_path = $attachment_file;
             if (filemtime($image_abspath) > filemtime($target_path)) {
                 $copy_image = TRUE;
             }
         } else {
             $url = $this->object->get_full_url($image);
             $target_relpath = null;
             $target_basename = M_I18n::mb_basename($image_abspath);
             if (strpos($image_abspath, $gallery_abspath) === 0) {
                 $target_relpath = substr($image_abspath, strlen($gallery_abspath));
             } else {
                 if ($image->galleryid) {
                     $target_relpath = path_join(strval($image->galleryid), $target_basename);
                 } else {
                     $target_relpath = $target_basename;
                 }
             }
             $target_relpath = trim($target_relpath, '\\/');
             $target_path = path_join($thumbs_dir, $target_relpath);
             $max_count = 100;
             $count = 0;
             while (file_exists($target_path) && $count <= $max_count) {
                 $count++;
                 $pathinfo = M_I18n::mb_pathinfo($target_path);
                 $dirname = $pathinfo['dirname'];
                 $filename = $pathinfo['filename'];
                 $extension = $pathinfo['extension'];
                 $rand = mt_rand(1, 9999);
                 $basename = $filename . '_' . sprintf('%04d', $rand) . '.' . $extension;
                 $target_path = path_join($dirname, $basename);
             }
             if (file_exists($target_path)) {
             }
             $target_dir = dirname($target_path);
             wp_mkdir_p($target_dir);
         }
         if ($copy_image) {
             @copy($image_abspath, $target_path);
             if (!$attachment_id) {
                 $size = @getimagesize($target_path);
                 $image_type = $size ? $size['mime'] : 'image/jpeg';
                 $title = sanitize_file_name($image->alttext);
                 $caption = sanitize_file_name($image->description);
                 $attachment = array('post_title' => $title, 'post_content' => $caption, 'post_status' => 'attachment', 'post_parent' => 0, 'post_mime_type' => $image_type, 'guid' => $url);
                 $attachment_id = wp_insert_attachment($attachment, $target_path);
             }
             update_post_meta($attachment_id, '_ngg_image_id', $image->{$image->id_field});
             wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $target_path));
             set_post_thumbnail($post_id, $attachment_id);
         }
     }
     return $attachment_id;
 }
Example #5
0
 /**
  * nggPostThumbnail::ajax_set_post_thumbnail()
  * 
  * @return void
  */
 function ajax_set_post_thumbnail()
 {
     global $post_ID;
     // check for correct capability
     if (!is_user_logged_in()) {
         die('-1');
     }
     // get the post id as global variable, otherwise the ajax_nonce failed later
     $post_ID = intval($_POST['post_id']);
     if (!current_user_can('edit_post', $post_ID)) {
         die('-1');
     }
     $thumbnail_id = intval($_POST['thumbnail_id']);
     // delete the image
     if ($thumbnail_id == '-1') {
         delete_post_meta($post_ID, '_thumbnail_id');
         die('0');
     }
     if ($thumbnail_id != null) {
         $imap = C_Image_Mapper::get_instance();
         $storage = C_Gallery_Storage::get_instance();
         $image = $imap->find($thumbnail_id);
         // for NGG we look for the image id
         if ($image) {
             $image_id = $thumbnail_id;
             $args = array('post_type' => 'attachment', 'meta_key' => '_ngg_image_id', 'meta_compare' => '==', 'meta_value' => $image_id);
             $upload_dir = wp_upload_dir();
             $basedir = $upload_dir['basedir'];
             $thumbs_dir = implode(DIRECTORY_SEPARATOR, array($basedir, 'ngg_featured'));
             $gallery_abspath = $storage->get_gallery_abspath($image->galleryid);
             $image_abspath = $storage->get_full_abspath($image);
             $target_path = null;
             $posts = get_posts($args);
             $attachment_id = null;
             if ($posts != null) {
                 $attachment_id = $posts[0]->ID;
             } else {
                 $url = $storage->get_full_url($image);
                 $target_relpath = null;
                 $target_basename = M_I18n::mb_basename($image_abspath);
                 if (strpos($image_abspath, $gallery_abspath) === 0) {
                     $target_relpath = substr($image_abspath, strlen($gallery_abspath));
                 } else {
                     if ($image->galleryid) {
                         $target_relpath = path_join(strval($image->galleryid), $target_basename);
                     } else {
                         $target_relpath = $target_basename;
                     }
                 }
                 $target_relpath = trim($target_relpath, '\\/');
                 $target_path = path_join($thumbs_dir, $target_relpath);
                 $max_count = 100;
                 $count = 0;
                 while (file_exists($target_path) && $count <= $max_count) {
                     $count++;
                     $pathinfo = M_I18n::mb_pathinfo($target_path);
                     $dirname = $pathinfo['dirname'];
                     $filename = $pathinfo['filename'];
                     $extension = $pathinfo['extension'];
                     $rand = mt_rand(1, 9999);
                     $basename = $filename . '_' . sprintf('%04d', $rand) . '.' . $extension;
                     $target_path = path_join($dirname, $basename);
                 }
                 if (file_exists($target_path)) {
                     // XXX handle very rare case in which $max_count wasn't enough?
                 }
                 $target_dir = dirname($target_path);
                 wp_mkdir_p($target_dir);
                 if (@copy($image_abspath, $target_path)) {
                     $size = @getimagesize($target_path);
                     $image_type = $size ? $size['mime'] : 'image/jpeg';
                     $title = sanitize_file_name($image->alttext);
                     $caption = sanitize_file_name($image->description);
                     $attachment = array('post_title' => $title, 'post_content' => $caption, 'post_status' => 'attachment', 'post_parent' => 0, 'post_mime_type' => $image_type, 'guid' => $url);
                     // Save the data
                     $attachment_id = wp_insert_attachment($attachment, $target_path);
                     if ($attachment_id) {
                         wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $target_path));
                         update_post_meta($attachment_id, '_ngg_image_id', $image_id);
                     }
                 }
             }
             if ($attachment_id) {
                 //$attachment = get_post($attachment_id);
                 //$attachment_meta = wp_get_attachment_metadata($attachment_id);
                 $attachment_file = get_attached_file($attachment_id);
                 $target_path = $attachment_file;
                 if (filemtime($image_abspath) > filemtime($target_path)) {
                     if (@copy($image_abspath, $target_path)) {
                         wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $target_path));
                     }
                 }
                 die(strval($attachment_id));
             }
         }
     }
     die('0');
 }