• Resolved chrisptacos84

    (@chrisptacos84)


    Hi there,

    I have a simple form for image upload with a submit button. This enables users to upload images taken with smartphones. However, modern smartphones make for large image files. I want to create a function to intervene prior to saving the image file in the wp-content/uploads folder which will use imagick or GD to resize and compress it. However, when attempting to get this to work, I cannot get the function to fire from the hook/filter. I think I am supposed to use the ‘forminator_custom_form_submit_field_data’ hook but I’m not sure. I also tried ‘wp_handle_upload’ and ‘wp_handle_upload_prefilter’ but those don’t work either. I have code below which should generate a simple output in the error log to test if its working. Maybe I am going about it wrong? I’m not too experienced with coding and stuff. Can please someone help?

    function compress_uploaded_image() {
    error_log(“compress_uploaded_image hook called.”);
    }
    add_filter(‘forminator_custom_form_submit_field_data’, ‘compress_uploaded_image’, 10, 4);

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @chrisptacos84

    I hope you are doing well.

    Had you considered the Smush plugin?

    https://ww.wp.xz.cn/plugins/wp-smushit/

    This will compress and have the option to resize the images as well setting the max size ( based on the WordPress filter )

    About the upload, can you let us know if you are using the Multiple or Single file upload field and if you are using the Upload with Ajax in case of multiple file upload?

    Best Regards
    Patrick Freitas

    Thread Starter chrisptacos84

    (@chrisptacos84)

    @wpmudevsupport12

    I considered it but I was looking for a something free. I am going to revisit that option but I think its free only up to 5MB sized files, and after that there is a subscription required. I’m not making any money off of the site so I want to keep costs down . This is for a single file upload field.

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @chrisptacos84

    The limit refers to the size of a single image file, not to the “amount of image data optimized in total”. In other words, you can optimize unlimited number of files that are up to 5MB each and that is not limited and doesn’t require any subscription.

    But I understand that sometimes images uploaded directly from mobile device may indeed be bigger than this so getting back to your code question then –

    You need to use this hook in a slightly different way. Here’s a working code example:

    add_filter( 'forminator_custom_form_submit_field_data', 'custom_compress_uploaded_image', 10, 2);
    function custom_compress_uploaded_image( $form_data, $form_id) {
    	
    	error_log('compress_uploaded_image hook called.');
    	
    	return $form_data;
    	
    }

    Note the changes please:

    – I changed number of arguments from 4 to 2 because filter supports only 2
    – I added parameters to callback function definition – as they need to be there
    – and I added line to return the data that is passed through filter – without it all you’ll get is a Fatal Error.

    So if you use this code, you will get the “compress uploaded image” message in error log and the hook will work.

    kind regards,
    Adam

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @chrisptacos84 ,

    We haven’t heard from you for over 2 weeks now, so it looks like you no longer need our assistance.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Forminator to Image Compression’ is closed to new replies.