SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Solved! Delete Created Image after loaded (watermarking)

    Hi guys, I have a question. I have images on my gallery folder and I want to show them on popup window, but before it's show up I do some changes (watermarking) on the image to add some courtesy text. Here's my script:
    PHP Code:
    // adding text and save as new file
    $image_name $image;
    $image_path "gallery/images/";
    $font_size "13";
    $font_path "gallery/trebuchet.ttf";
    $text "Courtesy of " $ownID;
    $save_path "gallery/temp_ims/";
    $new_name "fv_".$image;

    $size getimagesize($image_path.$image_name);
    $sizew $size[0];
    $sizeh $size[1];

    $dest_x $sizew 220;
    $dest_y $sizeh 13

      
    $image imagecreatefromjpeg($image_path.$image_name);
      
    $color imagecolorallocate($image255,255,255);
      
    $black imagecolorallocate($image0,0,0);

      
    imagettftext ($image$font_size0, ($dest_x 1), ($dest_y 1), $black$font_path$text);
      
    imagettftext ($image$font_size0$dest_x$dest_y$color$font_path$text);

      
    imagejpeg($image$save_path.$new_name'100');

      
    imagedestroy($image); 
    Showing the new file
    Code:
    <html><head></head>
    <body>
    <? echo "<img src='$save_path"."$new_name' border='0'>"; ?>
    </body>
    </html>
    What I like to do is deleting the new created image file (not the original file) after it's loaded in the page. I've tried to delete the file manualy from the folder after its been loaded and then I try to save the image and it is saving the file, I guess the browser take it from the cache file. So basiccaly this can be done.
    What I've done is put an unlink function right at the end of the html page
    Code:
    unlink ($save_path.$new_name);
    it is deleting the file but it's doing it before the image loaded, so in the popup window the image won't show.
    How can I do this?

    Thank you so much in advance guys!
    Last edited by the.activator; Apr 29, 2006 at 03:13. Reason: Problem Solved!
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  2. #2
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure whether it works with images as well but you can try flush or ob_flush before unlinking.
    Maybe that helps
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    One solution would be to put a PHP wrapper around the image. Instead of <img src="$save_path.$new_name">, have the image be <img src="view.php?im=$save_path.$new_name">. View.php sends the content-type header for the image format then dumps the file's contents to the browser (readfile()). After that the image has definitely been sent to the viewer and you can unlink() the file from there.

  4. #4
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Howdy guys! Thanks alot your respons, I will try each of your suggestions. Will get back soon to let you know if theres any problem or if it's works I'll post the complete solution
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  5. #5
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey guys it's work like a charm! I'm following Dan Grossman suggestion. Here's my script
    Code:
    // adding text and save as new file
    $image_name = $image;
    $image_path = "gallery/images/";
    $font_size = "13";
    $font_path = "gallery/trebuchet.ttf";
    $text = "Courtesy of " . $ownID;
    $save_path = "gallery/temp_ims/";
    $new_name = "fv_".$image;
    
    $size = getimagesize($image_path.$image_name);
    $sizew = $size[0];
    $sizeh = $size[1];
    
    $dest_x = $sizew - 220;
    $dest_y = $sizeh - 13;
    
      $image = imagecreatefromjpeg($image_path.$image_name);
      $color = imagecolorallocate($image, 255,255,255);
      $black = imagecolorallocate($image, 0,0,0);
    
      imagettftext ($image, $font_size, 0, ($dest_x + 1), ($dest_y + 1), $black, $font_path, $text);
      imagettftext ($image, $font_size, 0, $dest_x, $dest_y, $color, $font_path, $text);
    
      imagejpeg($image, $save_path.$new_name, '100');
    
      imagedestroy($image);
    
    //html view the image
    <html><head></head>
    <body>
    <? echo "<img src='doview.php?imgn=$save_path"."$new_name' border='0'>"; ?>
    </body>
    </html>
    And here's the content doview.php file:
    PHP Code:
    header"Content-Type: image/jpeg" );
    header"Content-Disposition: attachment; filename=\"$imgn\"" );
    header"Content-Length: " filesize"$imgn) );
    readfile"$imgn);
    unlink ($imgn);
    exit; 
    Once again, Thank you so much for your help.
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  6. #6
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooops... i was wrong... it was working fine when I'm working on my project localy. but then I just i finish my project and upload it, and now it's not working... I mean the image already deleted before it sent to user cache, so the image won't show... please help me... what's wrong with my script above.

    Thanks in advance guys
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can dump the file to the browser without creating a temporary file, opening it and reading it through. Just call imagejpeg() with no filename argument and it outputs directly to the browser. A flush() afterwards is a good idea too, and you won't need to unlink() anything afterwards.

  8. #8
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot for a very quick reply and suggestion Dan, I want to try it but I got a little bit confuse about your suggestion.
    Quote Originally Posted by Dan Grossman
    ... Just call imagejpeg() with no filename argument and it outputs directly to the browser. A flush() afterwards is a good idea too ...
    Would you kindly explain a little bit more distinct about this
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Actually, it'd take a little more than I said after refreshing myself on this thread... you'd have to move the image generation code to doview.php... so let's focus on fixing what you had instead.

    Something must be different between your local setup and the server that's causing it to act different. Can you remove the unlink() call and check if the image was being created manually?

    If that's not it, perhaps register_globals is turned off. If so, replace $imgn in doview.php with $_GET['imgn'].

    Off Topic:

    As is, your code also allows your site visitors to view any file on your server since no checks are done that what they're viewing is an image in your images directory. Some checking should be added for security.

  10. #10
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I add this in the first line of doview.php $imgn = $_GET['imgn']; and then i close the unlink call. The new images was succesfuly created on the temp folder, but still it won't show on the page do i have to move the image generation code to doview.php page?

    Quote Originally Posted by Dan Grossman
    As is, your code also allows your site visitors to view any file on your server since no checks are done that what they're viewing is an image in your images directory. Some checking should be added for security.
    Yes you're right, I've never tried or realized that until you mention it, I just check it and my directories can be browse this is horrible... what kind of security check that I should do? Can you give me a suggestion Dan? or should I just put a blank html file with index name (index.html) inside every folder so if someone directly browse to each folder the file will be loaded automaticaly and they can't see any other files inside.

    Thank you very much Dan!
    Last edited by the.activator; Apr 26, 2006 at 02:39.
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  11. #11
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys.. I've tried to move image genaration code but, still nothing happen...
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  12. #12
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by the.activator
    Yes you're right, I've never tried or realized that until you mention it, I just check it and my directories can be browse this is horrible... what kind of security check that I should do? Can you give me a suggestion Dan? or should I just put a blank html file with index name (index.html) inside every folder so if someone directly browse to each folder the file will be loaded automaticaly and they can't see any other files inside.
    How's that going to protect you? You have a script that takes a filename parameter, opens that file and dumps it to the browser. It doesn't know anything about "index.html". You'll need to restrict the filenames it accepts to certain patterns at the least.

    As for why your script doesn't work now, you're just going to have to do some debugging. Step through the program, place test output wherever you can and find out where it stops working as expected. Check if you can successfully open the file, if you can read its contents, if your script works without sending the headers, or with sending different content..

  13. #13
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved! I followed Dan Grossman suggestion above to dump the image directly to browser without having save it into temporary folder, do some buffer to get some information of the new image for sending header()
    Here's my script:
    PHP Code:
    $image_name $image;
    $image_path "gallery/images/";
    $font_size "13";
    $font_path "gallery/trebuchet.ttf";
    $text "Courtesy of " $ownID;

    $size getimagesize($image_path.$image_name);
    $sizew $size[0];
    $sizeh $size[1];

    $dest_x $sizew 220;
    $dest_y $sizeh 13;

      
    $image imagecreatefromjpeg($image_path.$image_name);
      
    $color imagecolorallocate($image255,255,255);
      
    $black imagecolorallocate($image0,0,0);

      
    imagettftext ($image$font_size0, ($dest_x 1), ($dest_y 1), $black$font_path$text);
      
    imagettftext ($image$font_size0$dest_x$dest_y$color$font_path$text);

    ob_start(); // start the output buffer
       
    imagejpeg$image""100 );
       
    $ImageData ob_get_contents();
       
    $ImageDataLength ob_get_length();
    ob_end_clean(); //  stop buffer

    header("Content-type: image/jpeg") ;
    header("Content-Length: ".$ImageDataLength);
    echo 
    $ImageData//show the image 
    and I won't need doview.php file anymore. cheers!
    Thank you so much for your help Dan, and for the security issue I use .htaccess security to protect hot linking for every folder and file in my server.
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    .htaccess won't protect you. The URL being accessed is doview.php, which is unprotected.

    What happens to your script there if I pass in "doview.php?image_name=../../../../../../etc/httpd/httpd.conf" ? Does it open httpd.conf and send it to the browser? I don't know if any of those image functions will stop execution of the script if they fail.

  15. #15
    SitePoint Enthusiast the.activator's Avatar
    Join Date
    Apr 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know about that Dan... never know nor had this kind of problem what should I do then? can u give me a suggestion?

    Right now my hosting company available with cpanel and within cpanel theres a feature to protect folders and files from hot linking or direct access, and I use it. It's create a .htaccess file to protect the folders and files.
    Regards from []
    -----------------------------------
    Art is either Plagiarism or Revolution!
    -----------------------------------


Bookmarks

Posting Permissions

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