PHP Classes

Upload Images to S3

Recommend this page to a friend!

      PHP Parse DOCX to HTML with Images  >  All threads  >  Upload Images to S3  >  (Un) Subscribe thread alerts  
Subject:Upload Images to S3
Summary:Upload Images to S3
Messages:8
Author:Greg
Date:2024-09-23 16:01:13
 

  1. Upload Images to S3   Reply   Report abuse  
Picture of Greg Greg - 2024-09-23 16:01:13
Hello Tim,

Hope your well? still loving this code but I was wondering if you could help again, I need to be able to upload the images to an amazon (AWS) S3 bucket but cant get my head around how the code is uploading images? its clearly complex as its having to pick them from word and go through them? I asked AI which would be helpful if I understood it better, but hopefully it might be able to help you? I have included what it said below, in a hope it might help out? I guess what im asking is which bit of the code do I need to replace the upload to server with upload to AWS S3?

Any help would be amazing? and thanks in advance if you do manage to reply.

AI Suggestion. .
---------------------------------------------
Certainly! I can help you modify the code to upload images to an Amazon S3 bucket instead of storing them on the server. This approach offers several benefits, including scalability, durability, and easier management of your image files. [1]

Modify the existing code to upload to S3 instead of the local server

Let's go through these steps:

Install the AWS SDK for PHP: You can install it using Composer. Run the following command in your project directory:

composer require aws/aws-sdk-php

Configure AWS credentials: It's best to use environment variables for AWS credentials. Set these in your server environment:

AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=your_preferred_region

Modify the existing code: Here's an example of how you could modify your PHP code to upload to S3:

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Create an S3 client
$s3 = new S3Client([
'version' => 'latest',
'region' => getenv('AWS_DEFAULT_REGION')
]);

{
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}

if ($uploadOk == 1) {
$bucket = 'XXXXXXXXXXXXXXXX';
$key = 'images/' . basename($_FILES["image"]["name"]);

try {
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $_FILES["image"]["tmp_name"]
]);

echo "Image uploaded successfully. Image URL: " . $result['ObjectURL'];
} catch (AwsException $e) {
echo "There was an error uploading the file.\n";
echo $e->getMessage();
}
}
}
?>

<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" id="image">
<input type="submit" value="Upload Image" name="submit">
</form>

Make sure to replace 'your-bucket-name' with the actual name of your S3 bucket.

This code does the following:

It checks if the uploaded file is an image.

If it is an image, it attempts to upload it to the specified S3 bucket.

If the upload is successful, it displays the URL of the uploaded image.

If there's an error, it displays an error message.

Remember to set up the appropriate CORS configuration on your S3 bucket if you're uploading directly from a web browser.

This approach will store your images in S3 rather than on your local server, providing you with the benefits of Amazon S3's scalability and durability.

  2. Re: Upload Images to S3   Reply   Report abuse  
Picture of Timothy Edwards Timothy Edwards - 2024-09-24 21:15:31 - In reply to message 1 from Greg
Greg,
Thanks for your message, Glad that you are still liking my code. For your info, I will be updating it shortly, to fix a couple of bugs and some small enhancements to cover Word features not previously covered.

Regarding uploading the resultant images to Amazon S3. It is possible using PHP, but you will also need to install AWS SDK for PHP to do the actual uploading. See here for a good article on how to upload to Amazon S3 using PHP.
codexworld.com/upload-files-to-amaz ...

In the current latest version of my code (v2.1.13), the function which saves the images to the server is 'createImage' at lines 1450 to 1527.
However because of the way I have to extract the image code from Word, I need to use the create image functions like 'imagejpeg()'. I can't just copy the file.

So what you would probably need to do, is let it save the files to the server as usual and then insert some code to then copy the saved file to S3. Probably best to create a separate file and use the php 'include' function to bring the code in. Save accidentally messing up my code and will also be easier to upgrade for any future version of my code.

Hope that this helps. If you need more help please email on timothy.edwards1@btinternet.com

  3. Re: Upload Images to S3   Reply   Report abuse  
Picture of Greg Greg - 2024-09-25 18:18:11 - In reply to message 1 from Greg
Hi Tim,

Thanks for the reply your a top man! I have been playing around and come to the same conclusion to just transfer them later, or to store in memory and pass to S3 once processed and ready.

