Index: branches/5.2.x/core/units/helpers/upload_helper.php =================================================================== diff -u -N -r16561 -r16596 --- branches/5.2.x/core/units/helpers/upload_helper.php (.../upload_helper.php) (revision 16561) +++ branches/5.2.x/core/units/helpers/upload_helper.php (.../upload_helper.php) (revision 16596) @@ -16,12 +16,21 @@ { /** + * File helper reference + * + * @var FileHelper + */ + protected $fileHelper = null; + + /** * Creates kUploadHelper instance. */ public function __construct() { parent::__construct(); + $this->fileHelper = $this->Application->recallObject('FileHelper'); + // 5 minutes execution time @set_time_limit(5 * 60); } @@ -77,23 +86,13 @@ throw new kUploaderException('Write permissions not set on the server, please contact server administrator.', 500); } - /** @var FileHelper $file_helper */ - $file_helper = $this->Application->recallObject('FileHelper'); - $filename = $file_helper->ensureUniqueFilename($tmp_path, $filename); + $filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $filename); $storage_format = $this->getStorageFormat($this->Application->GetVar('field'), $event); + $this->moveUploadedFile($tmp_path . $filename); if ( $storage_format ) { - /** @var ImageHelper $image_helper */ - $image_helper = $this->Application->recallObject('ImageHelper'); - - $this->moveUploadedFile($value['tmp_name'] . '.jpg'); // add extension, so ResizeImage can work - $url = $image_helper->ResizeImage($value['tmp_name'] . '.jpg', $storage_format); - $tmp_name = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', '/', $url); - rename($tmp_name, $tmp_path . $filename); + $this->resizeUploadedFile($tmp_path . $filename, $storage_format); } - else { - $this->moveUploadedFile($tmp_path . $filename); - } $this->deleteTempFiles($tmp_path); @@ -108,6 +107,33 @@ } /** + * Resizes uploaded file. + * + * @param string $file_path File path. + * @param string $format Format. + * + * @return boolean + */ + public function resizeUploadedFile($file_path, $format) + { + /** @var ImageHelper $image_helper */ + $image_helper = $this->Application->recallObject('ImageHelper'); + + // Add extension, so that "ImageHelper::ResizeImage" can work. + $resize_file_path = tempnam(sys_get_temp_dir(), 'uploaded_') . '.jpg'; + + if ( rename($file_path, $resize_file_path) === false ) { + return false; + } + + $resized_file_path = $this->fileHelper->urlToPath( + $image_helper->ResizeImage($resize_file_path, $format) + ); + + return rename($resized_file_path, $file_path); + } + + /** * Sends headers to ensure, that response is never cached. * * @return void