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?