18

こんにちは、私は実際に base64 イメージ文字列を ajax 経由で php スクリプトに送信しています。このスクリプトは文字列をデコードし、コンテンツを .jpg ファイルとして保存します。

しかし、結果は空の画像です。

これはどのように可能ですか?

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);


            }

ajax の場合、echo $photoTemp が文字列を返すため、問題ありません。

私は試しvar_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));てみましたが、画像は保存されているので返されますbool(true)が、画像にはコンテンツがありません:(空の画像

空の画像の場合、ファイルが作成されて名前が付けられ、phpに渡すコンテンツと同じサイズですが、その画像を開いてプレビューしようとすると、破損または不良ファイルのためファイルを開くことができませんタイプフォーマット

とにかく、これは base64 として写真を撮り、それを php に送信する JS です。

<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>
4

8 に答える 8

32

私の知る限り、画像関数 imagecreatefromstring、imagejpeg を使用して画像を作成する必要があります。

$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);

これが役立つことを願っています。

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);

したがって、以下はプログラムのphp部分です..NOTEコメント付きの変更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**

      }
    }
于 2013-07-23T13:07:05.257 に答える
1

画像をデコードして 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();
于 2014-01-24T09:46:46.050 に答える
0

$base64 を有効な base64 として使用し、有効なカンマ区切りの画像ヘッダーを使用:

$parts = explode(',',$base64,2);
$image = imagecreatefromstring($parts[1]);
imagejpeg($image,$filename);

そのような単純な。

于 2021-06-19T02:36:24.213 に答える