static function checkAndPrepare($formname, $key = 'we_document')
 {
     // check to see if there is an image to create or to change
     if (isset($_FILES['we_ui_' . $formname]) && is_array($_FILES['we_ui_' . $formname])) {
         if (isset($_FILES['we_ui_' . $formname]['name']) && is_array($_FILES['we_ui_' . $formname]['name'])) {
             foreach ($_FILES['we_ui_' . $formname]['name'] as $imgName => $filename) {
                 $_imgDataId = we_base_request::_(we_base_request::STRING, 'WE_UI_IMG_DATA_ID_' . $imgName);
                 if ($_imgDataId !== false && isset($_SESSION[$_imgDataId])) {
                     $_SESSION[$_imgDataId]['doDelete'] = false;
                     if (we_base_request::_(we_base_request::BOOL, 'WE_UI_DEL_CHECKBOX_' . $imgName)) {
                         $_SESSION[$_imgDataId]['doDelete'] = true;
                         $_SESSION[$_imgDataId]['id'] = $_SESSION[$_imgDataId]['id'] ?: (intval($GLOBALS[$key][$formname]->getElement($imgName)) ?: 0);
                     } elseif ($filename) {
                         // file is selected, check to see if it is an image
                         $ct = getContentTypeFromFile($filename);
                         if ($ct == we_base_ContentTypes::IMAGE) {
                             $imgId = intval($GLOBALS[$key][$formname]->getElement($imgName));
                             // move document from upload location to tmp dir
                             $_SESSION[$_imgDataId]['serverPath'] = TEMP_PATH . we_base_file::getUniqueId();
                             move_uploaded_file($_FILES['we_ui_' . $formname]['tmp_name'][$imgName], $_SESSION[$_imgDataId]['serverPath']);
                             $we_size = we_thumbnail::getimagesize($_SESSION[$_imgDataId]['serverPath']);
                             if (empty($we_size)) {
                                 unset($_SESSION[$_imgDataId]);
                                 return;
                             }
                             $tmp_Filename = $imgName . '_' . we_base_file::getUniqueId() . '_' . preg_replace('/[^A-Za-z0-9._-]/', '', $_FILES['we_ui_' . $formname]['name'][$imgName]);
                             if ($imgId) {
                                 $_SESSION[$_imgDataId]['id'] = $imgId;
                             }
                             $_SESSION[$_imgDataId]['fileName'] = preg_replace('#^(.+)\\..+$#', '$1', $tmp_Filename);
                             $_SESSION[$_imgDataId]['extension'] = strpos($tmp_Filename, '.') > 0 ? preg_replace('#^.+(\\..+)$#', '$1', $tmp_Filename) : '';
                             $_SESSION[$_imgDataId]['text'] = $_SESSION[$_imgDataId]['fileName'] . $_SESSION[$_imgDataId]['extension'];
                             //image needs to be scaled
                             if (isset($_SESSION[$_imgDataId]['width']) && $_SESSION[$_imgDataId]['width'] || isset($_SESSION[$_imgDataId]['height']) && $_SESSION[$_imgDataId]['height']) {
                                 $imageData = we_base_file::load($_SESSION[$_imgDataId]['serverPath']);
                                 $thumb = new we_thumbnail();
                                 $thumb->init('dummy', $_SESSION[$_imgDataId]['width'], $_SESSION[$_imgDataId]['height'], $_SESSION[$_imgDataId]['keepratio'], $_SESSION[$_imgDataId]['maximize'], false, false, '', 'dummy', 0, '', '', $_SESSION[$_imgDataId]['extension'], $we_size[0], $we_size[1], $imageData, '', $_SESSION[$_imgDataId]['quality']);
                                 $imgData = '';
                                 $thumb->getThumb($imgData);
                                 we_base_file::save($_SESSION[$_imgDataId]['serverPath'], $imageData);
                                 $we_size = we_thumbnail::getimagesize($_SESSION[$_imgDataId]['serverPath']);
                             }
                             $_SESSION[$_imgDataId]['imgwidth'] = $we_size[0];
                             $_SESSION[$_imgDataId]['imgheight'] = $we_size[1];
                             $_SESSION[$_imgDataId]['type'] = $_FILES['we_ui_' . $formname]['type'][$imgName];
                             $_SESSION[$_imgDataId]['size'] = $_FILES['we_ui_' . $formname]['size'][$imgName];
                         }
                     }
                 }
             }
         }
     }
 }
 public static function deleteByImageID($id)
 {
     $thumbsdir = self::getThumbDirectory(true);
     $dir_obj = @dir($thumbsdir);
     $filestodelete = array();
     if ($dir_obj) {
         while (false !== ($entry = $dir_obj->read())) {
             switch ($entry) {
                 case '.':
                 case '..':
                     continue;
                 default:
                     if (substr($entry, 0, strlen($id) + 1) == $id . '_') {
                         $filestodelete[] = $thumbsdir . '/' . $entry;
                     }
             }
         }
     }
     $previewDir = WE_THUMB_PREVIEW_PATH;
     $dir_obj = @dir($previewDir);
     if ($dir_obj) {
         while (false !== ($entry = $dir_obj->read())) {
             switch ($entry) {
                 case '.':
                 case '..':
                     continue;
                 default:
                     if (substr($entry, 0, strlen($id) + 1) == $id . "_" || substr($entry, 0, strlen($id) + 1) == $id . '.') {
                         $filestodelete[] = $previewDir . '/' . $entry;
                     }
             }
         }
     }
     foreach ($filestodelete as $p) {
         we_base_file::deleteLocalFile($p);
     }
 }
 public static function createPreviewThumb($imgSrc, $imgID, $width, $height, &$outputFormat = "jpg", $outputQuality = 75, $tmpName = "")
 {
     if (self::gd_version() == 0) {
         $outputFormat = 'gif';
         return ICON_DIR . 'image.gif';
     }
     if (substr($imgSrc, 0, strlen($_SERVER['DOCUMENT_ROOT'])) == $_SERVER['DOCUMENT_ROOT']) {
         // it is no src, it is a server path
         $imgSrc = substr($imgSrc, strlen($_SERVER['DOCUMENT_ROOT']));
     }
     $imgSrc = '/' . ltrim($imgSrc, '/');
     $_imgPath = $_SERVER['DOCUMENT_ROOT'] . WEBEDITION_DIR . '../' . $imgSrc;
     $path_parts = pathinfo($_imgPath);
     if (isset($path_parts['extension']) && ($path_parts['extension'] === 'svg' || $path_parts['extension'] === 'svgz')) {
         if (file_exists($_imgPath)) {
             $outputFormat = 'svg-xml';
             return $imgSrc;
         }
         $outputFormat = 'gif';
         return ICON_DIR . 'image.gif';
     }
     if (!file_exists($_imgPath) || !($imagesize = getimagesize($_imgPath))) {
         $imagesize = array(0, 0);
     }
     if ($imagesize[0] > $width || $imagesize[1] > $height) {
         $_previewDir = WE_THUMB_PREVIEW_PATH;
         if (!file_exists($_previewDir) || !is_dir($_previewDir)) {
             we_base_file::createLocalFolder($_previewDir);
         }
         $_thumbSrc = $imgID ? WE_THUMB_PREVIEW_DIR . $imgID . '_' . $width . '_' . $height . strtolower($outputFormat) : TEMP_DIR . ($tmpName ?: we_base_file::getUniqueId()) . '.' . strtolower($outputFormat);
         $_thumbPath = WEBEDITION_PATH . '../' . $_thumbSrc;
         $_thumbExists = file_exists($_thumbPath);
         $_imageCreationDate = filemtime($_imgPath);
         $_thumbCreationDate = $_thumbExists ? filemtime($_thumbPath) : 0;
         if (!$_thumbExists || $_imageCreationDate > $_thumbCreationDate) {
             self::edit_image($_imgPath, $outputFormat, $_thumbPath, $outputQuality, $width, $height);
         }
         return $_thumbSrc;
     }
     return $imgSrc;
 }