Problem for "Save image as" php

Asked

Viewed 81 times

0

I have a code that generates an electronic signature but when the user clicks right on the image generated to "Save image as" the name of the image comes as default name_pagina.php.png.

How do I get the .php of the file name to be saved?

Because lay users are not knowing how to rename the file to remove the extension .php

<?php
header("Content-type: image/png");

$image = imagecreatefrompng("assinatura.png");
$imageCel = imagecreatefrompng("cel.png");
$imageWhats = imagecreatefrompng("whats.png");
$imgPira = imagecreatefrompng("piracicaba.png");
$imgBotucatu = imagecreatefrompng("botucatu.png");
$imgLencois = imagecreatefrompng("lencois.png");
$imgJau = imagecreatefrompng("jau.png");

if (!empty ($_POST["name"])) {
    $fotinha = $_POST["name"];
    $imgFotinha = imagecreatefrompng("upload/".$fotinha.".png");
}

$tileColor = imagecolorallocate($image, 0, 63, 114);
$gray = imagecolorallocate($image, 100, 100, 100);

$nome = $_POST["nome"];
$funcao = $_POST["funcao"];
$telefone = $_POST["telefone"];
$email = $_POST["email"];
$cel = $_POST["cel"];

if (!empty($_POST["cel"])) {
    imagecopy($image, $imageCel, 445, 17, 0, 0, 12, 16);
    imagettftext($image, 10, 0, 460, 30, $tileColor, "C:\\xampp\\htdocs\\assinatura\\fonts\\Helvetica\\Helvetica-Normal.ttf", $cel);
    if (isset($_POST["whats"])) {
        imagecopy($image, $imageWhats, 560, 17, 0, 0, 15, 16);
    }
}

if (!empty ($_POST["name"])) {
    imagecopy($image, $imgFotinha, 10, 16, 0, 0, 100, 100);
}

imagettftext($image, 12, 0, 110, 33, $tileColor, "C:\\xampp\\htdocs\\assinatura\\fonts\\Helvetica\\Helvetica Bold.ttf", $nome);
imagettftext($image, 10, 0, 120, 50, $tileColor, "C:\\xampp\\htdocs\\assinatura\\fonts\\Helvetica\\Helvetica-Normal.ttf", $funcao);

imagettftext($image, 10, 0, 332, 30, $tileColor, "C:\\xampp\\htdocs\\assinatura\\fonts\\Helvetica\\Helvetica-Normal.ttf", $telefone);
imagettftext($image, 11, 0, 334, 56, $tileColor, "C:\\xampp\\htdocs\\assinatura\\fonts\\Helvetica\\Helvetica-Normal.ttf", $email);

$filial = $_POST["filial"];

if ($filial == "piracicaba") {
    imagecopy($image, $imgPira, 310, 75, 0, 0, 324, 11);
}

if ($filial == "botucatu") {
    imagecopy($image, $imgBotucatu, 310, 75, 0, 0, 324, 11);
}

if ($filial == "lencois") {
    imagecopy($image, $imgLencois, 310, 75, 0, 0, 324, 11);
}

if ($filial == "jau") {
    imagecopy($image, $imgJau, 310, 75, 0, 0, 324, 11);
}

imagepng($image);
imagedestroy($image);


?>
  • This happens because you are generating an image from a php page, you can set a Roa to a separate image or make a button download image

  • how can I make this route?

  • 1

    This is a simple example of how to do https://www.taniarascia.com/the-simplest-php-router . It has more complete and complex forms, but initially this must solve

1 answer

2


Just change the header Content-disposition.

Header

According to RFC6266, "the header field Content-Disposition is used to transmit additional information on how to process the payload of the response and can also be used to attach additional metadata, such as the file name to be used when saving the payload of the response locally."

This header has two fields: disposition-type and filename.

Disposition Type

If the layout type matches "Attachment", this indicates that the recipient should ask the user to save the response locally, instead of processing it normally (depending on their type of media).

On the other hand, if it corresponds to "inline", it implies standard processing.

Filename

This field provides information on the file name, which can be downloaded by the user.

If the field value disposition-type be it inline, value of this field can be used in the window Save as in....

header("Content-type: image/png");
header('Content-disposition: inline; filename="psr.png"');

If the field value disposition-type be it Attachment, the browser can force the download of the content according to the content-type and the filename indicated in response headers.

header("Content-type: image/png");
header('Content-disposition: attachment; filename="psr_f.png"');

Browser other questions tagged

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