I am trying to implement a custom image upload with a cropping feature. The Upload will happen in a frontend form (acf_form). The cropping plugin i use is croppie.js. What i did:
everything works as expected but now im struggling to pass the cropped image data to my acf_image field to let it upload into the medialibrary and map to that post.
the croppie plugin gives back a base64 imagedata.
i tryed to append the data to the acf_field inputs
<label class="acf-basic-uploader"> <input type="file" name="acf[field_606445890ea6f]" id="acf-field_606445890ea6f"> </label>
aswell as on this input the with jquery .val(resp)
<div class="acf-image-uploader" data-preview_size="thumbnail" data-library="all" data-mime_types="" data-uploader="basic"> <input type="hidden" name="acf[field_606445890ea6f]" value=""> <div class="show-if-value image-wrap" style="max-width: 150px"> </div>
but still it does not upload the image.
Does anyone have an idea how i can achieve this?
The return options of the cropped image can be:
‘base64’ returns a the cropped image encoded in base64
‘html’ returns html of the image positioned within an div of hidden overflow
‘blob’ returns a blob of the cropped image
‘rawcanvas’ returns the canvas element allowing you to manipulate prior to getting the resulted image
When i use the image field directly it gives me this as value of the input
when i give back my base64 data this will be overwritten
I don’t know how you can do this.
An ACF image field only stores the image ID and there isn’t any way to have ACF store additional data for the image.
1) Store the generated value in a hidden field
2) Create an acf/save_post filter that gets the hidden value and makes any needed changes to the attachment post for the image or its meta values that you’ll need for displaying this image. Maybe you need to somehow insert the image into the media library and alter the image ID stored by ACF. I am not familiar with how you can do this with the data that will be provided. You might potentially start here https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html
Thanks John for this approach. i did it similiar i just save the base64 enc. image to an ACF Textfield and use this as img src where i need it.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2021 Advanced Custom Fields. Subscribe