doudui6756 2014-10-13 10:30
浏览 392

从python将图像插入MySql类型BLOB并从MySql显示到PHP

I am trying to store an image from an url to my database as base64, but when I try to show it in a webpage based on php, it doesn't work..

Here's the python code:

import urllib3, io, requests
from PIL import Image
import base64

URL = 'www....com/pic.jpg'
img_conn = urllib3.connection_from_url('www....com/')
img_file = img_conn.urlopen('GET', URL)
image.seek(0)
resized_image = Image.open(image)
resized_image.thumbnail((75,75), Image.ANTIALIAS
resized_image = base64.b64encode(resized_image.tostring())

The resized_image is added to the database to a column called pic type LONGBLOB.

my PHP code looks like something like this:

$sql = "SELECT pic FROM product LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch();

$decoded_image = base64_decode($row['pic']);

$formImage = imagecreatefromstring($decoded_image);

I get the message:

PHP Warning: imagecreatefromstring(): Data is not in a recognized format in /home/...

What am I doing wrong? Is there another way to save an image from an url inside my database?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?