Commit b00ce011 authored by Carsten  Rose's avatar Carsten Rose
Browse files

#5023 / Fabric: Cut, rotate and enhance uploaded images

HelperFormElement.php: AppendFormElementNameImageCut()
Support.php: Force Sanatize Class fuer FE.type=imageCut.
AbstractBuildForm.php: Add source filename to SIP
QuickFormQuery.php: Call $save->processAllImageCutFE();
Save.php: processAllImageCutFE(), extractImageDataReplaceFile()
parent 10629efb
......@@ -2969,6 +2969,12 @@ abstract class AbstractBuildForm {
}
}
if ($mode === FORM_LOAD) {
// Store source filename in SIP
$field = HelperFormElement::AppendFormElementNameImageCut($formElement);
$this->store->setVar($field, $value, STORE_SIP, false);
}
$htmlFabricId = $formElement[FE_HTML_ID];
$htmlFabricImageId = $formElement[FE_HTML_ID] . '-image';
......
......@@ -205,6 +205,7 @@ const ERROR_IO_DIR_EXIST_AS_FILE = 1309;
const ERROR_IO_CHDIR = 1310;
const ERROR_IO_CREATE_FILE = 1311;
const ERROR_IO_COPY_FILE = 1312;
const ERROR_IO_FILE_NOT_FOUND = 1313;
//Report
const ERROR_UNKNOWN_LINK_QUALIFIER = 1400;
......@@ -952,6 +953,7 @@ const FE_FILE_BUTTON_TEXT_DEFAULT = 'Choose File';
const FE_IMAGE_CUT_RESIZE_WIDTH = 'resizeWidth';
const FE_IMAGE_CUT_KEEP_ORIGINAL = 'keepOriginal';
const FE_IMAGE_CUT_ORIGINAL_EXTENSION = '.save';
const FE_FLAG_ROW_OPEN_TAG = '_flagRowOpenTag'; // will be automatically computed during Formload: true | false
const FE_FLAG_ROW_CLOSE_TAG = '_flagRowCloseTag'; // will be automatically computed during Formload: true | false
......
......@@ -385,6 +385,9 @@ class QuickFormQuery {
// SAVE
$save = new Save($this->formSpec, $this->feSpecAction, $this->feSpecNative, $this->feSpecNativeRaw);
$save->processAllImageCutFE();
$rc = $save->process();
// Reload fresh saved record and fill STORE_RECORD with it.
......
......@@ -350,6 +350,95 @@ class Save {
}
}
/**
* Process all Upload Formelements for the given $recordId. After processing &$formValues will be updated with the
* final filenames.
*
*/
public function processAllImageCutFE() {
foreach ($this->feSpecNative AS $formElement) {
// skip non upload formElements
if ($formElement[FE_TYPE] != FE_TYPE_IMAGE_CUT) {
continue;
}
$this->extractImageDataReplaceFile($formElement);
}
}
/**
*
* @param array $formElement
* @throws CodeException
* @throws UserFormException
*/
private function extractImageDataReplaceFile(array $formElement) {
// Take care the necessary target directories exist.
$cwd = getcwd();
$sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
if ($cwd === false || $sitePath === false || !chdir($sitePath)) {
throw new UserFormException("getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed.", ERROR_IO_CHDIR);
}
// Get original pathfilename
$field = HelperFormElement::AppendFormElementNameImageCut($formElement);
$pathFileName = $this->store->getVar($field, STORE_SIP);
if ($pathFileName == '' || !file_exists($pathFileName)) {
throw new UserFormException('Empty file or file not found: ' . $pathFileName, ERROR_IO_FILE_NOT_FOUND);
}
// 'data:image/png;base64,AAAFBfj42Pj4...';
$data = $this->store->getVar($formElement[FE_NAME], STORE_FORM, SANITIZE_ALLOW_ALLBUT);
// Replace data by pathFileName (that is stored in DB).
$this->store->setVar($formElement[FE_NAME], $pathFileName, STORE_FORM, true);
if ($data == '') {
return; // Nothing to do
}
// Split base64 encoded image: 'data:image/png;base64,AAAFBfj42Pj4...'
list($type, $imageData) = explode(';', $data, 2); // $type= 'data:image/png;', $imageData='base64,AAAFBfj42Pj4...'
list(, $extension) = explode('/', $type); // $type='png'
list(, $imageData) = explode(',', $imageData); // $imageData='AAAFBfj42Pj4...'
// If undefined: set default. BTW: Defined and empty means "no original".
if (!isset($formElement[FE_IMAGE_CUT_KEEP_ORIGINAL])) {
$formElement[FE_IMAGE_CUT_KEEP_ORIGINAL] = FE_IMAGE_CUT_ORIGINAL_EXTENSION;
}
$extSave = $formElement[FE_IMAGE_CUT_KEEP_ORIGINAL];
$pathParts = pathinfo($pathFileName);
// Keep the original file?
if ($extSave != '') {
// In case the leading '.' is missing.
if ($extSave[0] != ".") {
$extSave = '.' . $extSave;
}
// Check if there is already an original - don't create an additional one.
if (!file_exists($pathFileName . $extSave) &&
!file_exists($pathParts['dirname'] . $pathParts['filename'] . $extSave)
) {
if (!rename($pathFileName, $pathFileName . $extSave)) {
throw new UserFormException("Rename file: '$pathFileName' > '$pathFileName$extSave'", ERROR_IO_RENAME);
}
}
}
if ($extension != $pathParts['extension']) {
$pathFileName .= "." . $extension;
}
if (false === file_put_contents($pathFileName, base64_decode($imageData))) {
throw new UserFormException("Write new image failed: $pathFileName", ERROR_IO_WRITE);
}
$this->store->setVar($formElement[FE_NAME], $pathFileName, STORE_FORM, true);
}
/**
* Process upload for the given Formelement. If necessary, delete a previous uploaded file.
* Calculate the final path/filename and move the file to the new location.
......
......@@ -111,6 +111,18 @@ class HelperFormElement {
return "$field" . HTML_DELIMITER_NAME . "$id";
}
/**
* Build the internal FE name for an imageCut element (used only in SIP): <field>-imageCut
*
* @param array $formElement
* @return string
*/
public static function AppendFormElementNameImageCut(array $formElement) {
$field = ($formElement[FE_NAME] == '') ? $formElement[FE_ID] : $formElement[FE_NAME];
return "$field" . HTML_DELIMITER_NAME . FE_TYPE_IMAGE_CUT;
}
/**
* Build the FE id: <$formId>-<$formElementId>-<$formElementCopy>
*
......@@ -125,6 +137,7 @@ class HelperFormElement {
return "$formId" . HTML_DELIMITER_ID . "$formElementId" . HTML_DELIMITER_ID . "$recordId" . HTML_DELIMITER_ID . "$formElementCopy";
}
/**
* In an array for $feSpecNative, set FE_HTML_ID for all fe.class=FE_CONTAINER Elements.
*
......
......@@ -756,6 +756,14 @@ class Support {
self::setIfNotSet($formElement, FE_INPUT_EXTRA_BUTTON_INFO_CLASS, $store->getVar(FE_INPUT_EXTRA_BUTTON_INFO_CLASS, STORE_SYSTEM));
switch ($formElement[FE_TYPE]) {
case FE_TYPE_IMAGE_CUT:
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALLBUT;
break;
default:
break;
}
return $formElement;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment