I have reduced my problem to a simplified code to make it easier to understand, so here we go.
The exact thing what I want is to download a JPG image pressing a button, but the thing is that image is created dynamically by the code, how? I'm using dompdf to create a PDF file from scratch HTML code so then I have to convert that PDF to JPG, but I don't know how to do it, here are some visual explanations and the code:
The simple HTML button which I use to call the file where the file is generated
The PDF file that it downloads when clicked
Here is the code I have:
The code of the button (ultrasimple)
button.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Button download</title>
</head>
<body>
<?php $name = "Mike"; ?>
<button><a href="generate.php?name=<?php echo $name; ?>">BUTTON</a></button>
</body>
</html>
And the code where the file is generated:
generate.php
<?php
$name = $_GET['name'];
?>
<?php
ob_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generate file</title>
</head>
<body>
<?php echo $name; ?>
</body>
</html>
<?php
$html = ob_get_clean();
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$options = $dompdf->getOptions();
$options->set(array('isRemoteEnabled' => true));
$dompdf->setOptions($options);
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("file.pdf", array("Attachment" => true));
?>
(IMPORTANT: When I click the button it automatically download the PDF file and I want it to be the same but with that PDF converted to JPG)
So what I need is that when I click the button instead of downloading a PDF it must download a JPG.
To achieve that, you could use Imagick - a native PHP extension, quick and simple for processing images.
//first, define a file that you want to save as JPG image
$imgURL = 'tmp/NzgwYWIwMzZiOGNj.pdf';
//Process the file
$imagick = new Imagick();
$imagick->readImage($imgURL);
$imagick->writeImages('myimage.jpg', false);
This way you'll get your PDF file exported as JPG, page-by-page. You can also fine-tune the above snippet, for example by adding SetResolution() method. More on the matter can be found here: https://www.php.net/manual/en/book.imagick.php