public function makeThumb($photo, $size = 'small') { if (!$photo) { return false; } $sizes = $this->sizes; $absolutePath = $this->bcms->home_dir; $sourceFilePath = $absolutePath . $photo; $sourceFileName = basename($sourceFilePath); if (!file_exists($sourceFilePath)) { $this->bcms->flash('error', 'Check filename, system can not access it'); return false; } $targetDir = dirname($sourceFilePath) . '/' . $size; $targetFilePath = $targetDir . '/' . $sourceFileName; $targetFileRelativePath = dirname($photo) . '/' . $size . '/' . $sourceFileName; try { $img = new \BCMS\SimpleImage($sourceFilePath); //Check dir is exists if (!file_exists($targetDir)) { if (!mkdir($targetDir)) { $this->bcms->flash('error', 'Can not create folder ' . $targetDir); } } elseif (!is_writable($targetDir)) { $this->bcms->flash('error', $targetDir . ' is not writeable.'); } if ($size === 'small') { //$img->smart_crop($sizes[$size])->save($targetFilePath); $img->best_fit($sizes[$size], $sizes[$size])->save($targetFilePath); } elseif ($size === 'medium') { $img->best_fit($sizes[$size], $sizes[$size])->save($targetFilePath); } elseif ($size === 'medium_square') { $img->smart_crop($sizes[$size])->save($targetFilePath); } return $targetFileRelativePath; } catch (Exception $e) { $this->bcms->flash('error', $e->getMessage()); } }
public function uploadPhoto() { $log = $this->bcms->getLog(); $view = $this->bcms->view(); $req = $this->bcms->request(); if (!empty($this->bcms->user)) { $user = $this->bcms->user; $log->info('Uploading profile photo for ' . $user['email']); } $error = false; $user = $this->bcms->users->get($user['id']); $profile = $this->bcms->users->getProfile($user['id']); // Photo manipulation if (!empty($_FILES)) { $uploadDir = '/upload/users'; $uploadDirFull = $this->bcms->home_dir . $uploadDir; if (!file_exists($uploadDirFull)) { mkdir($uploadDirFull, 0777, true); } if (!empty($_FILES['photo']) && $_FILES['photo']['error'] !== 4) { if (!$_FILES['photo']['error']) { if ($profile->photo) { if (file_exists($this->bcms->home_dir . '/' . $profile->photo)) { @unlink($this->bcms->home_dir . '/' . $profile->photo); } } $userPhoto = $uploadDirFull . '/' . $_FILES['photo']['name']; if (!in_array(strtolower(pathinfo($userPhoto, PATHINFO_EXTENSION)), array('jpeg', 'jpg'))) { $error = 'Please select a jpg file'; $log->error('Please select a jpg file'); return $error; } if (move_uploaded_file($_FILES['photo']['tmp_name'], $userPhoto)) { $newFileName = uniqid() . '.' . pathinfo($userPhoto, PATHINFO_EXTENSION); rename($userPhoto, $uploadDirFull . '/' . $newFileName); $log->info($newFileName); $profile->photo = $uploadDir . '/' . $newFileName; $profile->save(); try { $img = new \BCMS\SimpleImage($this->bcms->home_dir . '/' . $profile->photo); $img->smart_crop(300, 300)->save($this->bcms->home_dir . '/' . $profile->photo); } catch (\Exception $e) { $error = 'There is a error while image processing'; $log->error('There is a error while image processing'); return $error; } } else { $error = 'There is a error while image processing, please try again'; $log->error('There is a error while image processing, please try again'); return $error; } } else { $error = 'Error while uploading file'; $log->error('Error while uploading file'); return $error; } } } else { $error = 'Error while uploading file'; $log->error(print_r($_FILES, true)); $log->error(print_r($_POST, true)); return $error; } return $error; }