[英]How to store base64 image create from string in database in php?
I want to store my image create from string into database in php.But there is a error.It shows an error message.我想将我从字符串创建的图像存储到 php 中的数据库中。但是有一个错误。它显示一条错误消息。 The message is
Warning : Undefined array key 1 in
C:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php on line
29
消息是警告:
C 中的未定义数组键 1:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php第
29行
Warning : Undefined array key 1 in
C:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php on line
30
警告:
C 中的未定义数组键 1:\xampp\htdocs\drag-drop-image-uploader\create_iamge_php.php第
30行
Any ideas how i can solve/fix this.Thanks in advance...我如何解决/解决这个问题的任何想法。在此先感谢...
Here is my demo code...这是我的演示代码...
<!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()));
the printf function is used for "Produces output according to format.", it returns the length of the output, not the result of format, try change this code to: printf function 用于“根据格式生成 output”,它返回 output 的长度,而不是格式的结果,尝试将此代码更改为:
$image = 'data:image/png;base64,'.base64_encode(ob_get_clean();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.