I am using laravel backpack for my backoffice and i need to upload some images, i copy the code from their website and works fine, but i need the original name on the images, i tried some things, but is not working.
What i need to change here?
public function setImageAttribute($value)
{
$attribute_name = "image1";
$disk = "uploads"; // or use your own disk, defined in config/filesystems.php
$destination_path = "productimg"; // path relative to the disk above
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image1 = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image1->stream());
// 3. Delete the previous image, if there was one.
\Storage::disk($disk)->delete($this->{$attribute_name});
// 4. Save the public path to the database
// but first, remove "public/" from the path, since we're pointing to it from the root folder
// that way, what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst('uploads/', '', $destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
}
}
you need to replace this
$filename = md5($value.time()).'.jpg';
with this
$filename = $value->getClientOriginalName();
but you have to be sure that all your images has an unique name or you must add a random prefix to file name to prevent duplacated names
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.