Laravel Image source not readable error - laravel

I am working on uploading a video locally and then use it locally to upload to s3. It successfully uploads to s3 but I get this error:
Image source not readable
/var/www/yt/vendor/intervention/image/src/Intervention/Image/AbstractDecoder
I found this error when I was trying to delete the image after I uploaded it to s3. I think it is the reason the delete does not work. How do I fix the error?
public function avatar(Request $request)
{
$imageData = $request->get('image');
$img = Image::make($request->get('image'))->fit(300)->encode('jpg');
// calculate md5 hash of encoded image
$hash = md5($img->__toString());
// use hash as a name
$path = "avatars/{$hash}.jpg";
// save it locally
$test = $img->save(storage_path($path));
// $url = "/images/{$hash}.jpg"
$url_local = $path;
$path = Storage::putFile('avatars', new File(storage_path($url_local)));
if($path){
//TODO make into event
Storage::disk('local')->delete(storage_path($url_local));
// \Debugbar::error($test);
}
$user = User::find(Auth::id());
$user->avatar_url = $path;
if ($user->save()) {
return response()->json(['avatar' => $user->gen_avatar()]);
}
}

Related

How to choose were Image Intervention should store the images?

Im trying to save a resized copy of an image that has been uploaded, but Image Interventions doesent seem to know in what root directory to begin in, or maybe it is just me that dosent know how to configure it propertly.
This is my code in AvatarImageController#store
public function store(Request $request, Game $game)
{
if ($request->hasFile('game_image')) {
$request->validate([
'game_image' => 'mimes:jpeg,png|max:1014'
]);
$file = $request->file('game_image');
$filename_thumbnail = "thumb_". $file->hashName();
$path_full = $request->file('game_image')->store('images/test_folder');
$path_thumb = $request->file('game_image')->storeAs('images/test_folder/thumbs', $filename_thumbnail);
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
$image = new Image;
$image->full = basename($path_full);
$image->thumb = $filename_thumbnail;
$game->images()->save($image);
return back()->with('success', "Success!! Image uploaded.");
}else{
return back()->with('success', 'Ooops.. something went wrong.');
}
abort(500, 'Could not upload image :(');
}
The error i receive:
Intervention\Image\Exception\NotReadableException
Image source not readable
I suspect this is because im not in the correct path, because of these lines from above function:
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
And it is a real hassel working with explicit paths when using git as deployment.
You can make image directly like this:
$uploadedFile = $request->file('game_image');
$image = Image::make($uploadedFile);
Resize it:
$image = $image->resize(300, 200);
and save it like this:
$image_data = $image->encode('jpg')->__toString();
$relative_file_path = 'images/test_folder/';
Storage::put($relative_file_path, $image_data)
use the storage() helper?
Intervention::make(storage_path('app/public/") . $path_thumb)

How to save formdata and this URL into the database .. I can upload the file to the cloudinary

Hiii , I have this formdata with few data and files. I want to store them into my database table.
this is my controller to store into the UserApplyJob model
public function store(Request $request)
{
$file = $request->file('resume');
Cloudder::upload($file->getRealPath(), null, ['resource_type' => 'raw']);
$publicId = Cloudder::getPublicId();
$url =Cloudder::show($publicId, ['resource_type' => 'raw']);
$requestJob = UserApplyJob::create($request->all());
}
You're not persisting it to the db. Try this at the end.
$requestJob->save();
This is from laravel 7.14
if($request->hasFile('cover_image'))
{
// Get the file with extension
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
//Get the file name
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get the ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
//File name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
}
// Save the image to column
$blog->cover_image = $fileNameToStore;

Image source not readable : Image Upload in Laravel

I'm trying to upload an image to our system. It is working well when I'm using it in localhost but when I'm testing in server, it's throwing an error Image source not readable. I'm trying to upload a 2.2MB photo since my max size of image upload is 3MB. Uploading images less than or equal to 2MB in server has no problem but in local, it accepts up to 3MB in which it is the expected behavior. I'm using docker for my local development and my server is CentOS7. Is there some configurations I should touch so that it can accept 3MB image size in my server? Or is there something I have to change in my code below in processing the image?
REQUEST
public function rules()
{
return [
'photo' => 'image|mimes:jpeg,png,jpg|max:3000',
];
}
Controller
$photo = $request->file('photo');
$server_dir = storage_path(config('const.upload_local_temp_path'));
FileHelper::addDirectory($server_dir, 0777);
$file_name = FileHelper::makeUniqFileName($photo->getClientOriginalExtension(), $server_dir);
$filepath = $server_dir . $file_name;
$img_path = FileHelper::storeResizeImg($photo->path(), $filepath, null, 300);
$img_url = FileHelper::getPublicPath($img_path);
return $img_url;
FILEHELPER
/*
*
*/
public static function addDirectory($directory, $mod)
{
if (!File::exists($directory)) {
File::makeDirectory($directory, $mod, true);
}
}
/**
*
*/
public static function storeResizeImg($file, $filepath, $width, $height)
{
$org_img = Image::make($file);
$org_img->orientate();
$org_img = $org_img->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$org_img->save($filepath);
return $filepath;
}
public static function makeUniqFileName($ext, $path)
{
$file_name = '';
while (1) {
$file_name = sha1(rand().microtime()).'.'.$ext;
if (!File::exists($path. $file_name)) {
break;
}
}
return $file_name;
}
UPDATE
Error message that the image size is greater than 3MB is working in my local, but in server it says : The given data was invalid.
Please let me know of your thoughts.

Storing an HTMLCanvasElement as a JPG in Laravel

I have integrated a cropping script that returns either an image or an HTMLCanvasElement in my Vue Component. In my component's crop method, I then make an axios post call to my upload function. Here is the crop method:
crop() {
var croppedCanvas = this.cropper.getCroppedCanvas()
var img = croppedCanvas.toDataURL('image/jpeg', 1.0);
var formData = new FormData();
formData.append('image', img) // I can send croppedCanvas here too if I want
axios.post('/api/upload', formData)
}
Now the problem. I can't seem to save the HTMLCanvasElement or the Image as a JPG file. I tried so many different approaches (toDataURL, toBlob, etc.) and have failed in every single one. My latest implementation was as follows:
public function upload(Request $request) {
$input = $request->all();
$img = $input['image'];
Storage::put('picture.jpg', $img);
return response(Response::HTTP_OK);
}
This saves a 26 byte file in the storage/public folder which is unaccessible.
Can someone please help with how to go about this.
I simply want to save the HTMLCanvasElement as a JPG file in my storage/public folder.
Thanks.
Actually as mentioned in the docs over here : https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
toDataURL method returns back a base64 encoded data so you have to decode it on the server side
you can use the base64_decode method in laravel like this
public function upload(Request $request) {
$img = $request->image;
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
Storage::put('picture.jpg', $data);
return response(Response::HTTP_OK);
}

Setting a config value dynamically in the controller in Codeigniter?

I'm trying to upload an image using Codegniter's Upload Library, the image should be uploaded to a directory which is created and returned by the model, do the upload directory changed every time, this is the code I'm supposed to use to change the config upload array:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload');
$this->config->set_item('upload', array('upload_path' => $path));
// I've also used
// $this->config->set_item('upload_path', $path);
if (!$this->upload->do_upload('image'))
echo $this->upload->display_errors();
else {
$image = $this->upload->data();
$data['image'] = $image['file_name'];
}
}
but it doesn't seem to work. The upload_path doesn't change, the image is still uploaded in the path I've specified in config/upload.php file.
Try this:
if ($_FILES['image']['size']!=0) {
$path = $this->homeModel->createDirectories($id);
$this->config->load('upload', TRUE);
$config = $this->config->item('upload');
$config['upload_path'] = $path;
$this->upload->initialize($config);
//etc.
}

Resources