Using foreach() On An Array Of Size Values With The resizeImage() Function Of Imagick Not Working – PHP

  foreach, imagick, loops, php

I have an image I want to re-size to different sizes using the resizeImage() function in Imagick.

I’m setting a series of widths in an associateive array and then looping through them with a foreach() loop.

When I remove the foreach loop and manually add a specific size as the width parameter (e.g. 150) it works, but when I take the values from the associate array with the loop it doesn’t?

// create new class of image from image in memory/tmp_folder
$image = new Imagick($temp);

$sizesjpeg = [
        'tiny_' => 50,
        'small_' => 150,
        'medium_' => 500,
        'large_' => 1500,

$destinationFolder = 'images_lib/';

// image processing of different sizes from the array
foreach($sizesjpeg as $sizename => $size) {

    $image->writeImage($destinationFolder . $file);


The error message I’m getting is:

Fatal error: Uncaught ImagickException: Can not process empty Imagick object

Source: Ask PHP