Images not showing in update function using BackPack for Laravel - laravel

I´m saving images in my crud. The url is being stored in the DB correctly, however the image is not appearing when I try to edit the element. The image shows like this:
I'm using this mutator:
public function setImageAttribute($value)
{
$evidencia = "image";
$disk = "public";
$destination_path = "public/uploads/evidenciaQuejas";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$evidencia});
// set null in the database column
$this->attributes[$evidencia] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \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, $image->stream());
// 3. Save the path to the database
$this->attributes[$evidencia] = $destination_path.'/'.$filename;
}
}
Also, the folder that I created in the server to store the images ("public/uploads/evidenciaQuejas") says it has 0 archives inside even though the image is being stored without errors.
Could you help me please?

Related

how to use database saved links of image to get images from laravel folder

i have saved images in 'public/images' folder in Laravel and the link/name is saved in mysql database. all i want to get all images via api call. the problem is i can get a single image easily but couldnt get all the images.[[[enter image description here](https://i.stack.imgur.com/9gerk.jpg)](https://i.stack.imgur.com/MIBIk.jpg)](https://i.stack.imgur.com/QcPQm.jpg)
i have no issue with single image however unable to get all at once. thanks
Try this instead
Store the only image name and extension into database and then retrieve it by using DB and use asset to get image, refer the following code as practice.
$custom_data = array();
for ($i = 0; i <= $total_images; $i++) {
$image_name = $data[$i]->image;
if ($image_name != "") {
$image_with_path = asset('public/products/') . '/' . $image_name;
} else {
$image_with_path = "";
}
$custom_data['image'] = $image_with_path;
}

delete old image while updating the field

I am doing CRUD operations for images. when I am doing the update operation image gets updated but the old image doesn't delete. The following code updates only the file name in the database but I need to remove the old image from the destination folder too while updating else the folder size will become too large. Any ideas would be great. Here my code.
<input type="file" name="profileimage" value="{{ isset($editabout) ? $editabout->profileimage : '' }}" class="custom-file-input" id="exampleInputFile">
Here is my controller
public function update(updateAboutusProfile $request, AboutUs $about)
{
$data=$request->only(['profiletext']);
if($request->hasFile('profileimage')){
$profileimage = $request->profileimage->store('aboutus', 'public');
$oldimage = $about->profileimage;
Storage::delete($oldimage);
$data['profileimage'] = $profileimage;
}
$about->update($data);
toastr()->success('Profile updated successfully');
return redirect(route('about.index'));
//
}
What can be the error I need to resolve,
thank you
Try setting the disk you used to store the file
Storage::disk('public')->delete($oldimage);
Delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Laravel file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
Enjoy ;)
You should save the path of the file to the database and then simply remove it using \Storage::delete() facade.
Store image using hash name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->store($store_path,'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->hashName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Store image using original name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->storeAs($store_path, $uploadedFile->getClientOriginalName(),'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->getClientOriginalName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Delete the old image while updating
public function update(Request $request, $id)
{
// other updates
if ($request->hasFile('image')) {
//delete old image if exist
if (File::exists(public_path($oldImage))) {
File::delete(public_path(oldImage));
}
//new image process
}
}

How to load List<Asset> from url with Flutter multi_image_picker: ^4.7.14

I am using Flutter multi_image_picker: ^4.7.14 to upload selected photos to server.
It stores images List, In my app new insert part is ok, I am getting the selected images and showing them in a gridview and sent them to the server.
But when I try to update my record, I don't understand how to get my record's images and set them to List. I have the url path of my uploaded images for that record. I want to get images and convert them to List because multi_image_picker is only accepting Asset, but there is no obvious explanation in multi_image_picker read me how to do this.
var images = List<Asset>;
for (var i = 0; i < tempList.length; i++) {
log("image link:" + ad_url + tempList[i].imageDownloadUrl);
final ByteData imageData = await NetworkAssetBundle(
Uri.parse(ad_url + tempList[i].imageDownloadUrl))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
Image img = Image.memory(bytes);
// in this part I want to convert img to Asset , but I don't know how to do this.
Asset asset = img.... //How can I do this conversion
images.add(asset);
}

ABCPdf - Image not a suitable format

In the end, my goal is to send a raw image data from the front-end, then split that image into however many pages, and lastly send that pdf back to the front-end for download.
But every time I use the theDoc.addImageFile(), it tells me that the "Image is not in a suitable format". I'm using this as reference: https://www.websupergoo.com/helppdfnet/source/5-abcpdf/doc/1-methods/addimagefile.htm
To troubleshoot, I thought that the image might not be rendering correctly, so I added a File.WriteAllBytes to view the rendered image and it was exactly what I wanted, but still not adding to the PDF. I also tried sending the actual path of a previously rendered image thinking that the new image might not have been fully created yet, but it also gave me the same error. Lastly, I thought PNGs might be problematic and changed to JPG but it did not work.
Here is the code:
[HttpPost]
public IActionResult PrintToPDF(string imageString)
{
// Converts dataUri to bytes
var base64Data = Regex.Match(imageString, #"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
var binData = Convert.FromBase64String(base64Data);
/* Ultimately will be removed, but used for debugging image */
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string imgName= "Test.jpg";
string filename = Path.Combine(path, imgName);
System.IO.File.WriteAllBytes(filename, binData);
/***********************************************************/
using (Doc theDoc = new Doc())
{
// Using explicit path
theDoc.AddImageFile(#"C:\Users\User\Documents\Test.jpg", 1);
// Using variable
//theDoc.AddImageFile(filename, 1);
// What I really want
//theDoc.AddImageFile(binData , 1);
theDoc.Page = theDoc.AddPage();
theDoc.AddText("Thanks");
Response.Headers.Clear();
Response.Headers.Add("content-disposition", "attachment; filename=test.pdf");
return new FileStreamResult(theDoc.GetStream(), "application/pdf");
}
}
Try something like this (not tested, but cleaned up from my own code):
public int AddImageFile(Doc doc, byte[] data, int insertBeforePageID)
{
int pageid;
using (var img = new XImage())
{
img.SetData(data);
doc.Page = doc.AddPage(insertBeforePageID);
pageid = doc.Page;
doc.AddImage(img);
img.Clear();
}
return pageid;
}
To add a JPEG from a byte array you need Doc.AddImageData instead of Doc.AddImageFile. Note that AddImageFile / AddImageData do not support PNG - for that you would definitely need to use an XImage. The XImage.SetData documentation has the currently supported image formats.

How to save a GD created image to an object in Silverstripe 4

I am working on a project in Silverstripe 4. On page save/ publish I want to create a "MetaImage" which is made up of random tiles images associated with the page.
The MetaImage is created by GD - I'm then trying to save it into the ORM. Here's some example code:
public function onBeforeWrite()
{
$metaImage = imagecreatetruecolor(1200, 1200);
//GD image created here
$fileName = 'metaimage-'.$this->ID.'.jpeg';
$saveFolder = Folder::find_or_make('MetaImages');
imagejpeg($metaImage, 'assets/MetaImages/'. $fileName);
$metaObjectImage = new Image();
$metaObjectImage->ParentID = $saveFolder->ID;
$metaObjectImage->Filename = $fileName;
$metaObjectImage->write();
$this->MetaImage()->$metaObjectImage;
imagedestroy($metaImage);
parent::onBeforeWrite();
}
All I get is a red box in the "Files" section of the CMS with "File cannot be found".
How do I associate the created GD image with $metaObjectImage?
How can I make it that if "metaimage-6.jpeg" already exists it's replaced by a new copy of the file?
This was my solution using setFromLocalFile:
$saveFolder = Folder::find_or_make('MetaImages');
$fileName = bin2hex(random_bytes(32)).'.jpeg';
$tmpFolder = 'assets/'.$saveFolder->Title;
imagejpeg($metaImage, $tmpFolder.'/'.$fileName);
$metaObjectImage = new Image();
$metaObjectImage->setFromLocalFile($tmpFolder.'/'.$fileName);
$metaObjectImage->ParentID = $saveFolder->ID;
$metaObjectImage->write();
$this->MetaImage()->$metaObjectImage;

Resources