phalcon/cphalcon · error · Phalcon\Image\Exceptions\MissingWidth

Width must be specified

Error message

Width must be specified

What it means

With master = Phalcon\Image\Enum::WIDTH the adapter scales the image proportionally based on the given width, so width is mandatory; a null width throws MissingWidth (the height value is derived and ignored).

Source

Thrown at phalcon/Image/Adapter/AbstractAdapter.zep:578

     * @throws Exception
     */
    private function checkResizeInput(
        int width = null,
        int height = null,
        int master = Enum::AUTO
    ) -> void {
        switch master {
            case Enum::TENSILE:
            case Enum::AUTO:
            case Enum::INVERSE:
            case Enum::PRECISE:
                if (null === width || null === height) {
                    throw new MissingDimensions();
                }
                break;
            case Enum::WIDTH:
                if (null === width) {
                    throw new MissingWidth();
                }
                break;
            case Enum::HEIGHT:
                if (null === height) {
                    throw new MissingHeight();
                }
                break;
            default:
                break;
        }
    }

    private function checkResizeMaster(
        int width = null,
        int height = null,
        int master = Enum::AUTO
    ) -> int {
        if (master === Enum::AUTO) {

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Supply the width: ->resize(800, null, Enum::WIDTH);
  2. If only the height is known, switch master: ->resize(null, 600, Enum::HEIGHT);
  3. Default the width from the source image: ->resize($image->getWidth(), null, Enum::WIDTH);

Example fix

// before
$image->resize(null, 600, \Phalcon\Image\Enum::WIDTH); // throws MissingWidth

// after
$image->resize(800, null, \Phalcon\Image\Enum::WIDTH); // height derived
Defensive patterns

Strategy: validation

Validate before calling

use Phalcon\Image\Enum;

if (Enum::WIDTH === $master && null === $width) {
    throw new \InvalidArgumentException('WIDTH master requires a width');
}
$image->resize($width, $height, $master);

Type guard

function hasRequiredWidth(?int $width, int $master): bool
{
    return \Phalcon\Image\Enum::WIDTH !== $master || $width !== null;
}

Try / catch

try { $image->resize($w, $h, \Phalcon\Image\Enum::WIDTH); } catch (\Phalcon\Image\Exceptions\MissingWidth $e) { $image->resize($image->getWidth(), null, \Phalcon\Image\Enum::WIDTH); // fall back to source width
}

Prevention

When it happens

Trigger: $image->resize(null, 600, Enum::WIDTH); - width omitted while the WIDTH master is selected.

Common situations: Swapping argument order relative to the master constant; dynamic width values (e.g. from request input) arriving null; copying a HEIGHT example and changing only the master.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/5a768b026e3f571a. Report an issue: GitHub.