...

View Full Version : GD and Force Download



dniwebdesign
05-18-2008, 10:43 AM
<?php
include("inc.config.php");
$fileID = $_GET['photoID']*1;
$loc = $_GET["loc"];
$copy = $_GET["copy"];
$filepath = "photos_04665171/".$fileID.".jpg";
if(file_exists($filepath)) {
$loc = str_replace(" ","_",$loc);

$image = imagecreatefromjpeg("$filepath");
$color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66);
$filesize = getimagesize($filepath);
$width = $filesize[0];
$height = $filesize[1];
$font = 'arial.ttf';
$fontSize = "20";
$fontRotation = "0";
$dateSize = "10";
$str_date = "Downloaded: " . date("m-j-Y h:i:s") ." - http://www.vanishinglandmarks.ca";
$str_copy = "Copyright";
$str_name = "Dawson Irvine";

// Get box info
$box = imagettfbbox($fontSize, 0, $font, $str_copy);
//echo "<pre>";
//print_r($box);
//Find out the width and height of the text box
$textW = $box[2] - $box[0];
$textH = $box[5] - $box[3];

// Calculate the positions
$positionLeft = ($width - $textW)/2;
$positionTop = ($height - $textH)/2;

/* Shadow */
imagettftext($image, $fontSize, $fontRotation, $positionLeft+2, $positionTop+2, $colorShadow, $font, $str_copy);
/* Copyright */
imagettftext($image, $fontSize, $fontRotation, $positionLeft, $positionTop, $color, $font, $str_copy);

// Get box info
$box = imagettfbbox($fontSize, 0, $font, $str_name);
//echo "<pre>";
//print_r($box);
//Find out the width and height of the text box
$textW = $box[2] - $box[0];
$textH = $box[5] - $box[3];

// Calculate the positions
$positionLeft = ($width - $textW)/2;
$positionTop = (($height - $textH)/2)+ 30;
/*Shadow */
imagettftext($image, $fontSize, $fontRotation, $positionLeft+2, $positionTop+2, $colorShadow, $font, $str_name);
/* Name */
imagettftext($image, $fontSize, $fontRotation, $positionLeft, $positionTop, $color, $font, $str_name);





/* Download Date & Link */
// Get box info
$box = imagettfbbox($dateSize, 0, $font, $str_date);
//echo "<pre>";
//print_r($box);
//Find out the width and height of the text box
$textW = $box[2] - $box[0];
$textH = $box[5] - $box[3];

// Calculate the positions
$positionLeft = ($width - $textW)-5;
$positionTop = $height-5;
/*Shadow */
imagettftext($image, $dateSize, $fontRotation, $positionLeft+2, $positionTop+2, $colorShadow, $font, $str_date);
/* Date & URL */
imagettftext($image, $dateSize, $fontRotation, $positionLeft, $positionTop, $color, $font, $str_date);


imagejpeg($image);
}
exit(0);
die();

Alright... I have this script which does exactly what I want it to do.... almost. I got it to output the image with the copyright text on it and the link and date, however when the user clicks on the download link, I would like to force the download the image. I had the code below working until I added the GD functionality to add the copyright and link texts.

header("Content-Length: " . filesize($filepath));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$loc.'.jpg');
readfile($filepath);

How can I get this to force-download after doing the GD functions? I can't get it to work no matter what I try. Some fresh brains might help... :-D Thnx.

Inigoesdr
05-18-2008, 08:22 PM
Edit: To force the download you're going to have to write the file to disk(or maybe capture the output with output buffering) before sending it.

You don't need both of these:

exit(0);
die();

dniwebdesign
05-18-2008, 08:49 PM
Man... you keep on changing your answer... :-P

How would I go about writing the image to the file, then downloading that specific file, then removing that file from the server (to prevent clutter).

Inigoesdr
05-18-2008, 08:56 PM
You would use the second parameter to imagejpeg() (http://php.net/imagejpeg) to specify the filename to write it to, use the filename in the header() (http://php.net/header) & readfile() (http://php.net/readfile) functions, and then unlink() (http://php.net/unlink) it once you have sent it.

dniwebdesign
05-18-2008, 09:09 PM
Ah, perfect!
I never used the second parameter for imagejpeg before. I just outputted it, used the same headers and readfile (as you said) and it worked fine.... then I can unlink it. :-D Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum