Hướng dẫn write base64 to file php - ghi base64 vào tệp php

174

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang cố gắng chuyển đổi chuỗi hình ảnh Base64 của mình thành một tệp hình ảnh. Đây là chuỗi base64 của tôi:

//pastebin.com/ENkTrGNG

Sử dụng mã sau để chuyển đổi nó thành một tệp hình ảnh:

function base64_to_jpeg[ $base64_string, $output_file ] {
    $ifp = fopen[ $output_file, "wb" ]; 
    fwrite[ $ifp, base64_decode[ $base64_string] ]; 
    fclose[ $ifp ]; 
    return[ $output_file ]; 
}

$image = base64_to_jpeg[ $my_base64_string, 'tmp.jpg' ];

Nhưng tôi đang gặp lỗi của invalid image, có chuyện gì vậy?

Jasonmarcher

13.6K22 Huy hiệu vàng55 Huy hiệu bạc51 Huy hiệu Đồng22 gold badges55 silver badges51 bronze badges

Đã hỏi ngày 1 tháng 3 năm 2013 lúc 8:49Mar 1, 2013 at 8:49

0

Vấn đề là

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 ] == 
    $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; 
}
0 được bao gồm trong các nội dung được mã hóa. Điều này sẽ dẫn đến dữ liệu hình ảnh không hợp lệ khi hàm base64 giải mã nó. Xóa dữ liệu đó trong hàm trước khi giải mã chuỗi, như vậy.

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 ] == 
    $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; 
}

Đã trả lời ngày 1 tháng 3 năm 2013 lúc 8:58Mar 1, 2013 at 8:58

14

Một cách dễ dàng tôi đang sử dụng:

file_put_contents[$output_file, file_get_contents[$base64_string]];

Điều này hoạt động tốt vì

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 ] == 
    $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; 
}
1 có thể đọc dữ liệu từ URI, bao gồm dữ liệu: // uri.

miken32

40.6K16 Huy hiệu vàng100 Huy hiệu bạc140 Huy hiệu đồng16 gold badges100 silver badges140 bronze badges

Đã trả lời ngày 10 tháng 4 năm 2018 lúc 14:52Apr 10, 2018 at 14:52

Henry Trầnhenry trầnHenry Trần

1.12610 Huy hiệu bạc13 Huy hiệu đồng10 silver badges13 bronze badges

5

Bạn cần xóa phần nói

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 ] == 
    $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; 
}
0 khi bắt đầu dữ liệu hình ảnh. Dữ liệu Base64 thực tế đến sau đó.

Chỉ cần loại bỏ mọi thứ lên đến và bao gồm

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 ] == 
    $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; 
}
3 [trước khi gọi
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 ] == 
    $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; 
}
4 trên dữ liệu] và bạn sẽ ổn.

Ashleedawg

Phim thương hiệu vàng 19.4K77 gold badges69 silver badges100 bronze badges

Đã trả lời ngày 1 tháng 3 năm 2013 lúc 8:53Mar 1, 2013 at 8:53

aaaaaa123456789aaaaaa123456789aaaaaa123456789

5.2081 Huy hiệu vàng20 Huy hiệu bạc 33 Huy hiệu đồng1 gold badge20 silver badges33 bronze badges

1

Có thể như thế này

function save_base64_image[$base64_image_string, $output_file_without_extension, $path_with_end_slash="" ] {
    //usage:  if[ substr[ $img_src, 0, 5 ] === "data:" ] {  $filename=save_base64_image[$base64_image_string, $output_file_without_extentnion, getcwd[] . "/application/assets/pins/$user_id/"]; }      
    //
    //data is like:    data:image/png;base64,asdfasdfasdf
    $splited = explode[',', substr[ $base64_image_string , 5 ] , 2];
    $mime=$splited[0];
    $data=$splited[1];

    $mime_split_without_base64=explode[';', $mime,2];
    $mime_split=explode['/', $mime_split_without_base64[0],2];
    if[count[$mime_split]==2]
    {
        $extension=$mime_split[1];
        if[$extension=='jpeg']$extension='jpg';
        //if[$extension=='javascript']$extension='js';
        //if[$extension=='text']$extension='txt';
        $output_file_with_extension=$output_file_without_extension.'.'.$extension;
    }
    file_put_contents[ $path_with_end_slash . $output_file_with_extension, base64_decode[$data] ];
    return $output_file_with_extension;
}

Đã trả lời ngày 25 tháng 5 năm 2016 lúc 17:26May 25, 2016 at 17:26

1

Đó là một chủ đề cũ, nhưng trong trường hợp bạn muốn tải lên hình ảnh có cùng tiện ích mở rộng-

    $image = $request->image;
    $imageInfo = explode[";base64,", $image];
    $imgExt = str_replace['data:image/', '', $imageInfo[0]];      
    $image = str_replace[' ', '+', $imageInfo[1]];
    $imageName = "post-".time[].".".$imgExt;
    Storage::disk['public_feeds']->put[$imageName, base64_decode[$image]];

Bạn có thể tạo 'public_feed' trong hệ thống tập tin của Laravel.php-

   'public_feeds' => [
        'driver' => 'local',
        'root'   => public_path[] . '/uploads/feeds',
    ],

Đã trả lời ngày 31 tháng 12 năm 2018 lúc 17:39Dec 31, 2018 at 17:39

1

if[$_SERVER['REQUEST_METHOD']=='POST']{
$image_no="5";//or Anything You Need
$image = $_POST['image'];
$path = "uploads/".$image_no.".png";

$status = file_put_contents[$path,base64_decode[$image]];
if[$status]{
 echo "Successfully Uploaded";
}else{
 echo "Upload failed";
}
}

Julfikar

1.2132 huy hiệu vàng16 Huy hiệu bạc33 Huy hiệu đồng2 gold badges16 silver badges33 bronze badges

Đã trả lời ngày 12 tháng 10 năm 2017 lúc 11:44Oct 12, 2017 at 11:44

3

Mã này làm việc cho tôi.

Bài Viết Liên Quan

Chủ Đề