That sounds like a really bad solution, why not just create a table that contains the image name and the batch, then use the incrementing id as the counter.
Uploaded files randomly get same position number
Hello, I am using filepond to upload images combined with intervention/image. For sorting images I need every uploaded image to have position incremented for 1 (position is column in database). So I have them starting from 0 and they keep incrementing. Problem is sometimes they get same position number, for example 0 1 2 3 3 4 5 6 and if I upload 60 images at once it will happen few times, i might have double 11, double 34 etc, and every time it happens at different numbers, even 0 0 1 2 3 was once. I can see in db that if I have 60 records my max position is 55 or 52, its different number every time. I hope I managed to explain the problem clearly, appreciate any idea or solution how to solve or troubleshoot this.
public function filepondUpload(Property $property)
{
if (request()->hasFile('image')) {
// get max position
$max = $property->images()->max('position') ?? -1;
// file name without extension
$file_name_without_extension = uniqid('', true) . time();
// validate input files
$validated = request()->validate([
'image' => ['image', 'max:5120'],
]);
$file = $validated['image'];
// read uploaded image
// convert to webp with compressed quality of 90%
// save image and add .webp extension to file name
$manager = new ImageManager(new Driver());
$image = $manager->read($file);
$watermark = false;
if ($watermark) {
$image->place('img/logo100.png', 'bottom-left', 40, 40, 90);
}
$image = $image->encodeByMediaType('image/webp', quality: 90);
Storage::disk('public')->put('properties/' . $property->id . '/' . $file_name_without_extension . '.webp', (string) $image);
// read uploaded image
// transform image to 300x300
// convert to webp with compressed quality of 90%
// save image and add .webp extension to file name
$manager = new ImageManager(new Driver());
$thumbnail = $manager->read($file);
$thumbnail = $thumbnail->cover(300, 300);
$thumbnail = $thumbnail->encodeByMediaType('image/webp', quality: 90);
Storage::disk('public')->put('properties/' . $property->id . '/thumbnails/' . $file_name_without_extension . '.webp', (string) $thumbnail);
// Save to db
Image::create([
'property_id' => $property->id,
'name' => $file_name_without_extension . '.webp',
'position' => $max + 1,
'public' => 1,
]);
// filepond response
return $file_name_without_extension;
}
}
Please or to participate in this conversation.