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:

http://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?

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

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

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

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.

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

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

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

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";
}
}

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

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.

Đã trả lời ngày 21 tháng 8 năm 2019 lúc 6:41Aug 21, 2019 at 6:41

Fazil Razafazil RazaFazil Raza

1312 Huy hiệu bạc6 Huy hiệu đồng2 silver badges6 bronze badges

$datetime = date("Y-m-d h:i:s");
$timestamp = strtotime($datetime);
$image = $_POST['image'];
$imgdata = base64_decode($image);
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
$temp=explode('/',$mime_type);
$path = "uploads/$timestamp.$temp[1]";
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded->>> $timestamp.$temp[1]";

Điều này sẽ đủ để xử lý hình ảnh. Đặc biệt cảm ơn ông Dev Karan Sharma

Đã trả lời ngày 10 tháng 7 năm 2018 lúc 12:09Jul 10, 2018 at 12:09

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

Làm thế nào để tạo tệp từ Base64 PHP?

Php Php Tạo tệp từ câu trả lời mã của Base64..
hàm base64_to_jpeg ($ base64_string, $ output_file) {.
// Mở tệp đầu ra để viết ..
$ ifp = fopen ($ output_file, 'wb') ;.
// Chia chuỗi trên dấu phẩy ..
// $ data [0] == "Dữ liệu: hình ảnh/png; base64".
// $ dữ liệu [1] ==.

Làm thế nào để lưu chuỗi base64 dưới dạng hình ảnh trong PHP?

Trong khi làm việc với Canvas, bạn sẽ có chuỗi được mã hóa Base64 trong biểu mẫu, sau đó bạn sẽ gửi dữ liệu biểu mẫu đến máy chủ bằng phương thức POST và trên máy chủ, bạn chuyển đổi chúng thành một tệp hình ảnh. Xác định ('upload_dir', 'hình ảnh/'); $ image_parts = explode ("; base64,", $ _post ['Image']);define('UPLOAD_DIR', 'images/'); $image_parts = explode(";base64,", $_POST['image']);

Làm cách nào để lưu một tệp base64?

Cách chuyển đổi Base64 thành Tệp.Dán chuỗi của bạn vào trường Base Base64.Nhấn nút Decode Decode Base64 vào tệp.Nhấp vào liên kết tên tệp để tải xuống tệp.Paste your string in the “Base64” field. Press the “Decode Base64 to File” button. Click on the filename link to download the file.

Làm cách nào để lưu hình ảnh được mã hóa base64?

Phương pháp Tobase64String.Bây giờ để lưu chuỗi được mã hóa base64 dưới dạng tệp hình ảnh, chuỗi được mã hóa base64 được chuyển đổi trở lại mảng byte bằng hàm convert.frombase644String.Cuối cùng, mảng byte được lưu vào thư mục trên đĩa dưới dạng tệp bằng phương thức WriteallBytes của lớp tệp.the Base64 encoded string is converted back to Byte Array using the Convert. FromBase64String function. Finally the Byte Array is saved to Folder on Disk as File using WriteAllBytes method of the File class.