/**
  * Loads image from $this->file into new Imagick Object.
  *
  * @since 3.5.0
  * @access protected
  *
  * @return true|WP_Error True if loaded; WP_Error on failure.
  */
 public function load()
 {
     if ($this->image instanceof Imagick) {
         return true;
     }
     if (!is_file($this->file) && !preg_match('|^https?://|', $this->file)) {
         return new WP_Error('error_loading_image', __('File doesn’t exist?'), $this->file);
     }
     /*
      * Even though Imagick uses less PHP memory than GD, set higher limit
      * for users that have low PHP.ini limits.
      */
     wp_raise_memory_limit('image');
     try {
         $this->image = new Imagick();
         $file_parts = pathinfo($this->file);
         $filename = $this->file;
         // By default, PDFs are rendered in a very low resolution.
         // We want the thumbnail to be readable, so increase the rendering dpi.
         if ('pdf' == strtolower($file_parts['extension'])) {
             $this->image->setResolution(128, 128);
             // Only load the first page.
             $filename .= '[0]';
         }
         // Reading image after Imagick instantiation because `setResolution`
         // only applies correctly before the image is read.
         $this->image->readImage($filename);
         if (!$this->image->valid()) {
             return new WP_Error('invalid_image', __('File is not an image.'), $this->file);
         }
         // Select the first frame to handle animated images properly
         if (is_callable(array($this->image, 'setIteratorIndex'))) {
             $this->image->setIteratorIndex(0);
         }
         $this->mime_type = $this->get_mime_type($this->image->getImageFormat());
     } catch (Exception $e) {
         return new WP_Error('invalid_image', $e->getMessage(), $this->file);
     }
     $updated_size = $this->update_size();
     if (is_wp_error($updated_size)) {
         return $updated_size;
     }
     return $this->set_quality();
 }
Example #2
0
/**
 * Load an image from a string, if PHP supports it.
 *
 * @since 2.1.0
 * @deprecated 3.5.0 Use wp_get_image_editor()
 * @see wp_get_image_editor()
 *
 * @param string $file Filename of the image to load.
 * @return resource The resulting image resource on success, Error string on failure.
 */
function wp_load_image($file)
{
    _deprecated_function(__FUNCTION__, '3.5.0', 'wp_get_image_editor()');
    if (is_numeric($file)) {
        $file = get_attached_file($file);
    }
    if (!is_file($file)) {
        /* translators: %s: file name */
        return sprintf(__('File “%s” doesn’t exist?'), $file);
    }
    if (!function_exists('imagecreatefromstring')) {
        return __('The GD image library is not installed.');
    }
    // Set artificially high because GD uses uncompressed images in memory.
    wp_raise_memory_limit('image');
    $image = imagecreatefromstring(file_get_contents($file));
    if (!is_resource($image)) {
        /* translators: %s: file name */
        return sprintf(__('File “%s” is not an image.'), $file);
    }
    return $image;
}
Example #3
0
    $typenow = '';
}
if (isset($_REQUEST['taxonomy']) && taxonomy_exists($_REQUEST['taxonomy'])) {
    $taxnow = $_REQUEST['taxonomy'];
} else {
    $taxnow = '';
}
if (WP_NETWORK_ADMIN) {
    require ABSPATH . 'wp-admin/network/menu.php';
} elseif (WP_USER_ADMIN) {
    require ABSPATH . 'wp-admin/user/menu.php';
} else {
    require ABSPATH . 'wp-admin/menu.php';
}
if (current_user_can('manage_options')) {
    wp_raise_memory_limit('admin');
}
/**
 * Fires as an admin screen or script is being initialized.
 *
 * Note, this does not just run on user-facing admin screens.
 * It runs on admin-ajax.php and admin-post.php as well.
 *
 * This is roughly analogous to the more general {@see 'init'} hook, which fires earlier.
 *
 * @since 2.5.0
 */
