1

サイズ変更メソッドで 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;
}
4

2 に答える 2

2

ほら、最初の問題はおそらく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();
于 2013-11-01T19:43:32.277 に答える
0

クラスのプロパティを初期化する必要があります。何が起こっているのかは、次のようなことをしようとしていると思います:

$this->file = $var;

ただし、 $file プロパティが何であるかをクラスに伝える必要があります。

class WaitmageComponent extends Component { 
     public $file = array();
}
于 2013-11-01T18:52:59.113 に答える