From ac70ea60e30c63afad0805580d15ba779a63a0e8 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sat, 12 May 2018 16:41:52 +0200 Subject: [PATCH] image cropping --- lam/lib/modules/windowsUser.inc | 86 +++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/lam/lib/modules/windowsUser.inc b/lam/lib/modules/windowsUser.inc index 11baceed..3d613661 100644 --- a/lam/lib/modules/windowsUser.inc +++ b/lam/lib/modules/windowsUser.inc @@ -1744,24 +1744,70 @@ class windowsUser extends baseModule implements passwordService { * * @return array meta HTML code */ - function display_html_photo() { + public function display_html_photo() { $container = new htmlTable(); - $label = _('Photo file'); - $container->addElement(new htmlTableExtendedInputFileUpload('photoFile', $label, 'photoUpload'), true); - $buttonContainer = new htmlTable(); - $buttonContainer->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'submit', _('Add photo'))); - $buttonContainer->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'back', _('Back'))); - $container->addElement($buttonContainer); + if (empty($this->attributes['jpegPhoto'][0])) { + $container->addElement(new htmlSubTitle(_('Upload image')), true); + $label = _('Photo file'); + $container->addElement(new htmlTableExtendedInputFileUpload('photoFile', $label, 'photoUpload'), true); + $container->addVerticalSpace('1rem'); + $buttonContainer = new htmlTable(); + $buttonContainer->addElement(new htmlAccountPageButton(get_class($this), 'photo', 'upload', _('Upload'))); + $buttonContainer->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'back', _('Back'))); + $container->addElement($buttonContainer); + } + else { + $container->addElement(new htmlSubTitle(_('Crop image')), true); + $jpeg_filename = 'jpg' . getRandomNumber() . '.jpg'; + $outjpeg = @fopen(dirname(__FILE__) . '/../../tmp/' . $jpeg_filename, "wb"); + fwrite($outjpeg, $this->attributes['jpegPhoto'][0]); + fclose ($outjpeg); + $photoFile = '../../tmp/' . $jpeg_filename; + $img = new htmlImage($photoFile); + $img->setCSSClasses(array('photo')); + $img->enableCropping(); + $container->addElement($img, true); + $container->addVerticalSpace('1rem'); + $buttonContainer = new htmlTable(); + $buttonContainer->addElement(new htmlAccountPageButton(get_class($this), 'attributes', 'crop', _('Done'))); + $container->addElement($buttonContainer, true); + } return $container; } /** * Sets a new photo. */ - function process_photo() { - if (!isset($_POST['form_subpage_' . get_class($this) . '_attributes_submit'])) { + public function process_photo() { + if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_back'])) { return array(); } + if (isset($_POST['form_subpage_' . get_class($this) . '_photo_upload'])) { + return $this->uploadPhoto(); + } + if (isset($_POST['form_subpage_' . get_class($this) . '_attributes_crop'])) { + $messages = array(); + $image = new Imagick(); + try { + $image->readImageBlob($this->attributes['jpegPhoto'][0]); + $image->cropimage($_POST['croppingDataWidth'], $_POST['croppingDataHeight'], $_POST['croppingDataX'], $_POST['croppingDataY']); + $this->attributes['jpegPhoto'][0] = $image->getimageblob(); + } + catch (Exception $e) { + $msg = $this->messages['file'][2]; + $msg[] = htmlspecialchars($e->getMessage()); + $messages[] = $msg; + } + return $messages; + } + } + + /** + * Uploads the photo file. + * + * @return array error messages if any + */ + private function uploadPhoto() { $messages = array(); if ($_FILES['photoFile'] && ($_FILES['photoFile']['size'] > 0)) { $name = $_FILES['photoFile']['name']; @@ -1775,19 +1821,19 @@ class windowsUser extends baseModule implements passwordService { return array($errMsg); } fclose($handle); - // convert to JPG if imagick extension is available + // convert to JPG $image = new Imagick(); try { - $image->readImageBlob($data); - // resize if maximum values specified - if (!empty($this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]) || !empty($this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0])) { - $maxWidth = empty($this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]) ? $image->getimagewidth() : $this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]; - $maxHeight = empty($this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0]) ? $image->getimageheight() : $this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0]; - $image->thumbnailimage($maxWidth, $maxHeight, true); - } - $image->setImageCompression(Imagick::COMPRESSION_JPEG); - $image->setImageFormat('jpeg'); - $data = $image->getimageblob(); + $image->readImageBlob($data); + // resize if maximum values specified + if (!empty($this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]) || !empty($this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0])) { + $maxWidth = empty($this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]) ? $image->getimagewidth() : $this->moduleSettings['windowsUser_jpegPhoto_maxWidth'][0]; + $maxHeight = empty($this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0]) ? $image->getimageheight() : $this->moduleSettings['windowsUser_jpegPhoto_maxHeight'][0]; + $image->thumbnailimage($maxWidth, $maxHeight, true); + } + $image->setImageCompression(Imagick::COMPRESSION_JPEG); + $image->setImageFormat('jpeg'); + $data = $image->getimageblob(); } catch (Exception $e) { $msg = $this->messages['file'][2];