使用数组设置图像大小 - PHP

时间:2015-03-07 17:06:04

标签: php arrays

数组$file_ary包含来自文件输入的图像。它看起来像这样:

Array
(
    [0] => Array
        (
            [name] => Screenshot from 2015-03-06 15:41:58.png
            [type] => image/png
            [tmp_name] => /tmp/php2b8cbW
            [error] => 0
            [size] => 87810
        )

    [1] => Array
        (
            [name] => Screenshot from 2015-03-06 15:45:00.png
            [type] => image/png
            [tmp_name] => /tmp/phpcpmK2A
            [error] => 0
            [size] => 93532
        )

)

我想做的是裁剪每个文件。我想在另一个数组中设置新的宽度和高度。

  $imgSizes = array(
    '100' => '100',
    '150' => '150',
    '130' => '400'
  );


 if(!empty($file_ary)) {

        foreach($file_ary as $file) {

            foreach($imgSizes as $key => $val) {

                echo $file['name'];
                echo '<br>';
                //$resizeObj -> resizeImage(100, 100, 'crop');
                //$resizeObj -> saveImage('images/100/'.$this->id.'.gif', 100);

            }

        }

      }

现在foreach($imgSizes as $key => $val) {正在返回6张图片...每张一张一张,每张一张一张。我想使用$imgSizes数组中的键和值来设置新大小$resizeObj -> resizeImage($key, $val, 'crop');,重命名并保存//$resizeObj -> saveImage('images/'.$key.'/'.$this->id.'.gif', $key);

我不确定这是怎么做的。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你将不得不重写一下数组的结构,例如:

$imgSizes = array(
    0 => array(
        'width'  => '100',
        'height' => '100'
    ),
    1 => array(
        'width'  => '150',
        'height' => '150'
    ),
    2 => array(
        'width'  => '130',
        'height' => '400'
    )
  );

if(!empty($file_ary)) {
    foreach($file_ary as $file) {
        foreach($imgSizes as $key => $val) {
            echo $file['name'];
            echo '<br>';
            $resizeObj -> resizeImage($val['width'], $val['height'], 'crop');
            $resizeObj -> saveImage('images/'.$val['width'].'/'.$this->id.'.gif', $val['width']);
        }
    }
}