[Symfony] sfThumbnail w backendzie
25 grudnia 2007Wpis przeleżał trochę jako szkic, a właśnie robie małe porządki więc publikuję ;)
W dzisiejszym odcinku będzie o wykorzystaniu sfThumbnailPlugin`u do automatycznego zmniejszania uploadowanych obrazków w panelu admina.
Posłużę się przykładem Portfolio - Klienci. Klasa Client (name, url, image).
Wpisujemy w konsoli:
symfony propel-init-admin backend clients ClientI mamy wygenerowany moduł. Przechodzimy do pliku /backend/modules/clients/config/generator.yml i wrzucamy
generator: class: sfPropelAdminGenerator param: model_class: Client theme: default edit: fields: image: type: admin_input_file_tag params: include_link='./' include_remove=trueTen zabieg zamienia pole image na input[type=file]
Czyścimy cache, odpalamy /backend_dev.php/clients w przeglądarce i jeżeli wszystko jest w porządku (nie ma żadnych błędów etc) otwieramy plik /cache/backend/dev/modules/autoClients/actions/actions.class.php i kopiujemy z niego metodę updateClientFromRequest() do pliku /apps/backend/modules/clients/actions/actions.class.php
Domyślnie ta akcja uploaduje cały plik do folderu /web/uploads/ natomiast ja chcę aby zapisywała się tylko miniaturka i to w folderze /web/upload/clients/
Blok, którym będziemy się zajmować
$currentFile = sfConfig::get('sf_upload_dir')."//".$this->client->getImage();
if (!$this->getRequest()->hasErrors() && isset($client['image_remove']))
{
$this->client->setImage('');
if (is_file($currentFile))
{
unlink($currentFile);
}
}
if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('client[image]'))
{
$fileName = md5($this->getRequest()->getFileName('client[image]').time().rand(0, 99999));
$ext = $this->getRequest()->getFileExtension('client[image]');
if (is_file($currentFile))
{
unlink($currentFile);
}
$this->getRequest()->moveFile('client[image]', sfConfig::get('sf_upload_dir')."//".$fileName.$ext);
$this->client->setImage($fileName.$ext);
}
}
Pierwszą czynnością będzie zmiana folderu:
$currentFile = sfConfig::get('sf_upload_dir')."/clients/".$this->client->getImage();
Następnie dodajemy zmniejszenie rozmiaru obrazka:
$thumbnail = new sfThumbnail(150, 150); // sfThumbnail($width, $height) $thumbnail->loadFile($this->getRequest()->getFilePath('client[image]')); $thumbnail->save(sfConfig::get('sf_upload_dir').'/clients/'.$fileName.$ext);oraz usuwamy linijke:
$this->getRequest()->moveFile('client[image]', sfConfig::get('sf_upload_dir')."//".$fileName.$ext);
Cała metoda powinna wyglądać tak:
public function updateClientFromRequest() { $client = $this->getRequestParameter('client'); if (isset($client['name'])) { $this->client->setName($client['name']); } if (isset($client['url'])) { $this->client->setUrl($client['url']); } $currentFile = sfConfig::get('sf_upload_dir')."/clients/".$this->client->getImage(); // dodane /clients if (!$this->getRequest()->hasErrors() && isset($client['image_remove'])) { $this->client->setImage(''); if (is_file($currentFile)) { unlink($currentFile); } } if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('client[image]')) { $fileName = md5($this->getRequest()->getFileName('client[image]').time().rand(0, 99999)); $ext = $this->getRequest()->getFileExtension('client[image]'); if (is_file($currentFile)) { unlink($currentFile); } $thumbnail = new sfThumbnail(150, 150); // sfThumbnail($width, $height) $thumbnail->loadFile($this->getRequest()->getFilePath('client[image]')); $thumbnail->save(sfConfig::get('sf_upload_dir').'/clients/'.$fileName.$ext); $this->client->setImage($fileName.$ext); } }
5 komentarzy
OT: Super pomysł na logo joga :)
Szczerze? Z braku jakiegoś fajnego zdjęcia ;) Ale wyszło fajnie i chyba tak zostanie.
nie no, pomysł rox ;)
Eh, ja sie tu poce na temat Symfony, a wy mi o topie joga... (:P)
Chusteczką do wytarcia potu? ;P