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_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'))->failure(); echo $response; $app->close(); } // Get the model $model = $this->getModel(); /** @var $model CrowdfundingModelProject */ $projectId = $this->input->post->get("id"); // If there is a project, validate the owner. if (!empty($projectId)) { // Validate project owner. $validator = new Crowdfunding\Validator\Project\Owner(JFactory::getDbo(), $projectId, $userId); if (!$validator->isValid()) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'))->failure(); echo $response; $app->close(); } } // Get the filename from the session. $fileName = basename($app->getUserState(Crowdfunding\Constants::TEMPORARY_IMAGE_CONTEXT)); $temporaryFile = JPath::clean(CrowdfundingHelper::getTemporaryImagesFolder() . "/" . $fileName); if (!$fileName or !JFile::exists($temporaryFile)) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_FILE_DOES_NOT_EXIST'))->failure(); echo $response; $app->close(); } $imageUrl = ""; try { // Get the folder where the images will be stored $destination = CrowdfundingHelper::getTemporaryImagesFolder(); $params = JComponentHelper::getParams("com_crowdfunding"); $options = array("width" => $this->input->getFloat("width"), "height" => $this->input->getFloat("height"), "x" => $this->input->getFloat("x"), "y" => $this->input->getFloat("y"), "destination" => $destination, "resize_width" => $params->get("image_width", 200), "resize_height" => $params->get("image_height", 200)); // Resize the picture. $images = $model->cropImage($temporaryFile, $options); $imageName = basename(Joomla\Utilities\ArrayHelper::getValue($images, "image")); // Remove the temporary images if they exist. $temporaryImages = $app->getUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT); if (!empty($temporaryImages)) { $model->removeTemporaryImages($temporaryImages, $destination); } // If there is a project, store the images to database. // If there is NO project, store the images in the session. if (!empty($projectId)) { $model->updateImages($projectId, $images, $destination); $app->setUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT, null); // Get the folder of the images where the pictures will be stored. $imageUrl = JUri::base() . CrowdfundingHelper::getImagesFolderUri() . "/" . $imageName; } else { $app->setUserState(Crowdfunding\Constants::CROPPED_IMAGES_CONTEXT, $images); // Get the temporary folder where the images will be stored. $imageUrl = JUri::base() . CrowdfundingHelper::getTemporaryImagesFolderUri() . "/" . $imageName; } } catch (RuntimeException $e) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText($e->getMessage())->failure(); echo $response; $app->close(); } catch (Exception $e) { $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'))->failure(); echo $response; $app->close(); } $response->setTitle(JText::_('COM_CROWDFUNDING_SUCCESS'))->setText(JText::_('COM_CROWDFUNDING_IMAGE_SAVED'))->setData($imageUrl)->success(); echo $response; $app->close(); }