web-dev-qa-db-fra.com

PHP - récupère la chaîne d'image base64, décode et sauvegarde au format jpg (image vide résultante)

salut, je suis en train d’envoyer une image base64 via ajax à un script php qui décode simplement la chaîne et enregistre le contenu sous forme de fichier .jpg.

Mais le résultat est une image vide.

Comment cela est-il possible?

script php:

$uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4');
            foreach ($uploadedPhotos as $file) {
                if($this->input->post('photo_1')){
                     $photoTemp = base64_decode($this->input->post('photo_1'));


                    /*Set name of the photo for show in the form*/
                    $this->session->set_userdata('upload_'.$file,'ant');
                    /*set time of the upload*/
                    if(!$this->session->userdata('uploading_on_datetime')){
                     $this->session->set_userdata('uploading_on_datetime',time());
                    }
                     $datetime_upload = $this->session->userdata('uploading_on_datetime',true);
                    /*create temp dir with time and user id*/
                    $new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/';
                    @mkdir($new_dir);
                    /*move uploaded file with new name*/
                    @file_put_contents( $new_dir.$file.'.jpg',$photoTemp);


            }

Pour ajax c'est correct car echo $ photoTemp renvoie la chaîne.

j'ai essayé var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp)); et il retourne bool(true) puisque l'image est enregistrée mais qu'il n'y a pas de contenu dans l'image :( image vide

pour l'image vide je veux dire, le fichier est créé et nommé, et il a le même taille du contenu je passe à php, mais quand j'essaye d'ouvrir cette image pour prévisualiser, le fichier ne peut pas être ouvert car le format du type de fichier est corrompu ou incorrect 

de toute façon c'est le JS qui prend une photo en base64 et l'envoie à php:

<script type="text/javascript">

var _min_width = 470;
var _min_height = 330;
var _which;
var _fyle_type;
var  io;
var allowed_types = new Array('image/png','image/jpg','image/jpeg');
if (typeof(FileReader) === 'function'){
$('input[type="file"]').on('change', function(e) {
    var _file_name = $(this).val();
    $('.'+_which+'_holder').text(_file_name);
    var file = e.target.files[0];

    if (!in_array(file.type,allowed_types) || file.length === 0){
        notify("You must select a valid image file!",false,false); 
        return;
    }

    if(file.size > 3145728 /*3MB*/){
        notify("<?php echo lang('each-photo-1MB'); ?>",false,false); 
        return;
    }
    notify_destroy();

    var reader = new FileReader();
    reader.onload = fileOnload;
  reader.readAsDataURL(file);


});

function fileOnload(e) {
    var img = document.createElement('img');
    img.src = e.target.result;

    img.addEventListener('load', function() {
        if(img.width < _min_width || img.height < _min_height ){
        notify("<?php echo lang('each-photo-1MB'); ?>",false,false); 
        return;
        }


        $.ajax({
            type:'post',
            dataType:'script',
            data:{photo_1:e.target.result},
            url:_config_base_url+'/upload/upload_photos',
            progress:function(e){
                console.log(e);
            },
            success:function(d){
                $('body').append('<img src="'+d+'"/>');
            }
         });


    });

}
}
</script>
13
bombastic

Si j'ai bien compris, vous devez utiliser la fonction image imagecreatefromstring, imagejpeg pour créer les images.

$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);

J'espère que cela aidera.

PHP CODE WITH IMAGE DATA

$imageDataEncoded = base64_encode(file_get_contents('sample.png'));
$imageData = base64_decode($imageDataEncoded);
$source = imagecreatefromstring($imageData);
$angle = 90;
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageName = "hello1.png";
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);

Donc, voici la partie php de votre programme .. NOTE le changement avec le commentaire Change is here

    $uploadedPhotos = array('photo_1','photo_2','photo_3','photo_4');
     foreach ($uploadedPhotos as $file) {
      if($this->input->post($file)){                   
         $imageData = base64_decode($this->input->post($file)); // <-- **Change is here for variable name only**
         $photo = imagecreatefromstring($imageData); // <-- **Change is here**

        /* Set name of the photo for show in the form */
        $this->session->set_userdata('upload_'.$file,'ant');
        /*set time of the upload*/
        if(!$this->session->userdata('uploading_on_datetime')){
         $this->session->set_userdata('uploading_on_datetime',time());
        }
         $datetime_upload = $this->session->userdata('uploading_on_datetime',true);

        /* create temp dir with time and user id */
        $new_dir = 'temp/user_'.$this->session->userdata('user_id',true).'_on_'.$datetime_upload.'/';
        if(!is_dir($new_dir)){
        @mkdir($new_dir);
        }
        /* move uploaded file with new name */
        // @file_put_contents( $new_dir.$file.'.jpg',imagejpeg($photo));
        imagejpeg($photo,$new_dir.$file.'.jpg',100); // <-- **Change is here**

      }
    }
