间接修改重载属性 WatimageComponent::$file 不起作用



我正在尝试使用调整大小方法上的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;
}

你去吧,最初的问题很可能是取消设置 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();

你应该初始化你的类的属性,我认为正在发生的事情是你正在尝试做这样的事情:

$this->file = $var;

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

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