首页 > 解决方案 > 在 Imagick 的 resizeImage() 函数不起作用的情况下,在大小值数组上使用 foreach() - PHP

问题描述

resizeImage()我有一张图像,我想使用Imagick中的函数将其重新调整为不同的尺寸。

我在关联数组中设置了一系列宽度,然后用循环遍历它们foreach()

当我删除foreach循环并手动添加特定大小作为宽度参数(例如150)时,它可以工作,但是当我从关联数组中获取值与循环时,它不会?

// 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) {

  // setting the height to 0 means it only resizes the width thus keeping the image ratio
  $image->resizeImage($size, 0, Imagick::FILTER_LANCZOS, 1);
  $image->writeImage($destinationFolder . $file);

}
$image->clear();

更新将方法移到$image->clear();foreach 循环之外后,我不再收到错误消息,但我只得到了一个非常模糊的图像,它是数组中最后一个键的尺寸(1500px 宽)。

标签: phploopsforeachimagick

解决方案


推荐阅读