Support

Account

Home Forums ACF PRO Convert uploaded image to base64 value Reply To: Convert uploaded image to base64 value

  • Thanks again John,

    This works great for me:

    add_action('acf/save_post', 'convert_to_base64');
    function convert_to_base64( $post_id ) {
    
    	// Get newly saved values.
    	$values = get_fields( $post_id );
    
    	// Check the new value of a specific field.
    	$icon_set = get_field('icon', $post_id);
    	$icon_output = get_field('icon_base64', $post_id);
    	$icon_default = 'No icon';
    	
    	if( $icon_set ) {
    		$image = $icon_set;
    		$finfo = new finfo(FILEINFO_MIME_TYPE);
    		$type  = $finfo->buffer($image);
    		$base64 = "data:".$type.";charset=utf-8;base64,".base64_encode(file_get_contents($image));
    		update_field('icon_base64', $base64, $post_id);
    	} else {
    		update_field('icon_base64', $icon_default, $post_id);
    	}
    
    }

    I want the user to be able to see the Base64 output but not interact with it. So have added some backend CSS styles to the output field to disable the cursor and show the field at 50% opacity.

    Thanks again for your help. Will mark as solved 👍