resize example

/** * Tests \Drupal\Core\Image\Image::resize(). */
  public function testResize() {
    $this->getTestImageForOperation('Resize');
    $this->toolkitOperation->expects($this->once())
      ->method('execute')
      ->will($this->returnArgument(0));

    // Resize with integer for width and height.     $ret = $this->image->resize(30, 40);
    $this->assertEquals(30, $ret['width']);
    $this->assertEquals(40, $ret['height']);
  }

  /** * Tests \Drupal\Core\Image\Image::resize(). */
  public function testFloatResize() {
    $this->getTestImageForOperation('Resize');
    $this->toolkitOperation->expects($this->once())
      ->method('execute')
      

class ResizeImageEffect extends ConfigurableImageEffectBase {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!$image->resize($this->configuration['width']$this->configuration['height'])) {
      $this->logger->error('Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function transformDimensions(array &$dimensions$uri) {
    // The new image will have the exact dimensions defined for the effect.

function image_make_intermediate_size( $file$width$height$crop = false ) {
    if ( $width || $height ) {
        $editor = wp_get_image_editor( $file );

        if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width$height$crop ) ) ) {
            return false;
        }

        $resized_file = $editor->save();

        if ( ! is_wp_error( $resized_file ) && $resized_file ) {
            unset( $resized_file['path'] );
            return $resized_file;
        }
    }
    return false;
}


    // 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 );
}

/** * Restores the metadata for a given attachment. * * @since 2.9.0 * * @param int $post_id Attachment post ID. * @return stdClass Image restoration message object. */
$origWidth  = $this->image()->origWidth;
        $origHeight = $this->image()->origHeight;

        [$cropWidth$cropHeight] = $this->calcAspectRatio($width$height$origWidth$origHeight);

        if ($height === null) {
            $height = ceil(($width / $cropWidth) * $cropHeight);
        }

        [$x$y] = $this->calcCropCoords($cropWidth$cropHeight$origWidth$origHeight$position);

        return $this->crop($cropWidth$cropHeight$x$y)->resize($width$height);
    }

    /** * Calculate image aspect ratio. * * @param float|int $width * @param float|int|null $height * @param float|int $origWidth * @param float|int $origHeight */
    protected function calcAspectRatio($width$height = null, $origWidth = 0, $origHeight = 0): array
    {

        if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
            $editor = wp_get_image_editor( $file );

            if ( is_wp_error( $editor ) ) {
                // This image cannot be edited.                 return $image_meta;
            }

            // Resize the image.             $resized = $editor->resize( $threshold$threshold );
            $rotated = null;

            // If there is EXIF data, rotate according to EXIF Orientation.             if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
                $resized = $editor->maybe_exif_rotate();
                $rotated = $resized;
            }

            if ( ! is_wp_error( $resized ) ) {
                /* * Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". * This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). */
$size_data['height'] = null;
        }

        if ( ! isset( $size_data['crop'] ) ) {
            $size_data['crop'] = false;
        }

        if ( ( $this->size['width'] === $size_data['width'] ) && ( $this->size['height'] === $size_data['height'] ) ) {
            return new WP_Error( 'image_subsize_create_error', __( 'The image already has the requested size.' ) );
        }

        $resized = $this->resize( $size_data['width']$size_data['height']$size_data['crop'] );

        if ( is_wp_error( $resized ) ) {
            $saved = $resized;
        } else {
            $saved = $this->_save( $this->image );

            $this->image->clear();
            $this->image->destroy();
            $this->image = null;
        }

        

function image_resize( $file$max_w$max_h$crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
    _deprecated_function( __FUNCTION__, '3.5.0', 'wp_get_image_editor()' );

    $editor = wp_get_image_editor( $file );
    if ( is_wp_error( $editor ) )
        return $editor;
    $editor->set_quality( $jpeg_quality );

    $resized = $editor->resize( $max_w$max_h$crop );
    if ( is_wp_error( $resized ) )
        return $resized;

    $dest_file = $editor->generate_filename( $suffix$dest_path );
    $saved = $editor->save( $dest_file );

    if ( is_wp_error( $saved ) )
        return $saved;

    return $dest_file;
}


    public function remove(int $offset, int $length = 1)
    {
        if (!isset($this->elements[$offset])) {
            throw new OutOfBoundsException(sprintf('The offset "%s" is not within the property path.', $offset));
        }

        $this->resize($offset$length, 0);
    }

    /** * Replaces a sub-path by a different (sub-) path. * * @param int $pathOffset The offset where the inserted piece starts in $path * @param int $pathLength The length of the inserted piece; if 0, the full path is inserted * * @return void * * @throws OutOfBoundsException If the offset is invalid */

  public function transformDimensions(array &$dimensions$uri) {
    $dimensions = $this->getUriDependentDimensions($uri);
  }

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    $dimensions = $this->getUriDependentDimensions($image->getSource());
    return $image->resize($dimensions['width']$dimensions['height']);
  }

  /** * Make the image dimensions dependent on the image file extension. * * @param string $uri * Original image file URI. * * @return array * Associative array. * - width: Integer with the derivative image width. * - height: Integer with the derivative image height. */
Home | Imprint | This part of the site doesn't use cookies.