php script for thumbnails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johan

    php script for thumbnails

    Hi,

    Where to find a php script to upload jpg files and make thumbnails of the
    jpg files ?

    Johan


  • John Murtari

    #2
    Re: php script for thumbnails

    "Johan" <me@knoware.n l> writes:
    [color=blue]
    > Where to find a php script to upload jpg files and make thumbnails of the
    > jpg files ?[/color]

    DOn't have the complete thing, but here a few functions to help:

    # Function CreateThumbnail - creates the thumbnail version
    # of a photo at a fixed size (81 high and correct width)
    # INPUT - sourcefile name, targetfile name (thumbnail)
    # OUTPUTS - new file in targetfile location
    # RETURNS - 0 on success, message otherwise
    #
    function CreateThumbnail ($sourcefile, $targetfile) {


    $desiredY = 81;

    // Get the dimensions of the source picture
    $picsize=getima gesize("$source file");

    if ($picsize == false) { // failed
    return("Could not get size on picture $sourcefile");
    }

    $source_x = $picsize[0];
    $source_y = $picsize[1];

    $ratio = $desiredY / $source_y;

    $newX = (int) ($ratio * $source_x);
    $newY = (int) ($ratio * $source_y);


    if ($msg = ResizeToFile($s ourcefile, $newX, $newY, $targetfile)) {
    return("Resize failed to targetfile: $targetfile ($msg)");
    }

    return(0);

    } // end function CreateThumbnail



    /* Function: resizeToFile resizes a picture and writes it to the harddisk
    *
    * $sourcefile = the filename of the picture that is going to be resized
    * $dest_x = X-Size of the target picture in pixels
    * $dest_y = Y-Size of the target picture in pixels
    * $targetfile = The name under which the resized picture will be stored
    * $jpegqual = The Compression-Rate that is to be used
    */
    function ResizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual=60)
    {

    /* Get the dimensions of the source picture */
    $picsize=getima gesize("$source file");
    if ($picsize == false) {
    return("Could not get size on file: $sourcefile\n") ;
    }

    $source_x = $picsize[0];
    $source_y = $picsize[1];

    $source_id = imageCreateFrom JPEG("$sourcefi le");

    if (! $source_id) {
    return("Could not create image from jpeg file: $sourcefile\n") ;
    }

    /* Create a new image object (not neccessarily true colour) */
    $target_id=imag ecreatetruecolo r($dest_x, $dest_y);

    /* resize the original picture and copy it into the just created image
    object. Because of the lack of space I had to wrap the parameters to
    several lines. I recommend putting them in one line in order keep your
    code clean and readable
    */
    $target_pic=ima gecopyresampled ($target_id,$so urce_id,
    0,0,0,0,
    $dest_x,$dest_y ,
    $source_x,$sour ce_y);

    /* Create a jpeg with the quality of "$jpegqual" out of the
    image object "$target_pi c".
    This will be saved as $targetfile */
    $stat = imagejpeg ($target_id,"$t argetfile" ,$jpegqual);
    if (! $stat) {
    return("Failed to create new image file: $targetfile");
    }

    return 0;


    } // end function ResizeToFile



    --
    John
    _______________ _______________ _______________ _______________ _______
    John Murtari Software Workshop Inc.
    jmurtari@follow ing domain 315.635-1968(x-211) "TheBook.Co m" (TM)

    Comment

    • Andrew Wasielewski

      #3
      Re: php script for thumbnails

      Here are some scripts I have written to dynamically build a HTML table of
      thumbnails & insert it as an OBJECT element in the page from which it is
      called.

      The top-level page contains JavaScript to create the OBJECT element:

      <script LANGUAGE="JavaS cript1.2">
      OutStr = '<object type="text/html" data="BuildThum bnailGrid.php?w in_width='
      + GetWinWidth() + '&img_width=100 &border_width=0 &cell_space= 40" width="100%"
      height="100%" border=0>If you are reading this your browser doesn’t
      support Object element...opros tite.'
      document.write( OutStr);
      document.write( '</object>');
      </script>

      You can use this JS function to find out how wide your browser window is,
      otherwise just enter no. of pixels:

      <script LANGUAGE="JavaS cript1.2">
      function GetWinWidth()
      {
      if (navigator.appN ame == 'Netscape' && document.layers != null)
      {
      return WinWidth = window.innerWid th;
      }
      if (document.all != null)
      {
      return WinWidth = document.body.c lientWidth;
      }
      }
      </script>

      The OBJECT element calls the BuildThumbnailG rid.php script, which in turn
      finds all the *.JPG files in the current directory (or whichever you
      specify) and calls MakeThumbnail.p hp to make a thumbnail on the fly.

      File BuildThumbnailG rid.php:
      -----------------------------
      <?php
      // Pass available window width as win_width using HTTP GET method
      // Pass thumbnail width as img_width HTTP GET method
      // Pass table border width as border_width using HTTP GET method
      // Pass cell padding as cell_pad using HTTP GET method
      // Pass cell spacing width as cell_space using HTTP GET method

      $win_width = $_GET["win_width"];
      $img_width = $_GET["img_width"];
      $border_width = $_GET["border_wid th"];
      $cell_pad = $_GET["cell_pad"];
      $cell_space = $_GET["cell_space "];

      // Calculate no. of columns that will fit window
      $num_cols = floor(($win_wid th - $cell_space)/($img_width + $cell_space)) -
      1;
      $curr_col = 0; //Set column counter to zero

      echo '<table border=' . $border_width . ' cellspacing=' . $cell_space .
      '>';
      foreach (glob("*.JPG") as $filename) /* Needs PHP 4 >= 4.3.0, PHP 5 to
      work. Use readdir() to build array otherwise. */
      {
      if ($curr_col == 0)
      echo '<tr>';
      echo '<td>';
      echo '<a href="' . $filename . '">';
      echo '<img src="MakeThumbn ail.php?in_file =' . $filename . '&in_width=' .
      $img_width . '">';
      echo '</a>';
      echo '</td>';
      if ($curr_col < $num_cols)
      $curr_col += 1;
      else
      {
      echo '</tr>';
      $curr_col = 0;
      }
      }
      if ($curr_col < $num_cols)
      echo '</tr>';
      echo '</table>';

      ?>

      File MakeThumbnail.p hp:
      -------------------------
      <?php
      // Pass arguments with HTTP GET method
      // Pass $filename as in_file
      // Pass $new_width as in_width
      $filename = $_GET["in_file"];
      if (isset($_GET["in_width"]))
      $new_width = $_GET["in_width"];
      else
      $new_width = 120;

      // Set content type
      header('Content-type: image/jpeg');

      // Get image dimensions & resize
      list($width, $height) = getimagesize($f ilename);
      $aspect_ratio = $height / $width;
      $new_height = $new_width * $aspect_ratio;

      // Resample
      $output_image = imagecreatetrue color($new_widt h, $new_height);
      $image = imagecreatefrom jpeg($filename) ;
      imagecopyresamp led($output_ima ge, $image, 0, 0, 0, 0, $new_width,
      $new_height, $width, $height);

      // Output image
      imagejpeg($outp ut_image, null, 100);
      ?>


      "Johan" <me@knoware.n l> wrote in message
      news:10rh3nvfgp bik5d@corp.supe rnews.com...[color=blue]
      > Hi,
      >
      > Where to find a php script to upload jpg files and make thumbnails of the
      > jpg files ?
      >
      > Johan
      >
      >[/color]


      Comment

      Working...