Jump to content

Return updated temp file back after watermark added.


itsureboy
Go to solution Solved by requinix,

Recommended Posts

I have a script that uploads files uploaded through my website directly to my Amazon s3 bucket.
This never saves the file permanetly to my server, just as a temp_file_location
This works perfect alone.

However,
I am trying to now add a watermark to the image before passing the temp_file_location to the s3 function to upload.

My question is, how can I use the code below, with the temp_file and then return a new temp_file (which is the watermarked version)

Here is just the code for the watermarker, I need this to start with the temp_file and leave me with a new temp_file_location:
 

 

$temp_file_name = $_FILES['theFile']['tmp_name'];
$image = $temp_file_name;
 
// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');  

// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
 
// creting jpg from original image
$image = imagecreatefromjpeg($temp_file_name);
//something went wrong
if ($image === false) {
    return false;
}
// getting the dimensions of original image
$size = getimagesize($image_path);
// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 10;
$dest_y = $size[1] - $watermark_height - 10;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);

// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

//HERE I NEED A $temp_file_name TO BE THE UPDATED FILE WITH THE WATERMARK TO PASS TO THE S3 FUNCTION.


// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
 

Any ideas??
Thanks ahead!!

 

Link to comment
Share on other sites

I know I was trying to use imagejpg(); but couldn't get it to work.

Heres the amazon s3 code thats works alone:

 

 

if (!class_exists('S3'))require_once('S3.php');

//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'BLAHBLAH');
if (!defined('awsSecretKey')) define('awsSecretKey', 'BLAHBLAH');

//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);

$fileName = $_FILES['theFile']['name'];
$temp_file_name = $_FILES['theFile']['tmp_name'];
$fileSize = $_FILES['theFile']['size'];
$fileExt = substr($fileName, strrpos($fileName, '.') + 1);
$fileExt = strtolower($fileExt);

$custompostid = rand(1000000,9999999);
$imageName = $custompostid.".".$fileExt.""; 

//move the file
if ($s3->putObjectFile($temp_file_name, "BUCKETNAME", $image, S3::ACL_PUBLIC_READ, array(), $imageType)) { 
//success
}
else{ 
//error
}
 

But before the putObjectFile above , is where I want to implement this:



$temp_file_name = $_FILES['theFile']['tmp_name'];
$image = $temp_file_name;
 
// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');  

// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
 
// creting jpg from original image
$image = imagecreatefromjpeg($temp_file_name);
//something went wrong
if ($image === false) {
    return false;
}
// getting the dimensions of original image
$size = getimagesize($image_path);
// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 10;
$dest_y = $size[1] - $watermark_height - 10;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);

// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

//HERE I NEED A $temp_file_name TO BE THE UPDATED FILE WITH THE WATERMARK TO PASS TO THE S3 FUNCTION.


// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
 

I just need to link the variable to the function above and then output that variable again updated to pass it to S3 putObjectFile.

 

Hope I didn't confuse you too much.

Edited by itsureboy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.