Indirect modification of overloaded property Watim

2019-08-05 07:01发布

I am trying to create several thumbs of different sizes using a foreach loop on a resize method.

$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);

}

and then the resize function which uses some methods from a component goes like this:

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;
    }   

}

So this works great for the first image size (the thumb) but thereafter I get the error:

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>

I don't understand what I am doing wrong?? Have spent hours trying to figure this out. Any illumination on the matter will be greatly appreciated.

This is the method from the component class:

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条回答
你好瞎i
2楼-- · 2019-08-05 07:14

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();
查看更多
beautiful°
3楼-- · 2019-08-05 07:36

You should initialize the property on your class, i think what is going on is you are trying to do something like:

$this->file = $var;

But you need to tell your class what is the $file property is:

class WaitmageComponent extends Component { 
     public $file = array();
}
查看更多
登录 后发表回答