Last active
September 10, 2015 18:37
-
-
Save adeonir/2f55d8ff6cc17dc38f43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function add() | |
{ | |
$image = $this->Images->newEntity(); | |
if ($this->request->is('post')) { | |
$image = $this->Images->patchEntity($image, $this->request->data); | |
$album_id = $image['album_id']; | |
foreach ($image['image_file'] as $file) { | |
$image['image'] = $this->Upload->send($file, $album_id); | |
if ($this->Images->save($image)) { | |
$this->Flash->success(__('The image has been saved.')); | |
} else { | |
$this->Flash->error(__('The image could not be saved. Please, try again.')); | |
} | |
} | |
return $this->redirect(['action' => 'index']); | |
} | |
$albums = $this->Images->Albums->find('list', ['limit' => 200]); | |
$this->set(compact('image', 'albums')); | |
$this->set('_serialize', ['image']); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Component; | |
use Cake\Controller\Component; | |
use Cake\Controller\ComponentRegistry; | |
use Cake\Filesystem\Folder; | |
use Cake\Filesystem\File; | |
use WideImage\WideImage; | |
/** | |
* Upload component | |
*/ | |
class UploadComponent extends Component | |
{ | |
protected $config = [ | |
'root' => WWW_ROOT . 'img' . DS, | |
'suffix' => '_file', | |
'path' => 'uploads/images/:id/:md5', | |
'width' => 800, | |
'height' => 450 | |
]; | |
public function send($data, $album_id) | |
{ | |
if (!empty($data)) { | |
$filename = (new File($data['name'], false))->name(); | |
$extension = (new File($data['name'], false))->ext(); | |
$allowed = array('png','jpg','jpeg',''); | |
if (!in_array(substr(strrchr($data['name'], '.'), 1), $allowed)) { | |
throw new \ErrorException("File format not allowed."); | |
} | |
$uploadPath = $this->getUploadPath($album_id, $this->config['path'], $extension); | |
if (!$uploadPath) { | |
throw new \ErrorException(__('Error to get the uploadPath.')); | |
} | |
$folder = new Folder($this->config['root']); | |
$folder->create($this->config['root'] . dirname($uploadPath)); | |
$upload = WideImage::load($data['tmp_name']) | |
->resize($this->config['width'], $this->config['height'], 'outside') | |
->crop('center', 'center', $this->config['width'], $this->config['height']) | |
->saveToFile($this->config['root'] . $uploadPath); | |
return $uploadPath; | |
} else { | |
$this->Flash->error('The image could not be saved. Please, try again.'); | |
} | |
} | |
private function getUploadPath($album_id, $path = false, $extension = false) | |
{ | |
if ($extension === false || $path === false) { | |
return false; | |
} | |
$path = trim($path, DS); | |
$identifiers = [ | |
':id' => $album_id, | |
':md5' => md5(rand() . uniqid() . time()), | |
':y' => date('Y'), | |
':m' => date('m') | |
]; | |
return strtr($path, $identifiers) . '.' . strtolower($extension); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment