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.

3
  • 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 Commented Jun 9, 2016 at 17:16
  • after creating image, you can sent image url to client, then window.open(imageUrl) will download your image Commented Jun 9, 2016 at 17:19
  • PHP content-disposition reference Commented Jun 9, 2016 at 17:21

1 Answer 1

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;
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

With the possible caveat that you heed the file sizes. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.