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];
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * loads the binary image data
  *
  * @return void
  * @private
  */
 private function getBinaryData()
 {
     $this->imageData = we_base_file::load(WEBEDITION_PATH . '../' . $this->imagePath);
 }
 public static function edit_image($imagedata, $output_format = "jpg", $output_filename = "", $output_quality = 75, $width = "", $height = "", $keep_aspect_ratio = true, $interlace = true, $crop_x = 0, $crop_y = 0, $crop_width = -1, $crop_height = -1, $rotate_angle = 0, $fitinside = false)
 {
     $output_format = strtolower($output_format);
     if ($output_format === 'jpeg') {
         $output_format = "jpg";
     }
     $_fromFile = strlen($imagedata) < 255 && @file_exists($imagedata);
     // Output format is available
     if (in_array($output_format, self::supported_image_types())) {
         // Set quality for JPG images
         if ($output_format === 'jpg') {
             // Keep quality between 1 and 99
             $output_quality = max(1, min(99, is_int($output_quality) ? $output_quality : 75));
         }
         $_gdimg = $_fromFile ? self::ImageCreateFromFileReplacement($imagedata) : self::ImageCreateFromStringReplacement($imagedata);
         // Now we need to ensure that we could read the file
         if ($_gdimg) {
             // Detect dimension of image
             $_width = ImageSX($_gdimg);
             $_height = ImageSY($_gdimg);
             if ($rotate_angle != 0 && function_exists("ImageRotate")) {
                 $rotate_angle = floatval($rotate_angle);
                 while ($rotate_angle < 0) {
                     $rotate_angle += 360;
                 }
                 $rotate_angle = $rotate_angle % 360;
                 if ($rotate_angle != 0) {
                     $_gdimg = ImageRotate($_gdimg, $rotate_angle, 0);
                     $_width = ImageSX($_gdimg);
                     $_height = ImageSY($_gdimg);
                 }
             }
             $_outsize = self::calculate_image_size($_width, $_height, $width, $height, $keep_aspect_ratio, true, $fitinside);
             // Decide, which functions to use (depends on version of GD library)
             $_image_create_function = self::gd_version() >= 2.0 ? "imagecreatetruecolor" : "imagecreate";
             $_image_resize_function = function_exists('imagecopyresampled') ? "imagecopyresampled" : "imagecopyresized";
             $_outsize["width"] = max(1, $_outsize["width"]);
             $_outsize["height"] = max(1, $_outsize["height"]);
             // Now create the image
             $_output_gdimg = $_image_create_function($_outsize["width"], $_outsize["height"]);
             // this image is always black
             /* $GDInfo = self::gd_info();
             		  // DEBIAN EDGE FIX => crashes at imagefill, so use old Method
             		  if($GDInfo["GD Version"] === '2.0 or higher' && !function_exists("imagerotate")){
             		  // set black to transparent!
             		  if($output_format === 'gif' || $output_format === 'png'){ // transparency with gifs
             		  imagecolortransparent($_output_gdimg, imagecolorallocate($_output_gdimg, 0, 0, 0)); // set this color to transparent - done
             		  }
             		  } else {
             		 */
             // preserve transparency of png and gif images:
             switch ($output_format) {
                 case "gif":
                     $colorTransparent = imagecolortransparent($_gdimg);
                     imagepalettecopy($_gdimg, $_output_gdimg);
                     imagefill($_output_gdimg, 0, 0, $colorTransparent);
                     imagecolortransparent($_output_gdimg, $colorTransparent);
                     imagetruecolortopalette($_output_gdimg, true, 256);
                     break;
                 case "png":
                     imagealphablending($_output_gdimg, false);
                     //$transparent = imagecolorallocatealpha($_output_gdimg, 0, 0, 0, 127);
                     $transparent = imagecolorallocatealpha($_output_gdimg, 255, 255, 255, 127);
                     imagefill($_output_gdimg, 0, 0, $transparent);
                     imagesavealpha($_output_gdimg, true);
                     break;
                 default:
             }
             //}
             // Resize image
             //if($_outsize["width"] == "1")
             if ($fitinside && $keep_aspect_ratio && $width && $height) {
                 $wratio = $width / $_width;
                 $hratio = $height / $_height;
                 $ratio = max($width / $_width, $height / $_height);
                 $h = $height / $ratio;
                 $w = $width / $ratio;
                 if ($wratio < $hratio) {
                     $x = ($_width - $width / $ratio) / 2;
                     $y = 0;
                 } else {
                     $x = 0;
                     $y = ($_height - $height / $ratio) / 2;
                 }
                 // Set thumbnail focus point -ah2015
                 if (true) {
                     echo '<script>console.log("TX:"+' . $x . ', "TY:"+' . $y . ', "OWidth:"+' . $_width . ', "TWidth"+' . $width . ', "OHeight:"+' . $_height . ', "THeight"+' . $height . ');</script>';
                     $x_focus = $crop_x;
                     // von -1.0 bis 1.0
                     $y_focus = $crop_y;
                     // von -1.0 bis 1.0
                     $x = $x + $x * $x_focus;
                     $y = $y + $y * $y_focus;
                 }
                 $_image_resize_function($_output_gdimg, $_gdimg, 0, 0, $x, $y, $width, $height, $w, $h);
             } else {
                 $_image_resize_function($_output_gdimg, $_gdimg, 0, 0, 0, 0, $_outsize["width"], $_outsize["height"], $_width, $_height);
             }
             // PHP 4.4.1 GDLIB-Bug/Safemode - Workarround
             if ($output_filename != "" && file_exists($output_filename)) {
                 touch($output_filename);
             }
             ImageInterlace($_output_gdimg, $interlace ? 1 : 0);
             switch ($output_format) {
                 case 'jpg':
                     // Output to a filename or directly
                     if ($output_filename != "") {
                         $_gdimg = imagejpeg($_output_gdimg, $output_filename, $output_quality);
                         if ($_gdimg) {
                             $_gdimg = basename($output_filename);
                         }
                     } elseif ($_tempfilename = tempnam(TEMP_PATH, "")) {
                         imagejpeg($_output_gdimg, $_tempfilename, $output_quality);
                         $_gdimg = we_base_file::load($_tempfilename);
                         // As we read the temporary file we no longer need it
                         //unlink($_tempfilename);
                     }
                     break;
                 case 'png':
                 case 'gif':
                     // Set output function
                     $_image_out_function = 'image' . $output_format;
                     // Output to a filename or directly
                     if ($output_filename) {
                         $_gdimg = $_image_out_function($_output_gdimg, $output_filename);
                         if ($_gdimg) {
                             $_gdimg = basename($output_filename);
                         }
                     } elseif ($_tempfilename = tempnam(TEMP_PATH, "")) {
                         $_image_out_function($_output_gdimg, $_tempfilename);
                         $_gdimg = we_base_file::load($_tempfilename);
                         // As we read the temporary file we no longer need it
                         unlink($_tempfilename);
                     }
                     break;
             }
             ImageDestroy($_output_gdimg);
         }
         return isset($_gdimg) ? array($_gdimg, $_outsize["width"], $_outsize["height"]) : array(false, -1, -1);
     }
     return array(false, -1, -1);
 }