Im trying to use Intervention with AWS S3, but the resize method is not working for me.
$img = Image::make($file)->rotate($rotate)->crop($width, $width, $x, $y);
$img->backup();
foreach(Config::get('img.image-sizes') as $_k => $_v){
$img->resize($_v['w'], $_v['h']);
$s3->queue($img, $name);
$img->reset();
}
The images upload fine to S3, but resize fails, I get all images the size of the original image
If I call save() on the image, the resize works, but I do not wish to save the image as I am uploading via S3, putting the $img as the body:
$this->commands[] = $this->s3->getCommand('PutObject', [
'Bucket' => env('AWS_BUCKET'),
'Key' => Config::get('img.image-path').$name,
'Body' => $img,
'ContentType' => $img->mime(),
'ACL' => 'public-read'
]);
To get this to work will I have to call save on each image first? If so is there a way to get this to play nice with S3, ideally I do not want to save them to my server first before sending them off to S3.
This is the code I used to get my uploaded images resized and saved to Amazon S3 using Laravel 5 and Intervention.
$imageFile = \Image::make($imageUpload)->resize(600, 600)->stream();
$imageFile = $imageFile->__toString();
$filename = 'myFileName.png';
$s3 = \Storage::disk('s3');
$s3_response = $s3->put('/'.$filename, $imageFile, 'public');
Related
I am running into a problem with s3.
I allow the user to upload images on my website.
When an image is uploaded it stores in s3 and I use the [cloudfront] cdn to display the image.
But now i'm allowing the user to rotate the image. The user clicks the rotate button and the image rotates 90deg and stores back into s3. when I use s3 presigned url it shows that the image has rotated but when I use [cloudfront] url it still shows me the old image.
heres my code
$rotated = 0;
$fileName = $_POST['file'];
$originalFileName = $_POST['file'];
$keyName = basename($fileName);
//Creating a presigned URL
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $bucketName,
'Key' => "uploads/" . $keyName,
]);
$request = $s3->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$presignedUrl = (string)$request->getUri();
$img = basename($originalFileName);
// For this, I would generate a unqiue random string for the key name. But you can do whatever.
$keyName = 'uploads/' . $img;
$pathInS3 = 'https://<cloudfront_distribution_url>/' . $keyName;
$fileName = $pathInS3;
$degrees = 90;
$url = $fileName;
file_put_contents("../../../../uploads/" . $img, file_get_contents($url));
$source = imagecreatefromjpeg($fileName);
$rotate = imagerotate($source, $degrees, 0);
imagejpeg($rotate, "../../../../uploads/" . $img . "");
// Add it to S3
try {
// Uploaded:
$file = $originalFileName;
$s3->deleteObject(
array(
'Bucket' => $bucketName,
'Key' => $keyName
)
);
$s3->putObject(
array(
'Bucket' => $bucketName,
'Key' => $keyName,
'SourceFile' => "../../../../" . $file,
'StorageClass' => 'REDUCED_REDUNDANCY'
)
);
} catch (S3Exception $e) {
die('Error:' . $e->getMessage());
} catch (Exception $e) {
die('Error:' . $e->getMessage());
}
//here when I use presigned url it gives me back a url where the image has rotated but when using the $pathInS3 url which is the cloudfront url the image has not been rotated
echo $pathInS3;
on my cloudfront distribution I have created an invalidation.
All my images are stores in a folder called uploads/
so I created an invalidation for my folder uploads
which looks like this
uploads/
but this doesn't seem to work.
I then created an invalidation for everything
/*
also seems to not work.
Would love advice or help on how I can go about fixing this.
Thanks,
Arnav.
I'm using Intervention Image in my laravel app.
I upload a base64 string image through an ajax request, and then I convert that into a regular image.
$this->validate($request, [
'avatar' => 'required',
]);
$image_file = $request->get('avatar');
list($type, $image_file) = explode(';', $image_file);
list(, $image_file) = explode(',', $image_file);
$avatar = base64_decode($image_file);
Now, this works fine. In fact, if I go to save the image, it opens up just fine on my local like any other image. Great.
Storage::disk('public')->put($avatarPath.'/avatars/avatar.png', $avatar);
The problem is that when I try to pass the image into intervention image, like so
$image = Image::make($avatar)->fit(150, 150);
it just returns an image with all the properties being null. If I save the image, it just saves as a 0KB image with no data.
This does not return any errors.
Why is this happening?
Please, anyone, help me I want to set the Image Resolution to 300dpi for the uploaded image using Image Intervention.
Any alternate solution is also welcome.
What I want:
Upload Image -> Resize image to (100 X 100) -> Set image size to less than 30KB -> and set image resolution to 300dpi -> auto download
I have done everything else the resolution to my project... Here I'm sending the code and link ...
$width = 400;
$height = 200;
$file = $request->file('upload');
$image = Image::make($file)->resize($width, $height)->encode('jpg');
$headers = [
'Content-Type' => 'image/jpeg',
'Content-Disposition' => 'attachment; filename=utipanpsa_'.$request->type.'.jpg',
];
return response()->stream(function() use ($image) {
echo $image;
}, 200, $headers);
https://www.utipanpsa.com/cropping-tools
acording to document you can use imageresolution() if you have php >= 7.2 like this:
//sets the image resolution to 200
imageresolution($imageRecource, 200);
So I currently have an upload system working using AWS S3 to upload images.
Here's the code:
//Upload image to S3
$s3 = Aws\S3\S3Client::factory(array('key' => /*mykey*/, 'secret' => /*myskey*/,));
try {
$s3->putObject(array(
'Bucket' => "bucketname",
'Key' => $file_name,
'Body' => fopen(/*filelocation*/, 'r+')
));
} catch(Exception $e) {
//Error
}
This image can be a jpeg or png, and I want to convert it to a png before uploading. To do this I use:
//This is simplified, please don't warn about transparency, etc.
$image = imagecreatetruecolor($width, $height);
imagecopyresampled($image, $source, 0, 0, 0, 0, etc.);
So I have this $image object in memory.
I want to upload this to S3 without having to save it locally, upload it and then delete it locally; this extra step seems pointless. But I can't work out how to upload this $image object directly.
Any ideas how this would be done? I'd assumed fopen() would create an object of a similar type to imagecreatetruecolor(), but I've tried passing the $image object in and it doesn't work - whereas it does if I open an image locally with fopen().
You can capture the content of a GD image resource using output buffering:
ob_start();
imagepng($image);
$pngdata = ob_get_clean();
I'm not sure what I'm doing wrong, but PNG files uploaded to an S3 Amazon bucket show a white color instead of transparent, even though the local file does show transparency. Here is what I have:
$thumb = \PhpThumbFactory::create(
$content,
array(
'jpegQuality' => 100
),
true
);
$thumb->setFormat('PNG');
ob_start();
$thumb->show(true);
$content = ob_get_clean();
//This part is for testing purposes, I store the file locally to see that the transparency is there
$file = 'picture' . '-' . time() . '.png';
file_put_contents($file, $content); //The file created is an image which perfectly shows the transparent, as it should be
//The code below should upload the same file, but somehow it replaces transparency with a white color.
$S3 = new Zend_Service_Amazon_S3($myKey, $mySecret);
$response = $S3->putObject(
$bucket,
$content,
array(
Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER => 'image/png',
Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ
)
);
Am I missing something when doing the upload? Is there anything that I should configure on the bucket?