crop example

$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
    {
break;

                case 'crop':
                    $size = $image_editor->get_size();

                    $crop_x = round( ( $size['width'] * $args['left'] ) / 100.0 );
                    $crop_y = round( ( $size['height'] * $args['top'] ) / 100.0 );
                    $width  = round( ( $size['width'] * $args['width'] ) / 100.0 );
                    $height = round( ( $size['height'] * $args['height'] ) / 100.0 );

                    if ( $size['width'] !== $width && $size['height'] !== $height ) {
                        $result = $image_editor->crop( $crop_x$crop_y$width$height );

                        if ( is_wp_error( $result ) ) {
                            return new WP_Error(
                                'rest_image_crop_failed',
                                __( 'Unable to crop this image.' ),
                                array( 'status' => 500 )
                            );
                        }
                    }

                    break;

            }

class CropImageEffect extends ResizeImageEffect {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    [$x$y] = explode('-', $this->configuration['anchor']);
    $x = image_filter_keyword($x$image->getWidth()$this->configuration['width']);
    $y = image_filter_keyword($y$image->getHeight()$this->configuration['height']);
    if (!$image->crop($x$y$this->configuration['width']$this->configuration['height'])) {
      $this->logger->error('Image crop 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 getSummary() {
    $summary = [
      

                break;
            case 'crop':
                $sel = $operation->sel;

                if ( $image instanceof WP_Image_Editor ) {
                    $size = $image->get_size();
                    $w    = $size['width'];
                    $h    = $size['height'];

                    $scale = 1 / _image_get_preview_ratio( $w$h ); // Discard preview scaling.                     $image->crop( $sel->x * $scale$sel->y * $scale$sel->w * $scale$sel->h * $scale );
                } else {
                    $scale = 1 / _image_get_preview_ratio( imagesx( $image )imagesy( $image ) ); // Discard preview scaling.                     $image = _crop_image_resource( $image$sel->x * $scale$sel->y * $scale$sel->w * $scale$sel->h * $scale );
                }
                break;
        }
    }

    return $image;
}


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

    // Cropping with width only should preserve the aspect ratio.     $ret = $this->image->crop(0, 0, 44);
    $this->assertEquals(50, $ret['height']);
  }

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

    
$src = _load_image_to_edit_path( $src, 'full' );
        } else {
            $src = $src_file;
        }
    }

    $editor = wp_get_image_editor( $src );
    if ( is_wp_error( $editor ) ) {
        return $editor;
    }

    $src = $editor->crop( $src_x$src_y$src_w$src_h$dst_w$dst_h$src_abs );
    if ( is_wp_error( $src ) ) {
        return $src;
    }

    if ( ! $dst_file ) {
        $dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file )$src_file );
    }

    /* * The directory containing the original file may no longer exist when * using a replication plugin. */
return true;
        }

        $dims = image_resize_dimensions( $this->size['width']$this->size['height']$max_w$max_h$crop );
        if ( ! $dims ) {
            return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) );
        }

        list( $dst_x$dst_y$src_x$src_y$dst_w$dst_h$src_w$src_h ) = $dims;

        if ( $crop ) {
            return $this->crop( $src_x$src_y$src_w$src_h$dst_w$dst_h );
        }

        // Execute the resize.         $thumb_result = $this->thumbnail_image( $dst_w$dst_h );
        if ( is_wp_error( $thumb_result ) ) {
            return $thumb_result;
        }

        return $this->update_size( $dst_w$dst_h );
    }

    
Home | Imprint | This part of the site doesn't use cookies.