Download images

Asked

Viewed 1,055 times

0

I have the following code:

 <section class="margem-base-80 coluna  sombra-suave" id="recipiente-topo">

<h1 class="margem-topo-50 recipiente letra-branca">Gerar QRcode 
gratuitamente</h1>
<main class="em-linha" id="main-index">

<article class="recipiente" id="campo-qr">
  <form class="coluna" method="post">
    <label for="gerar-qr">Endereço web (URL)</label>
    <input type="text" name="code" id="code-input" 
placeholder="http://exemplo.com" required />
    <input type="hidden" name="gerar" value="s">
    <div>
    <button class="btn-azul margem-topo-50">Gerar QR code</button>
    </div>
  </form>
</article>

<?php

  if(isset($_POST['gerar'])) {

    require_once "interno/phpqrcode/qrlib.php";

    $valor = $_POST['code'];

    $code = $valor;

    QRcode::png($code, "Imagem_QRCODE_M.png", QR_ECLEVEL_M, 8);

    $qr= '<img id="tamanho-code" src="Imagem_QRCODE_M.png"/>';
  ?>  
<article class="recipiente" id="qr-campo">
  <?=$qr?>
</article>
  <?php
  } else { ?>
<article class="recipiente" id="qr-campo">
</article>
 <?php
  }
?>

</main>
</section>

This example image is not in a database and is also not in a folder among the files of the site, it is only being displayed on the site through my html code for being a qr code image that the site generates receiving the data via post. For some user to save this image on their computer it has to right-click and then go on to save images. How can I add my button download this image as soon as the customer clicks on it?

  • Please rephrase your question more clearly I am not able to understand what you want, put the code you have, tell what he is doing and what you should do

  • actually I put the code, but it’s not showing.. it’s just a src img and a button below.. I edited the question to be clearer now..

1 answer

0


I’m not sure I understand, but if you want a button to download an image, that’s it:

<a href="caminho_do_arquivo.txt" download="Nome do arquivo"><button>Download</button></a>

The attribute download causes the file mentioned in href is downloaded with the given name in it may contain spaces, but the name in href cannot contain spaces

Note: For your html code to appear you need to put 4 spaces before type, instead of

code...

place:

código...

These 4 spaces is for the site to understand that what you are going to type is a code block and not a code, so it is literally appearing a button, because you have put a <button>

That’s it?

  • It does not have a path, as I put in the question. This image is a qrcode that I receive information via post

  • try to arrange your question by showing your code

  • Thanks for the tip of the 4 spaces @Guilhermecotamilam

  • I repeat, my problem is that I do not have a herf in the image.. it is generated and displayed on the site by data I receive via post

  • can i put a href with any value? and even if the image change with the generation of a new qrcode will download?

  • not html, but with js you can assign a variable to href, is that what you want? put your code that manages the qrcode

  • edited the question and put my entire code. Can you help me? If the solution is js I won’t know how to do it

  • First try to put the src of <img>, works? If not, right-click on the image and click copy image address, try to put this address, if it doesn’t work put the address here in the comments

  • Thanks @Guilhermecostamilam

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.