1

我正在尝试在 resize 方法上使用 foreach 循环创建几个不同大小的拇指。

$sizes = array(
    'thumb' => Configure::read('Shop.image_thumb_dimensions'),
    'medium' => Configure::read('Shop.image_medium_dimensions'),
    'large' => Configure::read('Shop.image_large_dimensions')
);


foreach($sizes as $folder => $size) {

    $destFolder = WWW_ROOT. $this->upload_dir . DS . $folder;

    if (!file_exists($destFolder)) {
        @mkdir($destFolder);
    }

    $dimensionsArray = explode(',', $size);

    $newWidth = $dimensionsArray[0];
    $newHeight = $dimensionsArray[1];

    $destFile = $destFolder . DS . $fileName;

    $resize =  $this->__resize($filePath, $destFile, $newWidth, $newHeight);

}

然后使用组件中的某些方法的 resize 函数如下所示:

private function __resize($src, $destFile, $newWidth, $newHeight) {

    $this->Watimage->setImage($src);
    $this->Watimage->resize(array('type' => 'resizecrop', 'size' => array($newWidth, $newHeight)));
    if ( !$this->Watimage->generate($destFile) ) {
        // handle errors...
        return $this->Watimage->errors;
    }
    else {
        return true;
    }   

}

所以这适用于第一个图像大小(拇指),但此后我得到错误:

b>Notice</b> (8)</a>: Indirect modification of overloaded property WatimageComponent::$file has no effect [<b>APP/Plugin/Gallery/Controller/Component/WatimageComponent.php</b>, line <b>114</b>

我不明白我做错了什么??花了几个小时试图弄清楚这一点。对此事的任何启发将不胜感激。

这是来自组件类的方法:

public function setImage($file) {
    // Remove possible errors...
    $this->errors = array();
    try
    {
        if ( is_array($file) && isset($file['file']) )
        {
            if ( isset($file['quality']) )
                $this->setQuality($file['quality']);
            $file = $file['file'];
        }
        elseif ( empty($file) || (is_array($file) && !isset($file['file'])) )
        {
            throw new Exception('Empty file');
        }

        if ( file_exists($file) )
            $this->file['image'] = $file;
        else
            throw new Exception('File "' . $file . '" does not exist');

        // Obtain extension
        $this->extension['image'] = $this->getFileExtension($this->file['image']);
        // Obtain file sizes
        $this->getSizes();
        // Create image boundary
        $this->image = $this->createImage($this->file['image']);
        $this->handleTransparentImage();
    }
    catch ( Exception $e )
    {
        $this->error($e);
        return false;
    }
    return true;
}
4

2 回答 2

2

There you go, the initial problem is most probably the unsetting of the WaitmageComponent::$file property

unset($this->file);

https://github.com/elboletaire/Watimage/blob/b72e7ac17ad30bfc47ae4d0f31c4ad6795c8f8d2/watimage.php#L706

After doing so, the magic property accessor Component::__get() will kick in when trying to access the now unexistent WaitmageComponent::$file property, and consequently this results in the warning you are receiving.

Instead of unsetting the variable, it should be reinitialized:

$this->file = array();

And of course it should also be initialized properly:

private $file = array();
于 2013-11-01T19:43:32.277 回答
0

你应该在你的类上初始化属性,我认为你正在尝试做类似的事情:

$this->file = $var;

但是您需要告诉您的班级 $file 属性是什么:

class WaitmageComponent extends Component { 
     public $file = array();
}
于 2013-11-01T18:52:59.113 回答