コード例 #1
0
 public function cropImage()
 {
     // Check for request forgeries.
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $response = new Prism\Response\Json();
     $userId = JFactory::getUser()->get('id');
     if (!$userId) {
         $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_NOT_LOG_IN'))->failure();
         echo $response;
         $app->close();
     }
     // Get the model
     $model = $this->getModel();
     /** @var $model SocialcommunityModelAvatar */
     $params = JComponentHelper::getParams('com_socialcommunity');
     $filesystemHelper = new Prism\Filesystem\Helper($params);
     // Get the filename from the session.
     $fileName = basename($app->getUserState(Socialcommunity\Constants::TEMPORARY_IMAGE_CONTEXT));
     $temporaryFolder = $filesystemHelper->getTemporaryMediaFolder(JPATH_BASE);
     $temporaryFile = JPath::clean($temporaryFolder . '/' . $fileName);
     if (!$fileName or !JFile::exists($temporaryFile)) {
         $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_FILE_DOES_NOT_EXIST'))->failure();
         echo $response;
         $app->close();
     }
     $imageUrl = '';
     try {
         // Get the folder where the images will be stored
         $params = JComponentHelper::getParams('com_socialcommunity');
         $options = array('width' => $this->input->getFloat('width'), 'height' => $this->input->getFloat('height'), 'x' => $this->input->getFloat('x'), 'y' => $this->input->getFloat('y'), 'destination' => $temporaryFolder);
         // Resize the picture.
         $images = $model->cropImage($temporaryFile, $options, $params);
         jimport('Prism.libs.init');
         $temporaryAdapter = new League\Flysystem\Adapter\Local($temporaryFolder);
         $temporaryFilesystem = new League\Flysystem\Filesystem($temporaryAdapter);
         $storageFilesystem = $filesystemHelper->getFilesystem();
         $manager = new League\Flysystem\MountManager(['temporary' => $temporaryFilesystem, 'storage' => $storageFilesystem]);
         $mediaFolder = $filesystemHelper->getMediaFolder($userId);
         $model->moveImages($images, $mediaFolder, $manager);
         $model->storeImages($userId, $images, $mediaFolder, $storageFilesystem);
         // Prepare URL to the image.
         $imageName = basename(Joomla\Utilities\ArrayHelper::getValue($images, 'image_profile'));
         $imageUrl = $filesystemHelper->getMediaFolderUri($userId) . '/' . $imageName;
         $app->setUserState(Socialcommunity\Constants::TEMPORARY_IMAGE_CONTEXT, null);
     } catch (RuntimeException $e) {
         $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText($e->getMessage())->failure();
         echo $response;
         $app->close();
     } catch (Exception $e) {
         JLog::add($e->getMessage(), JLog::DEBUG);
         $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_SYSTEM'))->failure();
         echo $response;
         $app->close();
     }
     $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_SUCCESS'))->setText(JText::_('COM_SOCIALCOMMUNITY_IMAGE_SAVED'))->setData($imageUrl)->success();
     echo $response;
     $app->close();
 }