PHP create thumbnail

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    PHP create thumbnail

    Sorry for the newbie question but PHP is not my forte...

    I am attempting to implement a script that will retrieve a file, create a thumbnail image, and write the thumbnail to the "Response.Outpu tStream". The thing is that I don't think that such a thing exists in PHP and so I'm a bit lost.

    How do I write the file (as bytes) directly to the response?

    This is what I have so far....
    Code:
    // load image and get image size
          $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
          $width = imagesx( $img );
          $height = imagesy( $img );
    
          // calculate thumbnail size
          $new_width = $thumbWidth;
          $new_height = floor( $height * ( $thumbWidth / $width ) );
    
          // create a new temporary image
          $tmp_img = imagecreatetruecolor( $new_width, $new_height );
    
          // copy and resize old image into new image
          imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    
          // save thumbnail into a file
          //imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
    //>>> Here I want to save the file to the to the output stream <<<
    Any guidance on the issue would be appreciated.

    Thanks

    -Frinny
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I don't think there is an equivalent to "Response.Outpu tStream" in PHP. There is no need for it.

    Everything PHP prints goes directly into the output stream, and it prints binary data just as it does strings. (Technically it would be the other way around, but the result is the same.)

    That is, if you want to have PHP read and print an image from the file-system, you simply do:
    [code=php]<?php
    header("Content-type: image/png");
    $data = file_get_conten ts("some_image. png");
    echo $data;
    ?>[/code]
    The $data would be a string of bytes, and the echo call would just add that to the output buffer. Text is also treated as a string of bytes, so there really isn't any difference between how the two are treated.

    In the case of GD images, you can use the GD "image*" functions (imagepng, imagejpeg, etc...) to create the image in the format you want and add it to the output. - Note, if you provide a file path in the second parameter, it saves it there. If you omit it, the data goes directly into the output buffer.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Ahh, thank you so much Atli.
      I'll have to check this out later tonight :)

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        **Huge Sigh of Disappointment*

        Apparently I can't do what I want to do because the (CMS) PHP framework I'm developing on top of is preventing people from accessing my script directly.

        This means that I can't create the thumbnail component to be used in my application.

        I'm so disappointed right now.

        I guess I'm going to have to try and resize the images using JavaScript instead (so that they aren't distorted by the <img> tag).


        Thanks again for your help.

        -Frinny

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          There is one thing you could try.

          If you have access to the index.php of your CMS (assuming all requests are routed through a central point?), then you could add ob_start() at the very top, which turns on Output Buffering, preventing anything from being printed.

          Then you can create a normal request via your CMS, and when it reaches the PHP code for that request you can issue a ob_end_clean(), print your thumb, and exit the code.

          For example, in a MVC app...
          [code=php]<?php
          /* file: index.php */

          // Start the output buffering.
          ob_start();

          // Kick of the MVC app.
          ($ctrl = @$_GET['ctrl']) or $ctrl = "Default";
          ($action = @$_GET['action']) or "default";

          $ctrl = ucfirst($ctrl) . "Controller ";
          require $ctrl_class . ".php";

          $inst = new $ctrl_class();
          $inst->$action();

          // Mandatory end-flush of the Output Buffer.
          ob_end_flush();
          ?>[/code]
          [code=php]<?php
          /* file ThumbController .php */

          class ThumbController {
          function create() {
          // Disgard and stop the Output Buffer
          ob_end_clean();

          // Create your thumb
          // ... etc

          // Print
          header('Content-type: image/jpeg');
          imagejpeg($myGd Image);

          // Exit the application, leaving on the image data in the
          // response.
          exit;
          }
          }
          ?>[/code]
          Now you can just call index.php?ctrl=thumb&action=create and it would display your thumb.

          Comment

          Working...