scale()

public function scale(float $scale): void

Description

Scales a loaded image by a percentage, adjusting both width and height proportionally to maintain the aspect ratio. This method provides an intuitive way to resize images relative to their current size rather than specifying absolute dimensions.

Load First, Then Scale: This method requires an image to be loaded first using load() or upload(). Calling scale() without a loaded image will throw an Exception with the message "No image is loaded to scale."

Parameters

Parameter Type Description Default Required
$scale float The percentage to scale the image. 100 = original size, <100 = reduce, >100 = enlarge. N/A Yes

Understanding Scale Values

The $scale parameter works as a percentage of the original dimensions:

  • 100: Keeps the original size (no change)
  • 50: Reduces to half size (50% of original)
  • 25: Reduces to quarter size (25% of original)
  • 200: Doubles the size (200% of original)
  • 150: Increases by 50% (150% of original)

Relative vs. Absolute Sizing: Use scale() when you want to resize relative to the current size (e.g., "make it 50% smaller"). Use resize_to_width() or resize_to_height() when you need specific pixel dimensions. The scale() method is particularly useful when processing images of varying sizes with a consistent reduction or enlargement factor.

Return Value

Type Description
void This method does not return a value. It modifies the loaded image resource in place.

Example #1

The code sample below demonstrates the most basic use of scale().

Proportional Scaling: The scale() method maintains aspect ratio automatically by applying the same percentage to both width and height. A scale value of 50 reduces a 2000x1000 image to 1000x500, maintaining the original 2:1 ratio perfectly.

Example #2

The example above shows how to create quick preview images at a fixed percentage of original size.

Percentage-Based Workflow: Using scale() is ideal when you want consistent relative sizing across images of different dimensions. A 30% preview works equally well for a 4000x3000 original (creating 1200x900) and a 2000x1500 original (creating 600x450), maintaining proportions in both cases.

Example #3

The example above demonstrates creating multiple scaled versions from a single source.

Multi-Scale Generation: Creating multiple percentage-based versions is straightforward with scale(). This approach works regardless of the source image dimensions, making it ideal for user-generated content where you can't predict original sizes but want consistent relative scaling across all images.

Example #4

The example above shows using scale() for progressive quality reduction in image optimisation.

Web Optimisation Strategy: Combining scale() with varying compression levels provides excellent file size reduction whilst maintaining acceptable quality. For example, a 60% scale with 85% compression can reduce file size by 80% or more, dramatically improving page load times without noticeable quality loss for web display.

Avoiding Upscaling: Be cautious when using scale values greater than 100, as enlarging images reduces quality through interpolation. If you need larger images, start with higher-resolution sources. The scale() method does not prevent upscaling, so validate dimensions before scaling if quality is critical.