Problem with imagecreatefromstring from Base64 Php

0

I have a problem with the imagecreatefromstring function in Php.

1- I have a file input in the front-end (html) of my web page, where the user selects a photo.

2- When the user selects an image, a javascript function executes a routine that creates a base64 string, which passes to the back-end (php file on the server).

3- A function in php takes that base64 string and converts it back to image with the function imagecreatefromstring ().

And here is the problem, when the user loads the image from a device (for example, a Motorola cell phone), the conversion, the past of the chain in base64 and the conversion of the chain into image again works well . But when users load the image from another device (for example, another cell phone like a Samsung J7), the process is cut at some point (I do not know where exactly) and the final image file is saved in half ... as a cut image

I have been working with this problem for a few days now and I need urgent help.

Here I publish the javascript conversion function:

function ResizeImage(imageFile, Elemento) {
    var filesToUpload = imageFile.files;
    var file = filesToUpload[0];
    var form = document.getElementById("ppal_form");

    // Create an image
    var img = document.createElement("img");
    // Create a file reader
    var reader = new FileReader();
    // Set the image once loaded into file reader

    reader.onload = function(e) {
            img.src = e.target.result;

            var canvas = document.createElement("canvas");
            //var canvas = $("<canvas>", {"id":"testing"})[0];
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0);

            var MAX_WIDTH = img.width / 4;
            var MAX_HEIGHT = img.height / 4;
            var width = img.width;
            var height = img.height;

            if (width > height) {
                if (width > MAX_WIDTH) {
                    height *= MAX_WIDTH / width;
                    width = MAX_WIDTH;
                }
            } else {
                if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                }
            }
            canvas.width = width;
            canvas.height = height;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, width, height);

            var dataurl = canvas.toDataURL("image/png",0.5);
            console.log(dataurl);

            auxinput = $('#aux_files1');

            auxinput.attr('value', dataurl);

            imageFile.value = "";
        }
    // Load files into file reader
    reader.readAsDataURL(file);

    //alert('se ejecuto funcion ResizeImage');
}

And the Php function that converts the string in base64 to image again:

public static function base64_to_jpeg($base64_string, $output_file) {
    // open the output file for writing
    $ifp = fopen( $output_file, 'wb' ); 

    // split the string on commas
    // $data[ 0 ] == "data:image/png;base64"
    // $data[ 1 ] == <actual base64 string>
    $data = explode( ',', $base64_string );

    // we could add validation here with ensuring count( $data ) > 1
    fwrite( $ifp, base64_decode( $data[ 1 ] ) );

    // clean up the file resource
    fclose( $ifp ); 

    return $output_file; 
}

Thank you very much !!

    
asked by Alejandro Zbrun 12.07.2018 в 18:43
source

0 answers