<?php header('Content-type: image/jpeg'); // Create Image From Existing File $jpg_image = imagecreatefromjpeg('img.jpg'); // Allocate A Color For The Text $white = imagecolorallocate($jpg_image, 255, 255, 255); // Set Path to Font File $font_path = 'E:\serv\OSPanel\domains\localhost\verdana.ttf'; // Set Text to Be Printed On Image $text = "**$name**"; $text2 = "**$description**"; // Print Text On Image imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text); imagettftext($jpg_image, 25, 0, 100, 400, $white, $font_path, $text2); // Send Image to Browser imagejpeg($jpg_image,"**$name**.jpg"); // Clear Memory imagedestroy($jpg_image);?> 

1) I want to output from the database the name and description on the image, and save the file with the product name jpg. 2) how to do it automatically for all products by product name.

  • specify the structure of your tables - Mykola Veriga
  • DB is called mybd, table shop, => id, name, description - Sergey

1 answer 1

For example, let's assume that you already have a mysql server installed locally. There you already have a tab images with fields

 Images id description image_name $host = 'localhost'; $dbname = 'you_database'; $user = 'user_login'; $password = 'user_pass'; try { $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $password); $sql = 'SELECT id, description, image_name FROM images'; $sth = $dbh->prepare($sql); $sth->execute(); $images = $sth->fetchAll(); echo '<pre>'; print_r($images); $dbh = null; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } 
  • ok add image field, here is the path to mybd database, table shop, => id, name, description, image - Sergey
  • I start connecting to the database like this $ connection = mysqli_connect ("localhost", "root", "", "data"); if ($ connection == false) {echo "Could not connect"; echo mysqli_connect_error (); exit (); } - Sergey
  • look at the book by Mark Gruber Understanding SQL, it will be much easier if you still read books in case something is not clear. For example, it is not clear how to get data from the database, find a book for those who start SQL. You will read 300-500 pages in a week, perhaps not everyone will understand, but other knowledge will be structured and will be confident in them - Mykola Veriga