⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Callback after delete image



T.O.M.
  • profile picture
  • Member

Posted 29 May 2015 - 08:16 AM

Hello everyone!

 

I need a field for upload image. When file is uploaded I need to save original image and resize this image. Plus I need to save original and resized images with watermark.

 

I know how to set upload_image field:

set_field_upload($field_name, $upload_path);

I know how to resize image and set watermarks after upload:

callback_after_upload(array($this, 'callback_after_upload'));

But how to delete all images when original image is deleting? There are no callback function something like callback_after_delete_image().


Paul Savostin
  • profile picture
  • Member

Posted 02 June 2015 - 12:15 PM

Hi.

You have few options

1)you can save all images in folder with name = ID of row, and after delete record - delete all folder
2)if option 1 not for you then in after upload new image (or before update all row) delete all images in particular folder with ID that not match new one

in anycase in callback before update record you can know if this is new image or it not changed

Or i missing smth?


Amit Shah
  • profile picture
  • Member

Posted 02 June 2015 - 17:27 PM

Well Paul - interested in creating a Recycle Bin option for the image files that gets deleted with a facility to restore them post its deletion?


T.O.M.
  • profile picture
  • Member

Posted 03 June 2015 - 06:42 AM

Thanks for your help.

 

Paul, option 1 is not very good for me, because I have fixed directory structure for images. And you described only delete record event, but on edit page when old image is deleting and new image is uploading I also need to delete all old images exactly when image is deleting.

Option 2 is fine but with little addition:

1) in callback before update - to check if image name in post equal with image name in database, if not - delete old images

2) in callback before delete - to get image name from database and delete images


Paul Savostin
  • profile picture
  • Member

Posted 03 June 2015 - 10:10 AM

Thanks for your help.

 

Paul, option 1 is not very good for me, because I have fixed directory structure for images. And you described only delete record event, but on edit page when old image is deleting and new image is uploading I also need to delete all old images exactly when image is deleting.

Option 2 is fine but with little addition:

1) in callback before update - to check if image name in post equal with image name in database, if not - delete old images

2) in callback before delete - to get image name from database and delete images

Yeah, in 2) option you are right, I do the same, looking for old images, compare with new one, and decide which images need to insert and which - to delete.
And offcourse before delete we get all images which we need to delete. Thats right, thanks!


Paul Savostin
  • profile picture
  • Member

Posted 03 June 2015 - 10:27 AM

Well Paul - interested in creating a Recycle Bin option for the image files that gets deleted with a facility to restore them post its deletion?

Hi again Amit! May I clarify - are you mean images for some kind of post or image gallery?


Amit Shah
  • profile picture
  • Member

Posted 04 June 2015 - 09:44 AM

well this can be any images...

infact this can be a part integrated into the grocerycrud itself ... where if the images are deleted - it lands up into the trashbox instead of it getting deleted. 

There can be setting - where if the user cancels and the image was deleted, it can be retrieved / restored from the trashcan. 

All can be part of the configuration option whether user want to preserve even after delete or delete post deletion of the record. This - i know is a tough nut but will be very tasty if we could crack it.


Paul Savostin
  • profile picture
  • Member

Posted 04 June 2015 - 11:13 AM

Hi Amit! Honestly i dont see benefit to restore any image for me.

In any case I write my own implementation of downloading images, in short

1)Create custom field 'gallery' where I get from database images if row in UPDATE mode (in INSERT it will be just button 'Download'), and functionality to upload images (using jquery download plugin),
input with array images like <input type=file name=images[]>

2)Create my own TMP folder

3)When click DOWNLOAD - images uploads to my tmp and add one more input to 'images'

4)In callback after insert - I get array of uploaded images, create folder for certain row with name = ID, and move images in array 'images' to this new folder

   IF it was update row, in callback_after_update:

   - get new image array (it may contains old and new images, or empty)

   -  get OLD image array from DB, compare with NEW one

   -  insert new, delete old or delete all if new image array is empty

And if I press DELETE (link i generate in gallery callback field for each image), I just remove image, not delete! All operation will be accept only if press SAVE or SAVE AND BACK TO THE LIST

5)my TMP folder clean files by cron job in a few hours, looking for files that placed in TMP one hour or more

 

Sor for my awful english, hope you understood!


Amit Shah
  • profile picture
  • Member

Posted 05 June 2015 - 11:01 AM

nice - will look and work around!!


T.O.M.
  • profile picture
  • Member

Posted 05 June 2015 - 14:03 PM

Can you help me - when uploading file how to save into DB not original filename, and some hash-name?
Because I have some trouble: user uploaded image with name "picture_(1).jpg". When he tries to delete image (by clicking "Delete" button) - image is not deleting in result.
The reason is GET-request ".../delete_file/photo/picture_(1).jpg"
It gets error - The URI you submitted has disallowed characters.


Paul Savostin
  • profile picture
  • Member

Posted 05 June 2015 - 16:25 PM

Can you help me - when uploading file how to save into DB not original filename, and some hash-name?
Because I have some trouble: user uploaded image with name "picture_(1).jpg". When he tries to delete image (by clicking "Delete" button) - image is not deleting in result.
The reason is GET-request ".../delete_file/photo/picture_(1).jpg"
It gets error - The URI you submitted has disallowed characters.

This error occurs because name of your picture contains disallowed characters!

To resolve this you need to change in you app config/config.php $config['permitted_uri_chars'] option. Include characters that you need and that is it.

More detailes here http://stackoverflow.com/questions/19297433/codeigniter-redirect-the-uri-you-submitted-has-disallowed-characters

 

Or you can using callback_before_upload http://www.grocerycrud.com/documentation/options_functions/callback_before_upload


T.O.M.
  • profile picture
  • Member

Posted 08 June 2015 - 07:09 AM

Paul, yes I know that image name has disallowed characters. But I can't to allow this character - it is not good. Yes, it will help this time but there are may be other disallowed characters. And this error will repeat again with every such character. And I can't to allow all characters.
Best way - to rename uploaded image. That was my question - is it possible to rename uploaded image by groceryCRUD options?

Callback_before_upload is not exactly what I wanted, but I can add a simple filename check and show error message if filename contains such characters.
Somehow I did not thought about this function :)

Thanks a lot!


Paul Savostin
  • profile picture
  • Member

Posted 08 June 2015 - 10:52 AM

T.O.M. if you want just change upload filename to be unique than in library Grocery_crud.php you have private function "trim_file_name" on line 5368 where you can generate unique name of you upload file.

This is bad to change core but you will get want you want simple I think :)


T.O.M.
  • profile picture
  • Member

Posted 08 June 2015 - 13:08 PM

I agree, it's not very good, but it will be correct. I will not need to check filename and write callback_before_upload in each upload. I don't matter at all what is filename in uploaded image - I just will set unique name to this file.

 

Thanks again :)


Paul Savostin
  • profile picture
  • Member

Posted 08 June 2015 - 13:36 PM

I agree, it's not very good, but it will be correct. I will not need to check filename and write callback_before_upload in each upload. I don't matter at all what is filename in uploaded image - I just will set unique name to this file.

 

Thanks again :)

 

You're welcome