Reputation: 98
Adding text on images. I have two codes below that works seperately. The first was used to upload image to a server and database via pdo connection. The second was used to watermark a stationary image.
My problem is that i want to combine the two codes so as to text watermark an image during upload to Server without the aids of globle variable. I have been working on this but cannot get it to work. can someone help me to integrate this. Thanks
<?php
include('pdo.php');
if (!isset($_FILES['image']['tmp_name'])) {
echo "";
}else{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
$image_size= getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE) {
echo "That's not an image!";
}else{
move_uploaded_file($_FILES["image"]["tmp_name"],"postphoto/" . $_FILES["image"]["name"]);
$location="postphoto/" . $_FILES["image"]["name"];
$from=$_POST['from'];
$time=time();
$photos='photos';
$statement = $db->prepare('INSERT INTO text_watermark (photo,from_send) values(:photo,:from_send)');
if(!$statement->execute(array(
':photo' => $photos,
':from_send' => $from))){
echo 'There is problem';
}
else{
header("location: lol.php");
exit();
}
}
}
?>
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('sunset.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
Upvotes: 0
Views: 117
Reputation: 470
If your code is correct it's just a case of moving the blocks about. You may want to consider what happens when someone uploads a png. Though you may catch this elsewhere..
<?php
include('pdo.php');
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size == FALSE)
{
echo "That's not an image!";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"], "postphoto/" . $_FILES["image"]["name"]);
$location = "postphoto/" . $_FILES["image"]["name"];
//Watermark it!
################
$jpg_image = imagecreatefromjpeg($location);
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'font.TTF';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
#################
$from = $_POST['from'];
$time = time();
$photos = 'photos';
$statement = $db->prepare('INSERT INTO text_watermark (photo,from_send) values(:photo,:from_send)');
if (!$statement->execute(
array(
':photo' => $photos,
':from_send' => $from
)
)
)
{
echo 'There is problem';
}
else
{
header("location: lol.php");
exit();
}
}
}
if ($jpg_image)
{
//Set the Content Type
header('Content-type: image/jpeg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
}
?>
Upvotes: 1