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