Skip to content

Commit

Permalink
Merge pull request #15 from KLXM/skerbis-patch-1
Browse files Browse the repository at this point in the history
Update api_filepond.php
  • Loading branch information
skerbis authored Dec 23, 2024
2 parents b652113 + 94f457e commit 4c8b67b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lang/de_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lang/en_gb.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 105 additions & 1 deletion lib/api/api_filepond.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand All @@ -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
]
];

Expand All @@ -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', ''));
Expand Down
5 changes: 3 additions & 2 deletions package.yml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down
15 changes: 13 additions & 2 deletions pages/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -74,8 +83,10 @@
$form->addRawField('<div class="col-sm-6">');

// Erlaubte Dateitypen
$field = $form->addInputField('text', 'allowed_types', null, [
'class' => 'form-control'
$field = $form->addTextAreaField('allowed_types', null, [
'class' => 'form-control',
'rows' => '5',
'style' => 'font-family: monospace;'
]);
$field->setLabel($addon->i18n('filepond_settings_allowed_types'));
$field->setNotice($addon->i18n('filepond_settings_allowed_types_notice'));
Expand Down

0 comments on commit 4c8b67b

Please sign in to comment.