I am using the following code to upload image content to the ECS server
$result = $s3->putObject(array(
'Bucket' => $this->bucket,
'Key' => $Destination_folder,
'Body' => $image_content
));
Earlier I was storying the image to a temporary location and then convering it into 3 images of different size (using Imagick) and then uploading those images individually.
Now I am able to upload the image directly using the image content and I also figured out how to resize the image using the image content. Following is the code for that using Imagick
$imagick->readImageBlob($imageBlob);
$res = $imagick->scaleImage(10, 10, true);
$writeSuccess = $imagick->writeImage("abc-m.jpeg");
But now I want to upload the image directly to the ECS server after resizing but without using temporary loaction.
How can I uplaod the resized image from Imagick to ECS server?
use getImageBlob()
$image_content = $imagick->getImageBlob();
Now upload this image content to the server.
Related
I have a form with a file upload where a random user can upload his profile picture, the goal is trying to crop the image by a 150x150 and save it in the database using the following code:
$formFields = $request->validate([
'name' => ['required','min:3'],
'password' => 'required|confirmed|min:6'
]);
if($request->hasFile('picture')){
$image = $request->file('picture');
$formFields['picture'] = $image->store('profiles','public');
Image::make(storage_path($formFields['picture']))->crop(150,150)->save();
}
Everything other than the cropping method is working correctly, the image is stored in the database and added into the storage/profiles folder, however when I write the code line to crop it, I get the following error: Image source not readable
I tried different approaches like cropping the image before storing it using the following code:
if($request->hasFile('picture')){
$image = $request->file('picture');
$resized_img = Image::make($image)->crop(150,150);
$formFields['picture'] = $resized_img->store('profiles','public');
}
but that resulted in another error: Command (Store) is not available for driver (Gd).
How can I save a cropped image?
I am trying to write a script to upload bulk product from excel sheet which contain product name, price and images. Images are the dropbox image share link. How can I download those image from the dropbox url, save them on my server, then upload the image url to my database?
Excel sheet reading:Maatwebsite/Laravel-Excel
Generic upload code:
public function productUpload(Request $request){
if($request->hasFile('products')){
$path= $request->file('products')->getRealPath();
$data = Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
//download the image create thumbain and store to /images/product/thumbnail folder and get the link,
$thumbnail = //here will be the path for the thumbnail
//Original image
$original = ;
$data['original']= $original;
$data['thumbnail']=$thumbnail;
$data['name']=$value->name;
$data['price']= $value->price;
Product::create($data);
}
return redirect()->back()->with('success','Product has been uploaded');
}
}
}
The image url that is in the excel sheet is like this one https://www.dropbox.com/s/x2tbsy49sraywvv/Ruby12.jpg?dl=0 This file has been removed at this point.
You can dowload the image directly by adding dl=1 to the dropbox link Source
And you can dowload the image to the server with the file_get_contents php command. Source and store it in the server with the Storage Facade
$url = "yoururl"; //check if has the dl=0 and change it to 1 or add dl=1
$contents = file_get_contents($url);
$name = 'image.png';//the name of the image to store on disk.
\Storage::disk('yourdisk')->put($name, $contents);
Also you will get the full path to the image like this.
$thumbnail = storage_path('yourdir/'.$name)
Tested it with a file that i have on my dropbox and worked without a problem.
You still need to handle some things like errors and delays while dowloading the images.
So I have a scenario where I have to upload a cropped image to AWS.
First, I have the basic image upload working (AWS putbucket and all) so that's not the issue.
I also have the cropping of the image working (using imgAreaSelect) so that is also not the issue.
On the PHP side I also grab the image from the $_FILES['file']['tmp_name'] and create a new cropped image (using code similar to found at http://www.codeforest.net/how-to-crop-an-image-using-jquery-and-php.
However I need a way to grab the new image created on the last line
imagejpeg($new, $new_filename, 95);
into $_FILES['file']['tmp_name'] in the aws upload here
$s3->putObject(array(
'Body' => fopen($_FILES['file']['tmp_name'], 'r'),
));
So:using GD
ob_start(); // start a new output buffer
imagejpeg( $dstImage, NULL, JPEG_QUALITY);
$resizedJpegData = ob_get_contents();
ob_end_clean(); // stop this output buffer
// free up unused memmory (if images are expected to be large)
unset($srcImage);
unset($dstImage);
// your resized jpeg data is now in $resizedJpegData
// Use your Undesigned method calls to store the data.
// (Many people want to send it as a Hex stream to the DB:)
$dbHandle->storeResizedImage( bin2hex($resizedJpegData) );
'Body' => fopen($dbHandle, 'r'),
I am using the Amazon SDK for PHP and wideimage. I am resizing an image with wideimage and trying to then upload that resized image to Amazon S3.
$resized = $image->resize($width,$height);
upload
$response = $s3->create_object($myBucket, $newFilename, array(
'fileUpload' => $resized, //this does not work
));
Does anyone know the proper way to do this?
You can use a stream wrapper and use WideImage's saveToFile method. There are many stream wrappers for S3, this is one example: https://github.com/jakajancar/S3StreamWrapper.
You don't need to save an image and then upload from there.
When you resize the image, you have to convert to a string. You can do that with WideImage class.
Example:
$image = WideImage::load($_FILES["file"]['tmp_name']);
$resized = $image->resize(1024);
$data = $resized->asString('jpg');
And then when you're uploading on Amazon, you have to use the param 'body' instead of 'fileUpload'.
Example:
$response = $s3->create_object($myBucket, $newFilename, array(
'body' => $data,
));
I hope that helps.
I would like to point out few things might help someone in making choice.
First of all, I think you better go with what you are trying to do first resize image over your server and then move it to Amazon because suppose if there is some kind of way to resize and upload image at same time on the fly then your script will perform slow because script will have to resize and save it to server which is far away destined. It would be minor if there are few images but can be a problem when it's huge resizing even on high speed bandwidth and as PHP will not be able to release its resources used for image resizing until it has not completely saved the target image.
Second that if you are using a CDN (Content Delivery Network) then CDN uses PULL SERVER technique means that we do not push static content to CDN server but when a user/client ask for static content then CDN first checks its entire servers and if not found then it asks our main server for that.
Amazon S3 is not a true CDN. S3 was designed for content storage. The correct Amazon service to use for content delivery is Amazon CloudFront. And if we are saving files to any of our files to any storage server or CDN then that's called PUSH SERVER
A thorough article can be read on http://www.binarymoon.co.uk/2010/11/timthumb-cdn-amazon-s3-good/. That's actually about TimThumb but worth a good knowledge.
I ended up saving the file to the server and then uploading the file from there. If there is a better way then please let me know.
I am working on building gallery where the user uploads all the images. I had tried to use GD originally but found that it used way too much memory when dealing with images from a digital camera. So I have been looking into ImageMagick and ran into this problem.
My end goal is to resize the image and then upload it. I am not sure if this is possible with ImageMagick or not. I have gotten it to resize the image after upload but it doesn't save the resized image, just the original size.
This is the code I am currently using: ($image is the path to the file on my server)
$resource = NewMagickWand();
MagickReadImage($resource,$image);
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
Any input would be appreciated,
Levi
Your code will send the modified image to the client (the web browser), but it will not save it to the server (replacing the original image, for example)
To save the image, use:
MagickWriteImage( $resource, 'new_image.jpg' );