Exciting to hear there is a new version coming!

would it be possible for it to include the header of the document?

and purely out of intrest what else are you going to cover in the next update? and when might it come as if its soon i might hang fire?

Exciting! and thanks again as always <3

  4. Re: Upload Images to S3   Reply   Report abuse  
Picture of Greg Greg - 2024-09-26 15:25:51 - In reply to message 1 from Greg
also Tim just to give a little back, my project required it to upload the png with a transparent background, where as the code uploaded it with black. . however if you change the case 'png' to

case 'png':
// Ensure alpha channel is preserved
imagesavealpha($im, true);
// Set alpha blending mode
imagealphablending($im, false);
// Output PNG with full alpha channel
imagepng($im, null, 9, PNG_ALL_FILTERS);
$contentType = 'image/png';
break;

Thought it was worth a share with you :)

  5. Re: Upload Images to S3   Reply   Report abuse  
Picture of Timothy Edwards Timothy Edwards - 2024-09-26 19:55:44 - In reply to message 4 from Greg
Greg,
Thanks for your suggestion re png images, I will incorporate it.

Will look into headers (and also footers). One of the biggest problems with these is that they can change several times in the course of a document. Do I just take the first one, or do I try and somehow incorporate them all. Obviously page numbering would be omitted.

Improvements for the next version are (already coded)
1. The horizontal line across a page (usually created by typing several dashes and then pressing return) were not always recognised properly. As well as fixing this it will now also recognise dotted, dashed and double lines and duplicate them.
2. Put a <div> around tables for external CSS.
3. Links will now use the href target selected in Word so that you can either keep in the same page/tab or go to a new one.
4. I was having a problem with browser caching when frequently changing the Word document, so I now force a browser reload for all images.
5. Will now recognise when an image is rotated (0 - 360 deg) or flipped in Word and duplicate it in html.

Improvements still to code
1. Recognise Text Boxes and their rotation and duplicate them in html, although positioning on the page may be an issue with some.
2. Will look into headers and footers.

I am afraid Word Art will be one area in which I am unlikely to be able to replicate.

Regarding timescales, I would hope to be able to do this within a couple of weeks or so, but I can't guarantee anything. Depends how much time I can give to it.

Hope that some of these may be of interest to you,
Timothy

  6. Re: Upload Images to S3   Reply   Report abuse  
Picture of Greg Greg - 2024-09-26 21:14:33 - In reply to message 1 from Greg
It's a pleasure Timothy and great for me to be able to offer something back, and help towards the great bit of work. I'll happily share anything else I teak as we get a lot of user feedback, or happy to share with you and we try crack together (im nowhere near your standard by the way)

To answer your question in regards to the header, I think just the first one, as they repeat the same on every page and as this is to convert to html surely the page thing is redundant and it just needs to show at the top of the first long page? thats my thinking anyway, but I guess as always there are many use cases for your work.

Oooo thats really cool regarding those updates, look forward to seeing the updated version <3 and as you suggested iv I have kept my S3 image upload to its own script so I can still upgrade.

Thanks again for all your hard work on this and for replying to me, it's much appreciated. Out of interest are you UK based?

  7. Re: Upload Images to S3   Reply   Report abuse  
Picture of Timothy Edwards Timothy Edwards - 2024-09-28 21:20:49 - In reply to message 6 from Greg
Greg,
Yes, I am UK based, in North Yorkshire. Are you UK as well?

Now got text boxes and their rotation implemented and starting to look at headers/footers. This doesn't look too difficult. However I am still wondering which headers/footers to implement as you can have different ones on the first page to the rest of the document. (You can also have different ones on the odd/even pages although I am not too worried about those as they will almost always be mirror images of each other.)

I am tempted to to add in another user option, so that users can select:-
1. No header or footer.
2. The header and footer as page 1.
3. The header and footer as page 2 (if this is different from page 1).
I could even give different options for the header and footer.

What do you think?

Of course the header will be at the top of the html page and the footer at the bottom.

If you send me an email (my email address is in the first reply I made to you in this thread), I could send you the updated script in advance of it being updated officially on this website, so you can try it out and give me feedback first.

  8. Re: Upload Images to S3   Reply   Report abuse  
Picture of Greg Greg - 2024-09-29 16:04:12 - In reply to message 1 from Greg
I have dropped you a message to your email Timothy :)