Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating "Save Image As" and "Copy Image To Clipboard" buttons for a gallery

Engaged ,
Mar 04, 2023 Mar 04, 2023

Just realized I've never had to implement a "Save Image As" or "Copy Image To Clipboard" button... and now I need to do both for a series of images in a gallery-style webpage.

 

What's the best way to implement both buttons next to an image? Can this be done with HTML alone? Unless there's an existing script out there that will overlay the image with a prompt to Save or Copy if you click it. Those are the two simplest layouts I can think of at the moment.

 

Either way, whatever is easiest to implement would help me out.

 

Thanks!

 

PS: If it makes a difference, my site isn't currently using any JS (just turned out that way, not a conscious decision) but does use PHP to parse includes and variables (which is the extent of my PHP knowledge) in case this influences recommendations in any way.

1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

No need to reinvent the wheel for this.

"Copy image" & "Save Image as" are built-in to your browser's right-click context menu.   See screenshot. 

image.png

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Try the 'download' attribute on the link as in

<img src="/images/myimage.jpg" alt="myimage">
<a download="myimage.jpg" href="/images/myimage.jpg">
    downkoad
</a>

 

Change the image name and path to suit.

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 05, 2023 Mar 05, 2023

So there's a built-in "download" attribute I was unaware of, that will be helpful. Thanks. But this only takes care of the SAVE, right? Is there an equivalent attribute for "Copy To Clipboard"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2023 Mar 05, 2023

The Clipboard API

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 05, 2023 Mar 05, 2023

Thinks, but I wish I knew the first thing about what I was looking at, there. Unfortunately, I'm more web design than web dev. Would it be possible to dumb down the syntax for me like Ben did for the Save? (Or point me to something with "for dummies" instructions?)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2023 Mar 05, 2023
quote

Would it be possible to dumb down the syntax for me like Ben did for the Save?


By @Under S.

========

I'm afraid not.  It's not simple for a lot of complicated reasons  -- HTTPS, which browsers people use, etc...

 

Thus this road leads back to browser's built-in feature, as mentioned in my first reply.  😉

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2023 Mar 05, 2023

See:

https://openjavascript.info/2022/09/27/how-to-copy-an-image-to-the-clipboard-with-vanilla-javascript...

 

As @Nancy OShea has stated, the browser built-in context menu is the best (at the moment) solution.

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2023 Mar 05, 2023

Did you test it?  It doesn't work for me. 

 

For what it's worth, I've tested several scripts so far and they all fail. Probably due to my system's security  -- which is OK with me. 

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2023 Mar 05, 2023
LATEST

Yes, it does work. As the video explains, the copyToClipboard function only works on PNG's. That is why the images are first converted using writeToCanvas function.

https://youtu.be/FWRFCPZ614g

 

But as you have stated: "why re-invent the wheel?"

 

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines