我想在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);
}
,然後它利用一些方法從組件的大小調整功能是這樣的:
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;
}
好,你發佈一些代碼,但實際的代碼,其中錯誤被觸發失蹤。另外請始終提及您的確切CakePHP版本!這就是說,搜索「_Indirect修改重載的屬性_」,你會發現通常會導致此錯誤的大量信息(例如http://stackoverflow.com/q/10454779/1392379)。 – ndm
重點在於它首次運行,但在循環的上下文中,$文件似乎未被設置,並且重載屬性的間接修改只發生在循環的第二次迭代中。我相信我正在運行cakephp 2.4.2的最新版本,但是這個錯誤並不是蛋糕特有的。我已經閱讀了超載財產,但沒有接近擺動的問題。 – Manu
它可能不是Cake特有的,但不管它是什麼,沒有看到觸發錯誤的代碼,沒有人能夠給你一個具體的解決方案。所以,請顯示組件代碼。 – ndm