0

I got the code from this link

but i have a problem with that. I extract the image from php file as under:-

<img src="http://localhost/wordpress/image.php" class="downloadable" id="mainimage"/>

and then i want to download it with javascript as under:-

$('img.downloadable').each(function(){
  var $this = $(this);
  $this.wrap('<a href="' + $this.attr('src') + '" download />')
});

The problem is that it download php file but i want to download it as PNG.

Extracts of image.php are as under:-

header ("Content-type: image/png");
$userinput = $_GET["user_input"];
$image=imagecreatefrompng('myimages/***image.png***'); 
$font_file = 'fonts/PR8Charade.ttf';
$col1 = imagecolorallocate($image, 129, 125,11);
    $text_size1 = 36;
    $xposition1 = 245;
    $yposition1 = 380;
    $angeldirection1 = 50;
    imagettftext($image, $text_size1, $angeldirection1, $xposition1, $yposition1, $col1, $font_file, $userinput);
ImagePng($image);
imagedestroy($image);

I want to download the image.png file from image.php.

Community
  • 1
  • 1
  • 2
    you need to output a content-disposition header which allows you to specify a filename for the "download". Since you don't have one, the browser simply chooses the most obvious filename it can - the name of the file in the url you're downloading from, which is `image.php` – Marc B Jun 09 '16 at 17:16
  • after creating image, you can sent image url to client, then `window.open(imageUrl)` will download your image – sabbir Jun 09 '16 at 17:19
  • [PHP content-disposition reference](http://php.net/manual/en/function.header.php#refsect1-function.header-examples) – shamsup Jun 09 '16 at 17:21

1 Answers1

1
<?php
//path to png image
$file = 'picture.png';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}
?>
Vitaly
  • 1,261
  • 2
  • 10
  • 20
  • 1
    With the possible caveat that you heed the file sizes. http://stackoverflow.com/questions/11786734/readfile-and-large-files might be worth reading. Also I'd like to suggest using xsendfile (header) if you can, it might be more effective. – Torbjörn Stabo Jun 09 '16 at 17:36