PHP - get img base64 string zu decodieren und als jpg speichern (was leeres Bild )
Hallo, ich bin eigentlich senden, die eine base64-image, string trough ajax an ein php-script, das gerade dekodiert string und speichern Sie die Inhalte als .jpg-Datei.
Aber das Resultat ist ein leeres image.
Wie kann dies möglich sein?
php-Skript:
$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);
}
Für ajax ist es ok, weil echo $photoTemp gibt den string zurück.
versuchte ich var_dump(@file_put_contents( $new_dir.$file.'.jpg',$photoTemp));
und es gibt bool(true)
da wird das Bild gespeichert, aber es ist kein Inhalt in der Bild 🙁 leeres Bild
leere Bild meine ich , die Datei ist erstellt und benannt, und es hat die gleiche
Größe der Inhalte, die ich übergeben an php, aber wenn ich versuche zu öffnen, das Bild
Vorschau sagt, Datei kann nicht geöffnet werden, da beschädigt oder bad file type format
jedenfalls ist dies die JS, die nimmt Foto als base64 und senden, 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>
InformationsquelleAutor der Frage bombastic | 2013-07-23
Du musst angemeldet sein, um einen Kommentar abzugeben.
AFAIK, verwenden Sie die Bild-Funktion imagecreatefromstring, imagejpeg um die Bilder zu erstellen.
Hoffe, das wird helfen.
PHP CODE WITH IMAGE DATA
Also Folgendes ist der php-Teil des Programms ..
NOTE
die änderung mit einem KommentarChange is here
InformationsquelleAutor der Antwort Yogesh
InformationsquelleAutor der Antwort naresh
Hier ist, was schließlich für mich gearbeitet. Müssen Sie konvertieren Sie den code für Ihre eigenen Bedürfnisse, aber dies wird es tun.
InformationsquelleAutor der Antwort Justin Eldracher
Dekodieren und speichern, Bild als PNG
InformationsquelleAutor der Antwort
Client senden müssen base64-server.
Und Antwort oben beschriebene code funktioniert perfekt:
Dank
InformationsquelleAutor der Antwort Dhiraj