/**
  * Process Imagick image stream request, e.g., for a PDF thumbnail
  *
  * Requires mla_stream_file (relative to wp_upload_dir ) in $_REQUEST;
  * optional $_REQUEST parameters are:
  * 		mla_stream_width, mla_stream_height, mla_stream_frame, mla_stream_resolution,
  *		mla_stream_quality, mla_stream_type, mla_stream_fit, mla_ghostscript_path
  *
  * @since 2.10
  *
  * @return	void	echos image content and calls exit();
  */
 public static function mla_process_stream_image()
 {
     self::_mla_debug_add('MLAImageProcessor::mla_process_stream_image REQUEST = ' . var_export($_REQUEST, true));
     if (!class_exists('Imagick')) {
         self::_mla_die('Imagick not installed', __LINE__, 500);
     }
     if (ini_get('zlib.output_compression')) {
         ini_set('zlib.output_compression', 'Off');
     }
     $file = $_REQUEST['mla_stream_file'];
     if (!is_file($file)) {
         self::_mla_die('File not found', __LINE__, 404);
     }
     $use_mutex = isset($_REQUEST['mla_single_thread']);
     $width = isset($_REQUEST['mla_stream_width']) ? abs(intval($_REQUEST['mla_stream_width'])) : 0;
     $height = isset($_REQUEST['mla_stream_height']) ? abs(intval($_REQUEST['mla_stream_height'])) : 0;
     $type = isset($_REQUEST['mla_stream_type']) ? $_REQUEST['mla_stream_type'] : 'image/jpeg';
     $quality = isset($_REQUEST['mla_stream_quality']) ? abs(intval($_REQUEST['mla_stream_quality'])) : 0;
     $frame = isset($_REQUEST['mla_stream_frame']) ? abs(intval($_REQUEST['mla_stream_frame'])) : 0;
     $resolution = isset($_REQUEST['mla_stream_resolution']) ? abs(intval($_REQUEST['mla_stream_resolution'])) : 72;
     /*
      * If mla_ghostscript_path is present, a non-standard GS location can be found in a file written by
      * the [mla_gallery] shortcode processor.
      */
     $ghostscript_path = isset($_REQUEST['mla_ghostscript_path']) ? $_REQUEST['mla_ghostscript_path'] : '';
     if (!empty($ghostscript_path)) {
         $ghostscript_path = @file_get_contents(dirname(__FILE__) . '/' . 'mla-ghostscript-path.txt');
     }
     if ($use_mutex) {
         $temp_file = self::_get_temp_file();
         @unlink($temp_file);
         $temp_file = pathinfo($temp_file, PATHINFO_DIRNAME) . '/mla-mutex.txt';
         $mutex = new MLAMutex();
         $mutex->init(1, $temp_file);
         $mutex->acquire();
         self::_mla_debug_add('MLAImageProcessor::mla_process_stream_image begin file = ' . var_export($file, true));
     }
     /*
      * Convert the file to an image format and load it
      */
     try {
         self::$image = new Imagick();
         /*
          * this must be called before reading the image, otherwise has no effect - 
          * "-density {$x_resolution}x{$y_resolution}"
          * this is important to give good quality output, otherwise text might be unclear
          * default resolution is 72,72
          */
         self::$image->setResolution($resolution, $resolution);
         //$result = false;
         $result = self::_ghostscript_convert($file, $frame, $resolution, $type, $ghostscript_path);
         if (false === $result) {
             try {
                 self::$image->readImage($file . '[' . $frame . ']');
             } catch (Exception $e) {
                 self::$image->readImage($file . '[0]');
             }
             if ('image/jpeg' == $type) {
                 $extension = 'JPG';
             } else {
                 $extension = 'PNG';
             }
             self::$image->setImageFormat($extension);
         }
         if (!self::$image->valid()) {
             self::_mla_die('File not loaded', __LINE__, 404);
         }
     } catch (Exception $e) {
         self::_mla_die('Image load exception: ' . $e->getMessage(), __LINE__, 404);
     }
     /*
      * Prepare the output image; resize and flatten, if necessary
      */
     try {
         if (isset($_REQUEST['mla_stream_fit'])) {
             $best_fit = '1' == $_REQUEST['mla_stream_fit'];
         } else {
             $best_fit = false;
         }
         self::_prepare_image($width, $height, $best_fit, $type, $quality);
     } catch (Exception $e) {
         self::_mla_die('_prepare_image exception: ' . $e->getMessage(), __LINE__, 500);
     }
     /*
      * Stream the image back to the requestor
      */
     try {
         header("Content-Type: {$type}");
         echo self::$image->getImageBlob();
     } catch (Exception $e) {
         self::_mla_die('Image stream exception: ' . $e->getMessage(), __LINE__, 500);
     }
     if ($use_mutex) {
         $mutex->release();
     }
     exit;
 }
 /**
  * Process an MLA_List_Table custom bulk action
  *
  * Creates new items from the "Bulk Translate" list.
  *
  * @since 2.13
  *
  * @param	array	$item_content	NULL, to indicate no handler.
  * @param	string	$bulk_action	the requested action.
  * @param	integer	$post_id		the affected attachment.
  *
  * @return	object	updated $item_content. NULL if no handler, otherwise
  *					( 'message' => error or status message(s), 'body' => '' )
  */
 public static function mla_list_table_custom_bulk_action($item_content, $bulk_action, $post_id)
 {
     if (self::MLA_GFI_ACTION != $bulk_action) {
         return $item_content;
     }
     /* translators: 1: post ID */
     $item_prefix = sprintf(__('Item %1$d', 'media-library-assistant'), $post_id) . ', ';
     /*
      * If there is a real thumbnail image, no generation is required or allowed
      */
     $thumbnail = wp_get_attachment_image($post_id);
     if (!empty($thumbnail)) {
         return array('message' => $item_prefix . __('has native thumbnail.', 'media-library-assistant'));
     }
     /*
      * Look for the "Featured Image" as an alternate thumbnail for PDFs, etc.
      */
     $thumbnail = get_post_thumbnail_id($post_id);
     if (!empty($thumbnail)) {
         switch (self::$bulk_action_options['existing_thumbnails']) {
             case 'ignore':
                 break;
             case 'trash':
                 delete_post_thumbnail($post_id);
                 wp_delete_post(absint($thumbnail), false);
                 break;
             case 'delete':
                 delete_post_thumbnail($post_id);
                 wp_delete_post(absint($thumbnail), true);
                 break;
             case 'keep':
             default:
                 return array('message' => $item_prefix . __('Featured Image retained.', 'media-library-assistant'));
         }
     }
     /*
      * Validate the file existance and type
      */
     $file = get_attached_file($post_id);
     if (empty($file)) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$sno attached file.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix));
     }
     if (!in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), array('ai', 'eps', 'pdf', 'ps'))) {
         return array('message' => $item_prefix . __('unsupported file type.', 'media-library-assistant'));
     }
     /*
      * Generate a thumbnail
      */
     require_once MLA_PLUGIN_PATH . 'includes/class-mla-image-processor.php';
     $results = MLAImageProcessor::mla_handle_thumbnail_sideload($file, self::$bulk_action_options);
     if (!empty($results['error'])) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$sthumbnail generation failed', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix) . ' - ' . $results['error']);
     }
     /*
      * Adjust the file name for the new item
      */
     $pathinfo = pathinfo($results['name']);
     if (isset(self::$bulk_action_options['suffix'])) {
         $pathinfo['filename'] = sanitize_file_name($pathinfo['filename'] . strtolower(self::$bulk_action_options['suffix']));
     }
     $pathinfo['extension'] = 'image/jpeg' == $results['type'] ? 'jpg' : 'png';
     $results['name'] = $pathinfo['filename'] . '.' . $pathinfo['extension'];
     $overrides = array('test_form' => false, 'test_size' => true, 'test_upload' => true);
     // move the temporary file into the uploads directory
     $results = wp_handle_sideload($results, $overrides);
     $item_data = get_post($post_id, ARRAY_A);
     unset($item_data['ID']);
     unset($item_data['post_author']);
     unset($item_data['post_date']);
     unset($item_data['post_date_gmt']);
     if (isset(self::$bulk_action_options['suffix'])) {
         $item_data['post_title'] .= self::$bulk_action_options['suffix'];
     }
     unset($item_data['post_name']);
     unset($item_data['post_modified']);
     unset($item_data['post_modified_gmt']);
     $item_parent = $item_data['post_parent'];
     unset($item_data['post_parent']);
     $item_data['guid'] = $results['url'];
     $item_data['post_mime_type'] = $results['type'];
     unset($item_data['comment_count']);
     unset($item_data['ancestors']);
     unset($item_data['post_category']);
     unset($item_data['tags_input']);
     // Insert the attachment.
     $item_id = wp_insert_attachment($item_data, $results['file'], $item_parent);
     if (empty($item_id)) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$swp_insert_attachment failed.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix));
     }
     // 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.
     $item_data = wp_generate_attachment_metadata($item_id, $results['file']);
     wp_update_attachment_metadata($item_id, $item_data);
     // Assign the new item as the source item's Featured Image
     delete_post_thumbnail($post_id);
     set_post_thumbnail($post_id, $item_id);
     MLA_Thumbnail::$bulk_action_includes[] = $item_id;
     /* translators: 1: Item post ID, 2: new thumbnail item ID */
     $item_content = array('message' => sprintf(__('%1$sthumbnail generated as new item %2$s.', 'media-library-assistant'), $item_prefix, $item_id));
     return $item_content;
 }
<?php

/**
 * Stand-alone stream image handler for the mla_viewer
 *
 * @package Media Library Assistant
 * @since 2.10
 */
/*
 * Process mla_viewer image stream requests
 */
//@ini_set('error_log','C:\Program Files (x86)\Apache Software Foundation\Apache2.2\logs\php-errors.log');
require_once pathinfo(__FILE__, PATHINFO_DIRNAME) . '/class-mla-image-processor.php';
if (isset($_REQUEST['mla_stream_file'])) {
    MLAImageProcessor::$mla_debug = isset($_REQUEST['mla_debug']) && 'log' == $_REQUEST['mla_debug'];
    MLAImageProcessor::mla_process_stream_image();
}
MLAImageProcessor::_mla_die('mla_stream_file not set', __LINE__, 500);