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:
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 );
}
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 );
}
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😉
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😉
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!
You need to login to be able to post a reply.
