重载属性的间接修改WatimageComponent :: $ file没有效果 [英] Indirect modification of overloaded property WatimageComponent::$file has no effect
问题描述
$ sizes = array 我尝试在resize方法中使用foreach循环创建多个不同大小的缩略图。 (
'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函数如下:
私有函数__resize($ src,$ destFile,$ newWidth,$ newHeight){
$ this-> Watimage-> setImage($ src);
$ this-> Watimage-> resize(array('type'=>'resizecrop','size'=> array($ newWidth,$ newHeight)));
if(!$ this-> Watimage-> generate($ destFile)){
//处理错误...
return $ this-> Watimage-&
}
else {
return true;
}
}
图片大小(大拇指),但后来出现错误:
b>注意< / b> (8)< / a> ;:重载属性的间接修改WatimageComponent :: $ file没有效果[< b> APP / Plugin / Gallery / Controller / Component / WatimageComponent.php< / b>,line< b> 114< / b>
我不明白我做错了什么?花了几个小时试图搞清楚这一点。
这是组件类中的方法:
public function setImage($ file){
//删除可能的错误...
$ 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');
//获取扩展
$ this-> extension ['image'] = $ this-> getFileExtension($ this-> file ['image'])
//获取文件大小
$ this-> getSizes();
//创建图像边界
$ this-> image = $ this-> createImage($ this-> file ['image']);
$ this-> handleTransparentImage();
}
catch(Exception $ e)
{
$ this-> error($ e);
return false;
}
return true;
}
问题最可能是 WaitmageComponent :: $ file
属性的设置
unset($ this-> file);
这样做后,当试图访问现在不存在的 WaitmageComponent :: $文件时,魔法属性访问器
Component :: __ get()
代替重设变量,
$ this-> file = array();
当然也应正确初始化:
private $ file = array();
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;
}
There you go, the initial problem is most probably the unsetting of the WaitmageComponent::$file
property
unset($this->file);
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();
这篇关于重载属性的间接修改WatimageComponent :: $ file没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!