Hướng dẫn php upload image base64 encode - php upload image base64 encode

Trong hướng dẫn này, chúng tôi sẽ cho bạn biết cách xử lý hình ảnh được mã hóa bằng base64 và viết hình ảnh vào thư mục. Base64 được mã hóa. Vì vậy, trong trường hợp đó, bạn sẽ cần di chuyển hình ảnh được mã hóa base64 sang máy chủ dưới dạng tệp hình ảnh. Và phải lưu nó trong thư mục
While we have working with API for mobile or web application , You will notice that they will send the format of images in Base64 encoded. So in that case, you will need to move the Base64 encoded image to server as a image file.and have to save it in the folder

Ví dụ php cốt lõi:

<?php
    $base64string = '';
    $uploadpath   = 'upload/images/';
    $parts        = explode(";base64,", $base64string);
    $imageparts   = explode("image/", @$parts[0]);
    $imagetype    = $imageparts[1];
    $imagebase64  = base64_decode($parts[1]);
    $file         = $uploadpath . uniqid() . '.png';
    file_put_contents($file, $imagebase64);
?> 

Tải lên cơ sở64 để tệp trong codeigniter & nbsp;

Bạn đang tìm kiếm một cách để lưu phía máy chủ hình ảnh PNG, từ chuỗi dữ liệu Base64 hoặc không thể tải lên hình ảnh được mã hóa Base64 bằng Codeigniter? Chúng tôi có thể giúp bạn với điều đó.

Chúng tôi đang tạo một số chức năng để sử dụng kiểm tra chuỗi cơ sở64 có hợp lệ hay không và kích thước và loại tệp

Tạo ứng dụng tệp/thư viện/base64fileuploads.phpapplication/libraries/Base64fileUploads.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Base64fileUploads { 
    
    function is_base64($s){
        // Check if there are valid base64 characters
       // if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s)) return false;
        // Decode the string in strict mode and check the results
        $decoded = base64_decode($s, true);
        if(false === $decoded) return false;
        // Encode the string again
        if(base64_encode($decoded) != $s) return false;
        return true;
    }
    
    public function du_uploads($path,$base64string){
        if(
            $this->is_base64($base64string) == true 
        ){  
            $base64string = "data:image/jpeg;base64,".$base64string;
            $this->check_size($base64string);
            $this->check_dir($path);
            $this->check_file_type($base64string);
            
            /*=================uploads=================*/
            list($type, $base64string) = explode(';', $base64string);
            list(,$extension)          = explode('/',$type);
            list(,$base64string)       = explode(',', $base64string);
            $fileName                  = uniqid().date('Y_m_d').'.'.$extension;
            $base64string              = base64_decode($base64string);
            file_put_contents($path.$fileName, $base64string);
            return array('status' =>true,'message' =>'successfully upload !','file_name'=>$fileName,'with_path'=>$path.$fileName);
        }else{
            print_r(json_encode(array('status' =>false,'message' => 'This Base64 String not allowed !')));exit;
        }
    }
    
    public function check_size($base64string){
        $file_size = 8000000;
        $size = @getimagesize($base64string);
        
        if($size['bits'] >= $file_size){
            print_r(json_encode(array('status' =>false,'message' => 'file size not allowed !')));exit;
        }
        return true;
    }
    
    public function check_dir($path){
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            return true;
        }
        return true;
    }
    
    public function check_file_type($base64string){
        $mime_type = @mime_content_type($base64string);
        $allowed_file_types = ['image/png', 'image/jpeg', 'application/pdf'];
        if (! in_array($mime_type, $allowed_file_types)) {
            // File type is NOT allowed
           // print_r(json_encode(array('status' =>false,'message' => 'File type is NOT allowed !')));exit;
        }
        return true;
    }
}

Ví dụ Codeigniter:

Gọi lớp này Codeigniter & nbsp; trong Trình điều khiển & NBSP;


$base64file   = new Base64fileUploads();
$return = $base64file->du_uploads($this->data['document'],$document_front_site);

22

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 biết cách tải lên tệp hình ảnh và lưu đến vị trí khác bằng cách sử dụng mã sau. Tuy nhiên, tôi cần thực hiện theo cách mà người dùng tải lên hình ảnh và tự động chuyển đổi sang Base64 mà không lưu hình ảnh đó ở vị trí của tôi. Tôi nên làm thế nào?