26
Yogesh
$data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAADICAYAAAAQj4UaAAAcNUlEQVR4nO3dwW4cR37H8d8jzBsMXyACX4Aw7yawPBhIsDqs3kAEcolPGuQinbI6BMneLCRZ57AwJARLW8hhRcU8xA6QoaVsHGC9kVZrIVl7LYkJVootyf8cukcckjNV1T3VU1Xd3w/wBwSSmq4ecrrr3/WvKgkAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgKhsLNkHdYxTtwYAAABAb9mWZC8lszq+JQkBAAAA0AHblOz5XPIxi9upWwYAAACgV+wdyV4tSD6sTkpGqVsIAAAAoBfsnSWJx3xcS91KAAAAAMWzTcleByQgr5gLAgAAAGAFtiXZ/wUkH7M4TN1iAAAAAEWycb3CVWjyMYvt1C0HAAAAUBz7tEXyYZJNU7ccAAAAQFFsu2XyMYt3U58BAAAAgGLY0YoJyGsmpAMAAAAIYO86EotfSvZ9YBJyM/WZAAAAAMiajT1L7l70JCjz8cWa2z6S7HIdbIoIAAAA5M9uhq1wFVqitbZ2j6rJ7/MT4UlCAAAAgMzZbU9SsVH/3HbgKMjumtq9t+DYe+s5NgAAAICW7DB8RCNomd7rHbd3VnZ1jwQEAAAAKI4dOJKJZ2d+disgAelwT5BzZVdnY7O7YwMAAACIwJ45OvQHC34+pAyro7kYC8uuZnHczTEBAAAARORMJA4X/PxBQAKy3VFbbzmOeb+bYwIA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAhuv/ATiSIPBj5ipCAAAAAElFTkSuQmCC';

$data = str_replace('data:image/png;base64,', '', $data);

$data = str_replace(' ', '+', $data);

$data = base64_decode($data);

$file = 'images/'.Rand() . '.png';

$success = file_put_contents($file, $data);

$data = base64_decode($data); 

$source_img = imagecreatefromstring($data);

$rotated_img = imagerotate($source_img, 90, 0); 

$file = 'images/'. Rand(). '.png';

$imageSave = imagejpeg($rotated_img, $file, 10);

imagedestroy($source_img);
9
naresh

Voici ce qui a finalement fonctionné pour moi. Vous devrez convertir le code pour répondre à vos propres besoins, mais cela le fera.

$fname = filter_input(INPUT_POST, "name");
$img = filter_input(INPUT_POST, "image");
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$img = base64_decode($img);

file_put_contents($fname, $img);

print "Image has been saved!";
6
Justin Eldracher

Décoder et enregistrer l'image au format PNG

header('content-type: image/png');

           ob_start();

        $ret = fopen($fullurl, 'r', true, $context);
        $contents = stream_get_contents($ret);
        $base64 = 'data:image/PNG;base64,' . base64_encode($contents);
        echo "<img src=$base64 />" ;


        ob_end_flush();
1
milind Morey

Une simplification mineure sur l'exemple de @naresh. Devrait traiter des problèmes de permission et offrir des éclaircissements.

$data = '<base64_encoded_string>';

$data = base64_decode($data);

$img = imagecreatefromstring($data);

header('Content-Type: image/png');

$file = '<path_to_home_or_user_directory>/decoded_images/test.png';

imagepng($img, $file);

imagedestroy($img);
0
Deci

Le client doit envoyer base64 au serveur.

Et au-dessus de la réponse, le code décrit fonctionne parfaitement:

$imageData = base64_decode($imageData);
$source = imagecreatefromstring($imageData);
$rotate = imagerotate($source, $angle, 0); // if want to rotate the image
$imageSave = imagejpeg($rotate,$imageName,100);
imagedestroy($source);

Merci

0
Dhiraj