0

For context, I'm a begginer web developer and I'm currently working on building a school system using a simple CRUD interface and database connections. Most of the system is complete and I am now working on some way to mass print ID cards for each of the students however I having a hard time fetching the img from the database.

Giving more details about the DB, it was determinated that we would store the img directly into it, even though i've seen it isn't the best method to do it. Also the img is stored in a BLOB cell.

Here it is a sample of he code i've made so far:

<?php
header("content-type: image/jpg");
include 'config-pdo.php';
$file_name='template.jpeg';
require 'config-pdo.php';
$sql="SELECT * FROM student";
foreach($dbo->query($sql) as $row){
    $x=180;
    $y=440;
    $img_source=imagecreatefromjpeg($file_name);
    
    // The idea is to use imagecopy() or something like that and fetch
    // the picture inside the database.
    // Using the id and the loop to garantee that it will be the 
    // correct pic.
    $text_color=imagecolorallocate($img_source,0,0,0);

    ImageString($img_source,5,$x,$y,$row['name'],$text_color);


    imagejpeg($img_source,"outputs/$row[id].jpg");
    imagedestroy($img_source);
}
?>
  • You can not respond to one single request, with a response that contains data for multiple images in one go. – CBroe May 10 '22 at 14:20
  • What exactly are you struggling with? I don't see anything in the above re: attempting to add the user's image into the image template. [`imagecreatefromstring($blob)`](https://www.php.net/imagecreatefromstring) would work? P.S. You probably don't want to both `include` and `require 'config-pdo.php';`. – Markus AO May 10 '22 at 14:23
  • My struggle is on how to fetch the image from the database. I've tried using this line $profilepic = imagecreatefromjpeg($row['foto']); but it didn't work and rreturned this error: Fatal error: Uncaught ValueError: imagecreatefromjpeg(): Argument #1 ($filename) must not contain any null bytes in C:\xampp\htdocs\gd-certificate2\teste.php:12 Stack trace: #0 C:\xampp\htdocs\gd-certificate2\teste.php(12): imagecreatefromjpeg('\xFF\xD8\xFF\xE1\r-Exif\x00\x00MM\x00...') #1 {main} thrown in C:\xampp\htdocs\gd-certificate2\teste.php on line 12 Ops: forgot to remove the require :/ – Lucas Santiago May 10 '22 at 14:45
  • If you have the binary image data stored in your database already, then you don't need to use `imagecreatefromjpeg`. You do not want to _work_ with those images using the GD library functions, you just want to show them on the client. – CBroe May 10 '22 at 14:59

0 Answers0