sahusoftcom/eloquent-image-mutator

Removing images

jahsome opened this issue · 8 comments

Use case: I need to replace an image/set of images for a particular model (e.g. a user updates his/her profile photo)

Current functionality: If a user uploads a new photo, there is currently no way to delete the existing photo, and thusly the uploads directory contains unused photos, potentially taking up disk space or impacting S3 storage costs.

Desired functionality: An API method to delete existing photo(s) and potentially a replacement method for the Eloquent model to automatically remove the previous photo(s) and store the new photo(s).

@jake-harris I had got this covered strange it didn't work. Will again test it and get back to you. Thanks for the heads up.

@jake-harris @mrigankmridul We can always do this $user->profile_picture->delete();

Because we have the delete() method on the ImageMutator object. Am I wrong?

@sanketsahusoft Correct we do have delete. but we also have a delete method on updated event, should have worked.

That's odd, I must have missed the delete method when I was browsing the API. It wasn't in the documentation so I barely skimmed the trait.

I'm also getting a second set of images upon save with a completely different timestamp, so it's possible I configured something incorrectly... I'll dig a little bit and let you know.

@mrigankmridul Jake is saying that there doesn't exist a way to delete the existing one, because I think it's not documented, though it exists.

@jake-harris If you replace the image with some other image and update the eloquent object, it deletes the existing one before adding the new one. And you can also explicitly delete the existing images by calling delete() method on the ImageMutator object. Let us know.

Thanks!

@sanketsahusoft yup didnt get time to do that, need to update the document (multiple new features), will update it once i am done with the s3 uploads.

@jake-harris Just tested it. Its working although its not deleting the folders, but the images are deleted. Also have integrated url uploads, just provide the image url and the trait would download the image and store it.

I appreciate the help gentlemen, thanks for your quick responses.

FYI all of the issues I was having seem to be related to my image field being of the type varchar(255). With a text type, everything is functioning as you described with the auto-deletes and the delete method. The text field was truncating the serialized object.

This is a great piece of work, and I think I could put it to use once #1 gets resolved.

@jake-harris Thank you for the appreciation, we will get #1 resolved soon. Actually its just a modification of the upload folder structure, wont refer it as an issue ;)