Custom file upload name with PHP code

Resize uploaded images and handle special characters in file names with ChronoForms.

Overview

The issue occurs when using custom file names with special characters, as ImageMagick may fail to process them during resizing.
First, configure the upload action with a safe custom file name. Then, use a PHP action to rename the file to remove special characters before resizing it with ImageMagick commands.

Answered
ChronoForms v6
Elita- Elita- 01 Jan, 2021
To anybody who needs to resize uploaded file, here is the solution:

1) Install ImageMagic and php_imagick;
2) enable imagic extension in php.ini - extension=imagick.so;
3) create upload action, disable auto upload; fill in custom fies config; define custom file name, if you prefer, eg.
{data:field_name}_{date:YmdHis}.{var:upload_action_name.file.extension}
add desired path for file upload - {path:root}/uploads/foldername
4) add the following PHP code in On Sucess field of File upload:

$dir = "/var/www/............/uploads/foldername";
$dir = "foldername";
$output_dir = $dir . "/";
$output_dir_ = "/var/www/................./uploads/foldername/";

$fileName = $this->data['field_name'];
$name = $fileName;

move_uploaded_file( $_FILES[ "foldername" ][ "tmp_name" ], $output_dir_ . $name );

chmod( $output_dir_ . $name, 0777 );

list( $width, $height, $type, $attr ) = getimagesize( $output_dir_ . $name );
if ( $width > 1050 || $height > 600 ) {
exec( "mogrify -resize 1050x600 " . $output_dir_ . $name );
}

Max_admin Max_admin 03 Jan, 2021
Answer
1 Likes
You could just use the code below:
$path = $this->get("upload_action_name.filefield_name.path");
list( $width, $height, $type, $attr ) = getimagesize($path);
if ( $width > 1050 || $height > 600 ) {
exec( "mogrify -resize 1050x600 " . $path );
}
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- Elita- 03 Jan, 2021
Thanks, Max, I will make my code cleaner with your advice.

NB! one should take in mind that this does not work with special characters and spaces, etc...
for now I changed the upload file name to the safiest possible - {date:YmdHis.u}.{var:upload_action.file.extension}

However I would like to have it like {data:name}_{data:surname}.{var:upload_action.file.extension} where Name and Surname is Persons name that may contain special Latin characters for Latvian like āēīķļ. ImageMagick does not support this.
Even built-in file upload name (where all the special characters have been striped out) you wrote, Max, does not execute file resize action.

Max, could you, please give some advice?
How can I load file name in that PHP action, clean it and THEN resize?

Looking forward to this being integrated in CF7😉
Max_admin Max_admin 08 Jan, 2021
Just add a PHP action to the next page "Actions" then get the file path and "move" it to a new name, all with PHP, the file path value is used in my code above!
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.