custom post types – Showing a converted image in orders page

Question

I am trying to take a snapshot of an image with the webcam, upload it to WordPress and show it with the orders details

So I took a base64 image as a test and the upload process of that image is working fine, now I only need to show it with the details of the order so it will be like that

<img src="https://mywebsite.com/wp-content/uploads/2021/07/'. esc_html($title) .'.jpeg">

$title is the file name uploaded

I tried it with esc_html and echo but couldn’t get it to work

and google said that I need to echo the link for it to work in the src image

A solution, please?

this is my code


/**
 * Save the image on the server.
 */
function save_image( $base64_img, $title ) {

    // Upload dir.
    $upload_dir  = wp_upload_dir();
    $upload_path = str_replace( "https://wordpress.stackexchange.com/", DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;

    $img             = str_replace( 'data:image/jpeg;base64,', '', $base64_img );
    $img             = str_replace( ' ', '+', $img );
    $decoded         = base64_decode( $img );
    $filename        = $title . '.jpeg';
    $file_type="image/jpeg";
    $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;

    // Save the image in the uploads directory.
    $upload_file = file_put_contents( $upload_path . $hashed_filename, $decoded );

    $attachment = array(
        'post_mime_type' => $file_type,
        'post_title'     => preg_replace( '/.[^.]+$/', '', basename( $hashed_filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit',
        'guid'           => $upload_dir['url'] . "https://wordpress.stackexchange.com/" . basename( $hashed_filename )
    );

    $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . "https://wordpress.stackexchange.com/" . $hashed_filename );
}




/**
 * Add order item meta.
 */

add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);

function add_order_item_meta ( $item_id, $values ) {

    if ( isset( $values [ 'custom_data' ] ) ) {



//example of an image
        $custom_data  = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/I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zVYU+QFeEGPIKfYU8AETkCYI6wANkZQwIKRNDB1hA0Jrf0rk6WXZ8BvWkb4kv99vn89kDrfVexBSYgVNwDA7AN+jAK3gEd+AlGMGIBFDgFvzouK3JV/lihQTOwLtOtw9wIRG5pJn91Tbgqk9kSk7GViADrTD4HCyZ0NQnomi51sb0fUyCMQEbp2WpU67IjfNjwcYyoUDhjJVcZBjYBy40j4wXgaobWoe8Z6Y80CJBwFpunepIzt2AUgFjtXXshNXjVmMh+K+zzp/CMs0CqeuzrxSRpbOKfdCkiMTS1VBQ41uxMyQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=
";

            

        
        save_image( $custom_data, $title );
        $link="e"
        
        $ima="<div class="product-image">
                <img src="https://polariva.com/test/wp-content/uploads/2021/07/". esc_html($title) .'.jpeg">
            </div>';

        wc_add_order_item_meta( $item_id, 'Image', $ima );
        
        
    }
}



0
Houssem Ghozzi 5 months 2021-07-05T10:26:52-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse