间接修改重载属性WatimageComponent :: $ file无效

时间:2013-10-31 20:52:05

标签: image cakephp foreach resize

我正在尝试使用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;
}

2 个答案:

答案 0 :(得分:2)

你去了,最初的问题很可能是WaitmageComponent::$file属性

的取消
unset($this->file);

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

执行此操作后,魔法属性访问者Component::__get()将在尝试访问现在不存在的WaitmageComponent::$file属性时启动,因此会导致您收到警告。

不应取消设置变量,而应重新初始化:

$this->file = array();

当然它也应该正确初始化:

private $file = array();

答案 1 :(得分:0)

您应该在课堂上初始化该属性,我认为您正在尝试做的事情是:

$this->file = $var;

但是你需要告诉你的类$ file属性是什么:

class WaitmageComponent extends Component { 
     public $file = array();
}