Add image to my PHP GD

Hello
Recently I tried to create user signatures via PHP GD.
I am using PHP GD class and everythign works great but I am facing a problem.
When I’m trying to add image from the database (database value for example: “http://www.someremoteurl.com/image.png”) it doesn’t work and show me blank instead of the signature.
This is my signature php file:

<?php
use PHPImageWorkshop\\ImageWorkshop as ImageWorkshop;
require_once('GD_Includes/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php');
require_once('GD_Includes/PHPImageWorkshop/Exception/ImageWorkshopException.php');
require_once('GD_Includes/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php');
require_once('GD_Includes/PHPImageWorkshop/Core/ImageWorkshopLib.php');
require_once('GD_Includes/PHPImageWorkshop/Core/ImageWorkshopLayer.php');
require_once('GD_Includes/PHPImageWorkshop/ImageWorkshop.php');
include("sigconfig.php");

$User = mysql_real_escape_string($_GET["user"]);
$query = mysql_query("SELECT * from user WHERE id = '".$User."'") or die(mysql_error());
if(mysql_num_rows($query) >=1) {
$row = mysql_fetch_array($query);


   function utf8_strrev($str){
   if (!preg_match('/^[\\w\\d\\s.,-]*$/', $str)) {
   preg_match_all('/./us', $str, $ar);
   return join('',array_reverse($ar[0]));
   }
   else {
	return $str;
   }
}


$Username = ''.$row["first_name"].'';
//Favorites!
$getfav = mysql_query('SELECT title,artist,img FROM favorites WHERE user_id = "'.$row['id'].'"');
$fav1 = mysql_fetch_assoc($getfav);
$fav1title = $fav1['title'];
$fav1artist = $fav1['artist'];
$fav1img = $fav1['img'];
$fav2 = mysql_fetch_assoc($getfav);
$fav2title = $fav2['title'];
$fav2artist = $fav2['artist'];
$fav2img = $fav2['img'];
$fav3 = mysql_fetch_assoc($getfav);
$fav3title = $fav3['title'];
$fav3artist = $fav3['artist'];
$fav3img = $fav3['img'];
$Base_Pic = "GD_Includes/CardNone.png";
$BaseLayer = ImageWorkshop::initFromPath($Base_Pic);

$text_username = $Username;
// Font Normal
$fontPath = "GD_Includes/arial.ttf"; // Song
$fontPath2 = "GD_Includes/PoetsenOne-Regular.ttf"; // Artist
$fontSize = 14; // Artist
$fontSize2 = 12;  // Song
$fontColor = "FFFFFF";
$textRotation = 0;
$text = ImageWorkshop::initTextLayer($text_username, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
//Songs
$song1a = ImageWorkshop::initTextLayer($fav1artist, $fontPath2, $fontSize, $fontColor, $textRotation, $backgroundColor);
$song1t = ImageWorkshop::initTextLayer($fav1title, $fontPath, $fontSize2, $fontColor, $textRotation, $backgroundColor);

$BaseLayer->addLayerOnTop($text, 20, 11, "LT");
$BaseLayer->addLayerOnTop($song1a, 50, 45, "LT");
$BaseLayer->addLayerOnTop($song1t, 55, 70, "LT");

//Show Result
$image = $BaseLayer->getResult();
header('Content-type: image/png');
header('Content-Disposition: filename="card.png"');
imagepng($image, null, 8); // We choose to show a PNG (quality of 8 on a scale of 0 to 9)
exit;
}
else {
echo "NOT FOUND";
}

?>

My goal is to add an image which stored in the parameter “$fav1img” near the text.
Any help?

Hello. I really need help with this - hope someone can help.

Thanks!

Okay, help me understand this better.

  1. You have an image URL stored in your database
  2. You want to read that image URL,
  3. download the image
  4. then add it to another image using GD

Am I on the right track? If so, you are missing step 3 which is important to your goal in your code.

I dont need 3
I can’t use images from remote URL in php GD?
because my solution is like that:
I have database table named: ‘user’
and it contains: username & avatar
avatar is url stored in remote url like http://www.someimageurl.com/myavatar.png
and I want to show it in my PHP GD image.

I’m looking at the documentation and I’m not seeing any tutorials or references to including images by URL

The tutorial, uses local files…

I know how to insert local
but I need to do it with remote :frowning:
I’ll don’t use it I guess…

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

You’d have to try it, but this may work:

<?php
use PHPImageWorkshop\\ImageWorkshop as ImageWorkshop;
require_once('GD_Includes/PHPImageWorkshop/Exception/ImageWorkshopBaseException.php');
require_once('GD_Includes/PHPImageWorkshop/Exception/ImageWorkshopException.php');
require_once('GD_Includes/PHPImageWorkshop/Core/Exception/ImageWorkshopLayerException.php');
require_once('GD_Includes/PHPImageWorkshop/Core/ImageWorkshopLib.php');
require_once('GD_Includes/PHPImageWorkshop/Core/ImageWorkshopLayer.php');
require_once('GD_Includes/PHPImageWorkshop/ImageWorkshop.php');
include("sigconfig.php");

$User = mysql_real_escape_string($_GET["user"]);
$query = mysql_query("SELECT * from user WHERE id = '".$User."'") or die(mysql_error());
if(mysql_num_rows($query) >=1) {
$row = mysql_fetch_array($query);
 
   
   function utf8_strrev($str){
   if (!preg_match('/^[\\w\\d\\s.,-]*$/', $str)) {
   preg_match_all('/./us', $str, $ar);
   return join('',array_reverse($ar[0]));
   }
   else {
	return $str;
   }
} 
   

$Username = ''.$row["first_name"].'';
//Favorites!
$getfav = mysql_query('SELECT title,artist,img FROM favorites WHERE user_id = "'.$row['id'].'"');
$fav1 = mysql_fetch_assoc($getfav);
$fav1title = $fav1['title'];
$fav1artist = $fav1['artist'];
$fav1img = $fav1['img'];
$fav2 = mysql_fetch_assoc($getfav);
$fav2title = $fav2['title'];
$fav2artist = $fav2['artist'];
$fav2img = $fav2['img'];
$fav3 = mysql_fetch_assoc($getfav);
$fav3title = $fav3['title'];
$fav3artist = $fav3['artist'];
$fav3img = $fav3['img'];
$Base_Pic = "GD_Includes/CardNone.png";
$BaseLayer = ImageWorkshop::initFromPath($Base_Pic);
$avatarLayer = ImageWorkshop::initFromPath($fav1img);

$text_username = $Username;
// Font Normal
$fontPath = "GD_Includes/arial.ttf"; // Song
$fontPath2 = "GD_Includes/PoetsenOne-Regular.ttf"; // Artist
$fontSize = 14; // Artist
$fontSize2 = 12;  // Song
$fontColor = "FFFFFF";
$textRotation = 0;
$text = ImageWorkshop::initTextLayer($text_username, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
//Songs
$song1a = ImageWorkshop::initTextLayer($fav1artist, $fontPath2, $fontSize, $fontColor, $textRotation, $backgroundColor);
$song1t = ImageWorkshop::initTextLayer($fav1title, $fontPath, $fontSize2, $fontColor, $textRotation, $backgroundColor);

$BaseLayer->addLayerOnTop($text, 20, 11, "LT");
$BaseLayer->addLayerOnTop($song1a, 50, 45, "LT");
$BaseLayer->addLayerOnTop($song1t, 55, 70, "LT");
$BaseLayer->addLayerOnTop($avatarLayer, 0, 0, "LT"); // change the X and Y positioning.

//Show Result
$image = $BaseLayer->getResult();
header('Content-type: image/png');
header('Content-Disposition: filename="card.png"');
imagepng($image, null, 8); // We choose to show a PNG (quality of 8 on a scale of 0 to 9)
exit;
}
else {
echo "NOT FOUND";
}

?>

I added the following lines:

$avatarLayer = ImageWorkshop::initFromPath($fav1img);
$BaseLayer->addLayerOnTop($avatarLayer, 0, 0, "LT"); // change the X and Y positioning.

I’ve been already trying that before posting here. the initfrompath seem doesn’t work on remote images

Then I guess you’ll have to download it first…, you can use file_get_contents and file_put_contents to write it locally, then delete it after you use it.
If that doesn’t work, you can use curl to get the image, write it to disk, then delete it (it is a bit more work, but works on every server, the former may not work if remote urls are disabled).