Support » Fixing WordPress » Half of image uploaded

  • Hey,

    I have built a custom plugin which lets end-users draw on a canvas in HTML5 and upload images to our server. But our large images, whose size is >= 730 KB comes cut off and sometimes admin-ajax returns as 404 for those images.

    We are pushing image as Base64 data in a ajax call:

    var itemImage = canvas.toDataURL();
        $.ajax({
                url: wc_variation_add_to_cart.ajax_url.toString(),
                type: 'POST',
                data: {
                    'action': 'upload_image',
                    'item-image': itemImage
                },
                success: function(response) {
                    if (response !== '0') {
                        var result = JSON.parse(response);
                        data['item-image-url'] = result['url'];
    
                        add_to_cart(data, $thisbutton);
                    } else {
                        alert('Sorry! unable to upload image to server');
                    }
                },
                error: function(response) {
                    alert('Sorry! Unable to upload image to server');
                    console.log(response.result);
                }
            });

    to a custom method:

    $upload_dir = wp_upload_dir();
            $upload_path = str_replace('/', DIRECTORY_SEPARATOR, $upload_dir['path']) . DIRECTORY_SEPARATOR;
            $imageData = $_POST['item-image'];
            $decoded = base64_decode(explode(',', $imageData)[1]);
            $filename = rand(1, 100000) . '.png';
            $hashed_filename = md5($filename . microtime()) . '_' . $filename;
    
            // Store image on server
            file_put_contents($upload_path . $hashed_filename, $decoded);
    
            //HANDLE UPLOADED FILE
            if (!function_exists('wp_handle_sideload')) {
                require_once(ABSPATH . 'wp-admin/includes/file.php');
            }
    
            if (!function_exists('wp_get_current_user')) {
                require_once(ABSPATH . 'wp-includes/pluggable.php');
            }
    
            $file = array();
            $file['error'] = '';
            $file['tmp_name'] = $upload_path . $hashed_filename;
            $file['name'] = $hashed_filename;
            $file['type'] = 'image/png';
            $file['size'] = filesize($upload_path . $hashed_filename);
    
            // upload file to server
            $file_return = wp_handle_sideload($file, array('test_form' => false));
            echo json_encode(array('url' => $file_return['url']));

    At first i thought that error must be because of PHP configuration as the plugin resides on a shared-server. But configuration looks following:

    post_max_size = 24M
      upload_max_filesize = 128M
      max_file_uploads = 20

    So, the configuration is not a problem (or maybe it is?). Second thought was RAM on a shared server. But the server contains more that 68GB of RAM with literally only us on the server (via htop).

    I am unable to figure out the reason behind this and therefore wonder if any of you have gone through this and know what could be the problem?

    Thanks in advance

  • The topic ‘Half of image uploaded’ is closed to new replies.