do_action('admin_init');
if (isset($plugin_page)) {
    if (!empty($typenow)) {
        $the_parent = $pagenow . '?post_type=' . $typenow;
 /**
  * Loads image from $this->file into new Imagick Object.
  *
  * @since 3.5.0
  * @access protected
  *
  * @return true|WP_Error True if loaded; WP_Error on failure.
  */
 public function load()
 {
     if ($this->image instanceof Imagick) {
         return true;
     }
     if (!is_file($this->file) && !preg_match('|^https?://|', $this->file)) {
         return new WP_Error('error_loading_image', __('File doesn’t exist?'), $this->file);
     }
     /*
      * Even though Imagick uses less PHP memory than GD, set higher limit
      * for users that have low PHP.ini limits.
      */
     wp_raise_memory_limit('image');
     try {
         $this->image = new Imagick($this->file);
         if (!$this->image->valid()) {
             return new WP_Error('invalid_image', __('File is not an image.'), $this->file);
         }
         // Select the first frame to handle animated images properly
         if (is_callable(array($this->image, 'setIteratorIndex'))) {
             $this->image->setIteratorIndex(0);
         }
         $this->mime_type = $this->get_mime_type($this->image->getImageFormat());
     } catch (Exception $e) {
         return new WP_Error('invalid_image', $e->getMessage(), $this->file);
     }
     $updated_size = $this->update_size();
     if (is_wp_error($updated_size)) {
         return $updated_size;
     }
     return $this->set_quality();
 }
 /**
  * Tests raising the memory limit.
  *
  * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
  * test suite is -1, we can not test the memory limit negotiations.
  *
  * @ticket 32075
  */
 function test_wp_raise_memory_limit()
 {
     if (-1 !== WP_MAX_MEMORY_LIMIT) {
         $this->markTestSkipped('WP_MAX_MEMORY_LIMIT should be set to -1');
     }
     $ini_limit_before = ini_get('memory_limit');
     $raised_limit = wp_raise_memory_limit();
     $ini_limit_after = ini_get('memory_limit');
     $this->assertSame($ini_limit_before, $ini_limit_after);
     $this->assertSame(false, $raised_limit);
     $this->assertEquals(WP_MAX_MEMORY_LIMIT, $ini_limit_after);
 }
Example #6
0
/**
 * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
 * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
 *
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
 * However, The most memory required shouldn't be much larger than the Archive itself.
 *
 * @since 2.5.0
 *
 * @global WP_Filesystem_Base $wp_filesystem Subclass
 *
 * @param string $file Full path and filename of zip archive
 * @param string $to Full path on the filesystem to extract archive to
 * @return mixed WP_Error on failure, True on success
 */
function unzip_file($file, $to)
{
    global $wp_filesystem;
    if (!$wp_filesystem || !is_object($wp_filesystem)) {
        return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    }
    // Unzip can use a lot of memory, but not this much hopefully.
    wp_raise_memory_limit('admin');
    $needed_dirs = array();
    $to = trailingslashit($to);
    // Determine any parent dir's needed (of the upgrade directory)
    if (!$wp_filesystem->is_dir($to)) {
        //Only do parents if no children exist
        $path = preg_split('![/\\\\]!', untrailingslashit($to));
        for ($i = count($path); $i >= 0; $i--) {
            if (empty($path[$i])) {
                continue;
            }
            $dir = implode('/', array_slice($path, 0, $i + 1));
            if (preg_match('!^[a-z]:$!i', $dir)) {
                // Skip it if it looks like a Windows Drive letter.
                continue;
            }
            if (!$wp_filesystem->is_dir($dir)) {
                $needed_dirs[] = $dir;
            } else {
                break;
            }
            // A folder exists, therefor, we dont need the check the levels below this
        }
    }
    /**
     * Filters whether to use ZipArchive to unzip archives.
     *
     * @since 3.0.0
     *
     * @param bool $ziparchive Whether to use ZipArchive. Default true.
     */
    if (class_exists('ZipArchive', false) && apply_filters('unzip_file_use_ziparchive', true)) {
        $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
        if (true === $result) {
            return $result;
        } elseif (is_wp_error($result)) {
            if ('incompatible_archive' != $result->get_error_code()) {
                return $result;
            }
        }
    }
    // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
    return _unzip_file_pclzip($file, $to, $needed_dirs);
}
Example #7
0
/**
 * Streams image in post to browser, along with enqueued changes
 * in $_REQUEST['history']
 *
 * @param int $post_id
 * @return bool
 */
function stream_preview_image($post_id)
{
    $post = get_post($post_id);
    wp_raise_memory_limit('admin');
    $img = wp_get_image_editor(_load_image_to_edit_path($post_id));
    if (is_wp_error($img)) {
        return false;
    }
    $changes = !empty($_REQUEST['history']) ? json_decode(wp_unslash($_REQUEST['history'])) : null;
    if ($changes) {
        $img = image_edit_apply_changes($img, $changes);
    }
    // Scale the image.
    $size = $img->get_size();
    $w = $size['width'];
    $h = $size['height'];
    $ratio = _image_get_preview_ratio($w, $h);
    $w2 = max(1, $w * $ratio);
    $h2 = max(1, $h * $ratio);
    if (is_wp_error($img->resize($w2, $h2))) {
        return false;
    }
    return wp_stream_image($img, $post->post_mime_type, $post_id);
}
 /**
  * Loads image from $this->file into new GD Resource.
  *
  * @since 3.5.0
  * @access protected
  *
  * @return bool|WP_Error True if loaded successfully; WP_Error on failure.
  */
 public function load()
 {
     if ($this->image) {
         return true;
     }
     if (!is_file($this->file) && !preg_match('|^https?://|', $this->file)) {
         return new WP_Error('error_loading_image', __('File doesn’t exist?'), $this->file);
     }
     // Set artificially high because GD uses uncompressed images in memory.
     wp_raise_memory_limit('image');
     $this->image = @imagecreatefromstring(file_get_contents($this->file));
     if (!is_resource($this->image)) {
         return new WP_Error('invalid_image', __('File is not an image.'), $this->file);
     }
     $size = @getimagesize($this->file);
     if (!$size) {
         return new WP_Error('invalid_image', __('Could not read image size.'), $this->file);
     }
     if (function_exists('imagealphablending') && function_exists('imagesavealpha')) {
         imagealphablending($this->image, false);
         imagesavealpha($this->image, true);
     }
     $this->update_size($size[0], $size[1]);
     $this->mime_type = $size['mime'];
     return $this->set_quality();
 }
 /**
  * Loads image from $this->file into new Imagick Object.
  *
  * @since 3.5.0
  * @access protected
  *
  * @return true|WP_Error True if loaded; WP_Error on failure.
  */
 public function load()
 {
     if ($this->image instanceof Imagick) {
         return true;
     }
     if (!is_file($this->file) && !preg_match('|^https?://|', $this->file)) {
         return new WP_Error('error_loading_image', __('File doesn’t exist?'), $this->file);
     }
     /*
      * Even though Imagick uses less PHP memory than GD, set higher limit
      * for users that have low PHP.ini limits.
      */
     wp_raise_memory_limit('image');
     try {
         $this->image = new Imagick();
         $file_extension = strtolower(pathinfo($this->file, PATHINFO_EXTENSION));
         $filename = $this->file;
         if ('pdf' == $file_extension) {
             $filename = $this->pdf_setup();
         }
         // Reading image after Imagick instantiation because `setResolution`
         // only applies correctly before the image is read.
         $this->image->readImage($filename);
         if (!$this->image->valid()) {
             return new WP_Error('invalid_image', __('File is not an image.'), $this->file);
         }
         // Select the first frame to handle animated images properly
         if (is_callable(array($this->image, 'setIteratorIndex'))) {
             $this->image->setIteratorIndex(0);
         }
         $this->mime_type = $this->get_mime_type($this->image->getImageFormat());
     } catch (Exception $e) {
         return new WP_Error('invalid_image', $e->getMessage(), $this->file);
     }
     $updated_size = $this->update_size();
     if (is_wp_error($updated_size)) {
         return $updated_size;
     }
     return $this->set_quality();
 }