Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

remove duplicate images in post gallery except latest version WordPress

  • SOLVED

I am looking for options here because I am very bemused for solution :/

I can add more funds to the if someone has a solid function in mind that they can custom build :-)


<strong>The overview</strong>

I have multiple users using the post gallery - they are adding between 50-150 photos per post (only in the post gallery - not the post content)

I then output these post attachments in a custom attachment loop.

Some times the user uploads revisions of a original photo (with exactly the same file name), for example...

FMI5235_1.jpg (the original version)
FMI5235_11.jpg (the second version)
FMI5235_12.jpg (the third version)

...but they all had this same file name originally - FMI5235_1.jpg

They still all have the same title FMI5235_1

Every time the duplicate file gets uploaded to the post gallery, it adds a sequential digit onto the end of the file name.



<strong>The brief</strong>

I will add another $100 dollars to best solution. If this requires job requires more then please contact me.

Using a function combined with ajax, is it possible within the post editor - to click a button and remove duplicate images from the post gallery (not the media gallery) - leaving only the latest version of this image which would be FMI5235_11.jpg in my example above?

Also can these files be moved to the media library trash - <strong>not deleted them permanently?</strong>

Can this be done as function to go in my functions.php?

For example, if someone is able willing to build this a solid function, I would like the button to look and work like this... [[LINK href="http://i.imgur.com/k47iKxl.jpg"]](see screenshot)[[/LINK]]

Please see code that I stripped from submit box to build visuals, this might help you with html structure but obviously IDs will need to be updated to work with your ajax request etc.

See html layout gist... [[LINK href="https://gist.github.com/jcranny/4773454"]]https://gist.github.com/jcranny/4773454[[/LINK]]


Obviosuly like I said $20 is not my final price and I'm willing to update price to $120.


I understand this is big function. Maybe encapsulating it in a plugin might be better.


If someone does post excellent code up I will take a look at it and test, and will add more funds. If multiple code solutions go up will use the one I think is best and all prize fund will go to one person.


Thanks in advance.
Josh




Answers (2)

2013-02-13

Arnav Joy answers:

Hi Josh ,

I would like to help for this , I am preparing code may be i can get a solution for it.

-Arnav


Arnav Joy comments:

Hi Josh ,

here is the final code , please check and let me know if there is any issue.

http://pastebin.com/vx6BvUdm

2013-02-13

Christianto answers:

Hi Josh,

Is it the file name format would be same?
I think it would be hard to get which one is the original image if it not same.

For example if without underscore:
FMI52351.jpg
FMI523511.jpg
FMI523512.jpg

above we still can clearly see original image, but if:
FMI523511.jpg (is it original?)
FMI5235111.jpg (or this is original?)
FMI5235112.jpg
...
FMI52351111.jpg (11th revision)


To get the original image is important, so we can get all revision from the name correctly,
because only file name that show connection between images.

But if the file name convention of all images file in same format
in this case original would be [image-name] [_1.] [file extension].
Please try this code,
[[LINK href="http://pastebin.com/SmxUhknb"]]http://pastebin.com/SmxUhknb[[/LINK]]

I have to convert latest revision of image to original image name FMI5235_1.jpg,
If we keep the latest version file name for example FMI5235_11.jpg, and user need to upload same revision image,
new image will be named with original image name FMI5235_1.jpg instead of FMI5235_12.jpg

for trash option, I don't think WordPress providing option to "mark" media/image attachment as trash,
setting up force delete in wp_delete_attachment to false also not making trash option on media library page to appear.


Josh Cranwell comments:

Hi Christianto,

Nicely done - seems to be functional and working.

But not deleting images yet because of what you said maybe...

Please see code I am using [[LINK href="https://gist.github.com/jcranny/4944150"]]https://gist.github.com/jcranny/4944150[[/LINK]]

------

OK, I have an idea which I just tested.

I just uploaded this file 4 times... <strong>H07B0619.jpg</strong>

Which did this...

<strong>V1</strong>
Attachment ID: <strong>7</strong>
Attachment Title: <strong>H07B0619 </strong>
Attachment File name: <strong>H07B0619.jpg</strong>

<strong>V2</strong>
Attachment ID: <strong>8</strong>
Attachment Title: <strong>H07B0619</strong>
Attachment File name: <strong>H07B06191.jpg</strong>

<strong>V3</strong>
Attachment ID: <strong>9</strong>
Attachment Title: <strong>H07B0619 </strong>
Attachment File name: <strong>H07B06192.jpg</strong>

<strong>V4</strong>
Attachment ID: <strong>10</strong>
Attachment Title: <strong>H07B0619</strong>
Attachment File name: <strong>H07B06193.jpg</strong>


So I tested your function which did not recognised any duplicates.

I then deleted Attachments (ID) 8 + 10

I then uploaded the same file again... <strong>H07B0619.jpg</strong>

And this is what happened...

<strong>V5</strong>
Attachment ID: <strong>11</strong>
Attachment Title: <strong>H07B0619</strong>
Attachment File name: <strong>H07B06191.jpg</strong>

Can you see what happened?

The attachment ID does not reset, it continues to grow incrementally.... 7,8,9,10,11

So maybe we can use the attachment ID as the version comparison? Instead of the file name.

And use the attachment Title to find the duplicates.


Thanks
Josh


Christianto comments:

Oh yes, nice shot Josh,
I didn't realize that each image title is same.. :)

For attachment id, its incrementally based on attachment creation,
so if we upload other image instead of H07B0619.jpg after deletion,
the id would grow as well.

I will edited my answer so it won't need specific filename format .






Christianto comments:

I've updated my code,
Please check this:
[[LINK href="http://pastebin.com/SmxUhknb"]]http://pastebin.com/SmxUhknb[[/LINK]]

Thanks