WP_Image_Editor_Imagick::multi_resize PHP Метод

multi_resize() публичный Метод

Resize multiple images from a single source.
С версии: 3.5.0
public multi_resize ( array $sizes ) : array
$sizes array { An array of image size arrays. Default sizes are 'small', 'medium', 'medium_large', 'large'. Either a height or width must be provided. If one of the two is set to null, the resize will maintain aspect ratio according to the provided dimension. @type array $size { Array of height, width values, and whether to crop. @type int $width Image width. Optional if `$height` is specified. @type int $height Image height. Optional if `$width` is specified. @type bool $crop Optional. Whether to crop the image. Default false. } }
Результат array An array of resized images' metadata by size.
    public function multi_resize($sizes)
    {
        $metadata = array();
        $orig_size = $this->size;
        $orig_image = $this->image->getImage();
        foreach ($sizes as $size => $size_data) {
            if (!$this->image) {
                $this->image = $orig_image->getImage();
            }
            if (!isset($size_data['width']) && !isset($size_data['height'])) {
                continue;
            }
            if (!isset($size_data['width'])) {
                $size_data['width'] = null;
            }
            if (!isset($size_data['height'])) {
                $size_data['height'] = null;
            }
            if (!isset($size_data['crop'])) {
                $size_data['crop'] = false;
            }
            $resize_result = $this->resize($size_data['width'], $size_data['height'], $size_data['crop']);
            $duplicate = $orig_size['width'] == $size_data['width'] && $orig_size['height'] == $size_data['height'];
            if (!is_wp_error($resize_result) && !$duplicate) {
                $resized = $this->_save($this->image);
                $this->image->clear();
                $this->image->destroy();
                $this->image = null;
                if (!is_wp_error($resized) && $resized) {
                    unset($resized['path']);
                    $metadata[$size] = $resized;
                }
            }
            $this->size = $orig_size;
        }
        $this->image = $orig_image;
        return $metadata;
    }

Usage Example

 /**
  * Test multi_resize with multiple sizes
  *
  * ticket 26823
  */
 public function test_multi_resize()
 {
     $file = DIR_TESTDATA . '/images/waffles.jpg';
     $imagick_image_editor = new WP_Image_Editor_Imagick($file);
     $imagick_image_editor->load();
     $sizes_array = array(array('width' => 10, 'height' => 10, 'crop' => false), array('width' => 75, 'height' => 50, 'crop' => true), array('width' => 9999, 'height' => 20, 'crop' => false), array('width' => 45, 'height' => 9999, 'crop' => true), array('width' => 50), array('width' => 55, 'height' => null), array('height' => 55), array('width' => null, 'height' => 60), array('width' => -9999, 'height' => 70), array('width' => 200, 'height' => -9999));
     $resized = $imagick_image_editor->multi_resize($sizes_array);
     $expected_array = array(array('file' => 'waffles-10x7.jpg', 'width' => 10, 'height' => 7, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-75x50.jpg', 'width' => 75, 'height' => 50, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-30x20.jpg', 'width' => 30, 'height' => 20, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-45x400.jpg', 'width' => 45, 'height' => 400, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-50x33.jpg', 'width' => 50, 'height' => 33, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-55x37.jpg', 'width' => 55, 'height' => 37, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-83x55.jpg', 'width' => 83, 'height' => 55, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-90x60.jpg', 'width' => 90, 'height' => 60, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-105x70.jpg', 'width' => 105, 'height' => 70, 'mime-type' => 'image/jpeg'), array('file' => 'waffles-200x133.jpg', 'width' => 200, 'height' => 133, 'mime-type' => 'image/jpeg'));
     $this->assertNotNull($resized);
     $this->assertEquals($expected_array, $resized);
     foreach ($resized as $key => $image_data) {
         $image_path = DIR_TESTDATA . '/images/' . $image_data['file'];
         // Now, verify real dimensions are as expected
         $this->assertImageDimensions($image_path, $expected_array[$key]['width'], $expected_array[$key]['height']);
     }
 }
All Usage Examples Of WP_Image_Editor_Imagick::multi_resize