<?php
//print_r($_FILES);
if(isset($_FILES['image']))
{
    $errors=array();
    $allowed_ext= array('jpg','jpeg','png','gif');
    $file_name =$_FILES['image']['name'];
 //   $file_name =$_FILES['image']['tmp_name'];
    $file_ext = strtolower( end(explode('.',$file_name)));


    $file_size=$_FILES['image']['size'];
    $file_tmp= $_FILES['image']['tmp_name'];
    echo $file_tmp;echo "<br>";

    $type = pathinfo($file_tmp, PATHINFO_EXTENSION);
    $data = file_get_contents($file_ext);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    echo "Base64 is ".$base64;



    if(in_array($file_ext,$allowed_ext) === false)
    {
        $errors[]='Extension not allowed';
    }

    if($file_size > 2097152)
    {
        $errors[]= 'File size must be under 2mb';

    }
    if(empty($errors))
    {
       if( move_uploaded_file($file_tmp, 'images/'.$file_name));
       {
        echo 'File uploaded';
       }
    }
    else
    {
        foreach($errors as $error)
        {
            echo $error , '<br/>'; 
        }
    }
   //  print_r($errors);

}
?>


<form action="" method="POST" enctype="multipart/form-data">

<p>
    <input type="file" name="image" />
    <input type="submit" value="Upload">

</p>
</form>

Hỏi ngày 12 tháng 10 năm 2013 lúc 14:50Oct 12, 2013 at 14:50

Hướng dẫn php upload image base64 encode - php upload image base64 encode

Khant thu Linnkhant Thu LinnKhant Thu Linn

5.6657 Huy hiệu vàng49 Huy hiệu bạc114 Huy hiệu đồng7 gold badges49 silver badges114 bronze badges

14

Có một sai lầm trong mã của bạn:

$data = file_get_contents( $file_ext );

Điều này nên là:

$data = file_get_contents( $file_tmp );

Điều này sẽ giải quyết vấn đề của bạn.

Đã trả lời ngày 16 tháng 2 năm 2015 lúc 11:16Feb 16, 2015 at 11:16

Hướng dẫn php upload image base64 encode - php upload image base64 encode

halfpastfour.amhalfpastfour.amhalfpastfour.am

5.5543 Huy hiệu vàng41 Huy hiệu bạc59 Huy hiệu Đồng3 gold badges41 silver badges59 bronze badges

Làm thế nào để tải lên hình ảnh base64 trong PHP?

Cách tải lên Base64 lên tệp trong PHP..
$ base64String = '';.
$ uploadPath = 'Tải lên/hình ảnh/' ;.
$ parts = Explode ("; base64,", $ base64String) ;.
$ ImageParts = Explode ("Image/", @$ Phần [0]) ;.
$ ImageType = $ ImageParts [1] ;.
$ imageBase64 = base64_decode ($ phần [1]) ;.
$ file = $ uploadPath. uniqid (). '. png ';.

Làm cách nào để mã hóa một hình ảnh trong base64?

Làm thế nào để chuyển đổi hình ảnh thành Base64 trực tuyến..
Chọn nguồn hình ảnh từ trường Data DataType ..
Dán URL hoặc chọn hình ảnh từ máy tính của bạn ..
Nếu cần thiết, chọn định dạng đầu ra mong muốn ..
Nhấn vào nút Imcode Encode vào nút BASE64 ..
Tải xuống hoặc sao chép kết quả từ trường Base Base64 ..

Làm thế nào để bạn mã hóa base64 trong PHP?

Cú pháp.hàm base64_encode () có thể mã hóa dữ liệu đã cho với Base64.Mã hóa này được thiết kế để làm cho dữ liệu nhị phân tồn tại vận chuyển thông qua các lớp vận chuyển không sạch 8 bit như các thân thư.Dữ liệu được mã hóa cơ sở64 có thể mất khoảng 33% không gian so với dữ liệu gốc.base64_encode() function can encode the given data with base64. This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean such as mail bodies. Base64-encoded data can take about 33% more space than original data.

Làm thế nào tôi có thể tải xuống hình ảnh base64 trong PHP?

PHP như một hình ảnh cơ sở64 ...
download..
base64..