Web Analytics Made Easy -
StatCounter Saving Images on the fly? - CodingForum

Announcement

Collapse
No announcement yet.

Saving Images on the fly?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving Images on the fly?

    Hello,
    I'm using this script to generate thumb nails, ( width, height )

    but it just shows the images, what i would like to do it save the image from the script,


    my code:
    PHP Code:
    <?php
    header
    ('Content-type: image/jpeg');
    $myimage resizeImage('images/Bikini006.jpg''120''80');

    function 
    resizeImage($filename$newwidth$newheight){
       list(
    $width$height) = getimagesize($filename);
       if(
    $width $height && $newheight $height){
           
    $newheight $height / ($width $newwidth);
       } else if (
    $width $height && $newwidth $width) {
           
    $newwidth $width / ($height $newheight);
       } else {
           
    $newwidth $width;
           
    $newheight $height;
       }
       
    $thumb imagecreatetruecolor($newwidth$newheight);
       
    $source imagecreatefromjpeg($filename);
       
    imagecopyresized($thumb$source0000$newwidth$newheight$width$height);
       return 
    imagejpeg($thumb);
    }

    print 
    resizeImage('images/Bikini006.jpg''120''80');
    ?>
    so how can i save the modified version to a directory on the server from this script??

    thanks in advance for your time!
    -Ken

  • #2
    hmm, ok.

    i've found the imagejpeg function, but i still can't get it to work,
    i'm not getting any errors, but the thumb just doesn't show up in the thumbs dir for some reason.

    my code so far:

    <?php
    header('Content-type: image/jpeg');
    $myimage = resizeImage('images/Bikini006.jpg', '120', '80');

    function resizeImage($filename, $newwidth, $newheight){
    list($width, $height) = getimagesize($filename);
    if($width > $height && $newheight < $height){
    $newheight = $height / ($width / $newwidth);
    } else if ($width < $height && $newwidth < $width) {
    $newwidth = $width / ($height / $newheight);
    } else {
    $newwidth = $width;
    $newheight = $height;
    }
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    return imagejpeg($thumb);


    imagejpeg($thumb, '/u/a/atticnet/www.domain.com/gallery/images/thumbs/Bikini006.jpg',100);

    }

    print resizeImage('images/Bikini006.jpg', '120', '80');

    ?>

    any ideas what i'm doing wrong here??

    thanks again for your time!
    -Ken

    Comment


    • #3
      Hiya Ken,
      The trick to this is to use output buffering to capture your data in a variable. Normally, the imagepng, imagegif, imagejpeg etc functions will flush it to the browser. This has been modified for your use - originally it was used to store information in a database, but it will work fine for your purposes.
      PHP Code:
      // This one is yours:
      $source imagecreatefromjpeg($filename);
      imagecopyresized($thumb$source0000$newwidth$newheight$width$height);
      return 
      imagejpeg($thumb);
      // Edit it like so:
      $source imagecreatefromjpeg($filename);
      imagecopyresized($thumb$source0000$newwidth$newheight$width$height); // Damn, I forgot how long these were :P
      ob_start();
      imagegif($thumb);
      $created_image ob_get_contents();
      return 
      ob_end_clean(); 
      /* This one... Im not certain will work for you.
      If you experience difficulties doing this, just return your $created_image instead.  I have never tested a function returning an ob_end_* function before, so I'm not certain of its effect. */ 
      Let me know if that works for you, specifically the returned section. I assume from this point you know how to write this to a file correct? The $created_image is binary btw.

      Edit:
      Sorry, I changed this as well. I removed the need for the jpeg simply because your are resizing it into a palett instead. Therefore, I would suggest a gif as your new thumb format.
      Last edited by Fou-Lu; Jul 30, 2005, 04:48 PM.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment

      Working...
      X