diff --git a/lang/de_de.lang b/lang/de_de.lang index 74084d5..8a2493c 100644 --- a/lang/de_de.lang +++ b/lang/de_de.lang @@ -27,6 +27,8 @@ filepond_settings_category_id = Standard Medien-Kategorie filepond_settings_category_notice = ID der Medienkategorie in die die Dateien geladen werden sollen filepond_settings_lang = Standardsprache filepond_settings_lang_notice = Beispiel: en_gb, de_de +filepond_settings_max_pixel = Maximale Bildgröße +filepond_settings_max_pixel_notice = Maximale Breite/Höhe in Pixeln, auf die große Bilder verkleinert werden. GIF-Dateien werden nicht verändert. # YForm field filepond_empty_value_label = Fehlermeldung wenn leer diff --git a/lang/en_gb.lang b/lang/en_gb.lang index 3eca3fb..40518bc 100644 --- a/lang/en_gb.lang +++ b/lang/en_gb.lang @@ -24,6 +24,8 @@ filepond_settings_category_id = Default media category filepond_settings_category_notice = ID of the media category where files should be uploaded to filepond_settings_lang = Default language filepond_settings_lang_notice = Example: en_gb, de_de +filepond_settings_max_pixel = Maximum image size +filepond_settings_max_pixel_notice = Maximum width/height in pixels to which large images will be resized. GIF files will remain unchanged. # YForm field filepond_empty_value_label = Error message if empty diff --git a/lib/api/api_filepond.php b/lib/api/api_filepond.php index 042b7b3..d9b73d9 100644 --- a/lib/api/api_filepond.php +++ b/lib/api/api_filepond.php @@ -70,6 +70,7 @@ protected function handleUpload($categoryId) } $file = $_FILES['filepond']; + // error_log('FILEPOND: Uploaded file type: ' . $file['type'] . ', name: ' . $file['name']); $maxSize = rex_config::get('filepond_uploader', 'max_filesize', 10) * 1024 * 1024; if ($file['size'] > $maxSize) { @@ -103,6 +104,14 @@ protected function handleUpload($categoryId) if (!$isAllowed) { throw new rex_api_exception('File type not allowed'); } + + // Process image if it's not a GIF + if (strpos($file['type'], 'image/') === 0 && $file['type'] !== 'image/gif') { + // error_log('FILEPOND: Starting image processing for: ' . $file['type'] . ' - ' . $file['name']); + $this->processImage($file['tmp_name']); + } else { + // error_log('FILEPOND: Skipping image processing - file type: ' . $file['type']); + } $originalName = $file['name']; $filename = rex_string::normalize(pathinfo($originalName, PATHINFO_FILENAME)); @@ -120,7 +129,7 @@ protected function handleUpload($categoryId) 'name' => $originalName, 'tmp_name' => $file['tmp_name'], 'type' => $file['type'], - 'size' => $file['size'] + 'size' => filesize($file['tmp_name']) // Update filesize after potential resize ] ]; @@ -145,6 +154,101 @@ protected function handleUpload($categoryId) } } + protected function processImage($tmpFile) + { + // error_log('FILEPOND: Processing image: ' . $tmpFile); + + $imageInfo = getimagesize($tmpFile); + if (!$imageInfo) { + // error_log('FILEPOND: Could not get image size for file: ' . $tmpFile); + return; + } + + list($width, $height, $type) = $imageInfo; + // error_log("FILEPOND: Image dimensions: {$width}x{$height}, type: {$type}"); + + $maxPixel = rex_config::get('filepond_uploader', 'max_pixel', 1200); + + + // Return if image is smaller than max dimensions + if ($width <= $maxPixel && $height <= $maxPixel) { + // error_log('FILEPOND: Image is already small enough, skipping resize'); + return; + } + // error_log('FILEPOND: Image needs resizing'); + + // Calculate new dimensions + $ratio = $width / $height; + if ($width > $height) { + $newWidth = min($width, $maxPixel); + $newHeight = floor($newWidth / $ratio); + } else { + $newHeight = min($height, $maxPixel); + $newWidth = floor($newHeight * $ratio); + } + + // Create new image based on type + $srcImage = null; + switch ($type) { + case IMAGETYPE_JPEG: + // error_log('FILEPOND: Processing as JPEG'); + $srcImage = imagecreatefromjpeg($tmpFile); + break; + case IMAGETYPE_PNG: + // error_log('FILEPOND: Processing as PNG'); + $srcImage = imagecreatefrompng($tmpFile); + break; + default: + // error_log('FILEPOND: Unsupported image type: ' . $type); + return; + } + + if (!$srcImage) { + // error_log('FILEPOND: Could not create image resource'); + return; + } + + $dstImage = imagecreatetruecolor($newWidth, $newHeight); + // error_log("FILEPOND: Creating new image with dimensions: {$newWidth}x{$newHeight}"); + + // Preserve transparency for PNG images + if ($type === IMAGETYPE_PNG) { + imagealphablending($dstImage, false); + imagesavealpha($dstImage, true); + $transparent = imagecolorallocatealpha($dstImage, 255, 255, 255, 127); + imagefilledrectangle($dstImage, 0, 0, $newWidth, $newHeight, $transparent); + } + + // Resize image + imagecopyresampled( + $dstImage, + $srcImage, + 0, 0, 0, 0, + $newWidth, + $newHeight, + $width, + $height + ); + + // Save image + $success = false; + if ($type === IMAGETYPE_JPEG) { + $success = imagejpeg($dstImage, $tmpFile, 90); + } elseif ($type === IMAGETYPE_PNG) { + $success = imagepng($dstImage, $tmpFile, 9); + } + + /*if ($success) { + error_log('FILEPOND: Successfully saved resized image'); + } else { + error_log('FILEPOND: Failed to save resized image'); + }*/ + + // Free memory + imagedestroy($srcImage); + imagedestroy($dstImage); + } + protected function handleDelete() { $filename = trim(rex_request('filename', 'string', '')); diff --git a/package.yml b/package.yml index c040077..b1726f9 100644 --- a/package.yml +++ b/package.yml @@ -1,5 +1,5 @@ package: filepond_uploader -version: '1.0.1' +version: '1.1.0' author: 'Friends Of REDAXO' supportpage: github.com/FriendsOfREDAXO/filepond_uploader @@ -18,9 +18,10 @@ page: settings: { title: 'translate:filepond_uploader_settings' } default_config: - allowed_types: 'image/*,video/*,.pdf,.docx,.txt' + allowed_types: 'image/*,video/*,application/pdf' max_size: 200 max_files: 30 + max_pixel: 2100 category_id: 0 installer_ignore: diff --git a/pages/settings.php b/pages/settings.php index 4af33ac..402924a 100644 --- a/pages/settings.php +++ b/pages/settings.php @@ -58,6 +58,15 @@ $field->setLabel($addon->i18n('filepond_settings_maxsize')); $field->setNotice($addon->i18n('filepond_settings_maxsize_notice')); +// Maximale Pixelgröße +$field = $form->addInputField('number', 'max_pixel', null, [ + 'class' => 'form-control', + 'min' => '100', + 'required' => 'required' +]); +$field->setLabel($addon->i18n('filepond_settings_max_pixel')); +$field->setNotice($addon->i18n('filepond_settings_max_pixel_notice')); + // Sprache $field = $form->addSelectField('lang', null, [ 'class' => 'form-control selectpicker' @@ -74,8 +83,10 @@ $form->addRawField('