簡體   English   中英

如何在 php 的數據庫中存儲從字符串創建的 base64 圖像?

[英]How to store base64 image create from string in database in php?

我想將我從字符串創建的圖像存儲到 php 中的數據庫中。但是有一個錯誤。它顯示一條錯誤消息。 消息是警告C 中的未定義數組鍵 1:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php29

警告C 中的未定義數組鍵 1:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php30
我如何解決/解決這個問題的任何想法。在此先感謝...

這是我的演示代碼...

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <button>click</button>

   

  <script type="text/javascript">
         $(document).ready(function(){
            $('button').click(function(){
                let text = "What is Lorem Ipsum?Lorem Ipsum is ";
                $.ajax({
                    url:'create_image_php.php',
                    type:'post',
                    data:{"text":text},
                    success:function(data){
                        alert(data);
                        console.log(data);
                    }
                })
            })
         })
     </script>
    
    </body>
    </html>

create image_php.php



<?php 
$text = $_POST['text'];
$image_width = 320; // pixels
text_to_image($text, $image_width);
function text_to_image($text, $image_width, $colour = array(0,244,34), $background = array(0,0,0))
{
    include 'conn.php';

    $font = 5;
    $line_height = 30;
    $padding = 10;
    $text = wordwrap($text, ($image_width/10));
    $lines = explode("\n", $text);
    $image = imagecreate($image_width,480);
    $background = imagecolorallocate($image, $background[0], $background[1], $background[2]);
    $colour = imagecolorallocate($image,$colour[0],$colour[1],$colour[2]);
    imagefill($image, 0, 0, $background);
    $i = $padding;
    foreach($lines as $line){
        imagestring($image, $font, $padding, $i, trim($line), $colour);
        $i += $line_height;
    }
    ob_start();
    imagepng($image);
    $image= printf('data:image/png;base64,%s', base64_encode(ob_get_clean()));
    $image_array_1 = explode(";", $image);
    $image_array_2 = explode(",", $image_array_1[1]);
    $image = base64_decode($image_array_2[1]);
    $imageName = time() . '.png';
    $image = file_put_contents($imageName, $image);
    $image_file = addslashes(file_get_contents($imageName));
    $sql = "INSERT INTO `images`(`image`) VALUES(:image_file)";
    $stmt = $conn->prepare($sql);
    $stmt->bindparam(':image_file',$image_file);
    $query = $stmt->execute();
    if($query){
        echo "success";
    }
    else{
        echo "error";
    }
    exit;
} ?>

conn.php

<?php
try{
$conn = new PDO("mysql:host=localhost;dbname=social_site","root","");
}
catch(PDOException $e){
echo "Error:".$e->getMessage();
}
?>
Table Name:images
index:image
$image= printf('data:image/png;base64,%s', base64_encode(ob_get_clean()));

printf function 用於“根據格式生成 output”,它返回 output 的長度,而不是格式的結果,嘗試將此代碼更改為:

$image = 'data:image/png;base64,'.base64_encode(ob_get_clean();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM