Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    842
    Thanks
    15
    Thanked 9 Times in 9 Posts

    Question GD and Force Download

    PHP Code:
    <?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($image0xFF0xFF0xFF);
        
    $colorShadow imagecolorallocate($image0x660x660x66);
        
    $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($fontSize0$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($fontSize0$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($dateSize0$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.
    PHP Code:
            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.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    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:
    PHP Code:
    exit(0);
    die(); 
    Last edited by Inigoesdr; 05-18-2008 at 07:47 PM.

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    842
    Thanks
    15
    Thanked 9 Times in 9 Posts
    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).
    Last edited by dniwebdesign; 05-18-2008 at 07:51 PM. Reason: The person I'm replying to keeps changing his....
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You would use the second parameter to imagejpeg() to specify the filename to write it to, use the filename in the header() & readfile() functions, and then unlink() it once you have sent it.

  • Users who have thanked Inigoesdr for this post:

    dniwebdesign (05-18-2008)

  • #5
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    842
    Thanks
    15
    Thanked 9 Times in 9 Posts
    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!
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •