Building thumbnail page (mysql)

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

    #1

    Building thumbnail page (mysql)

    Hi,

    I need some help (php rookie) to build a thumbnail page using php.
    I'v a mysql database containing links to the original image files.
    No thumbnails created so far.

    It would be nice when the thumbnail contains a link to the original file :-)

    Jane


  • Krustov

    #2
    Re: Building thumbnail page (mysql)

    <comp.lang.ph p>
    <Jane>
    <Sat, 28 Oct 2006 14:53:39 +0200>
    <ehvjs8$er8$1@l ocalhost.locald omain>
    I need some help (php rookie) to build a thumbnail page using php.
    I'v a mysql database containing links to the original image files.
    No thumbnails created so far.
    >
    It would be nice when the thumbnail contains a link to the original file :-)
    >
    Its usually better to scan the folder for image files , The following
    doesnt create the thumbnails for you - but it will get you started .


    <?php

    $batman=0;

    $dir="chatroom/images";
    $joker=opendir( $dir);
    while (false!==($boyw onder=readdir($ joker)))
    {
    $files[]=$boywonder;
    $batman=$batman +1;
    }

    $temp=0;
    while ($temp<$batman)
    {
    $demo=$files[$temp];
    $link="$dir" . "/" . "$demo";
    print "<a href=$link>$dem o</a><br>";
    $temp=$temp+1;
    }

    ?>


    --

    Comment

    • Krustov

      #3
      Re: Building thumbnail page (mysql)

      <comp.lang.ph p>
      <Krustov>
      <Sat, 28 Oct 2006 15:00:49 +0100>
      <MPG.1fad72d490 49c05b98a123@ne ws.newsreader.c om>
      Its usually better to scan the folder for image files , The following
      doesnt create the thumbnails for you - but it will get you started .
      >
      The following php code being a bit better as it will only display files
      that have the .jpg extension .


      <?php

      $batman=0;

      $dir="chatroom/images";
      $joker=opendir( $dir);
      while (false!==($boyw onder=readdir($ joker)))
      {
      $files[]=$boywonder;
      $batman=$batman +1;
      }

      $temp=0;
      while ($temp<$batman)
      {
      $demo=$files[$temp];

      $riddler=strlen ($demo);
      $penguin=substr ($demo,$riddler-4,4);

      if ($penguin==".jp g")
      {
      $link="$dir" . "/" . "$demo";
      print "<a href=$link>$dem o</a><br>";
      }

      $temp=$temp+1;
      }

      ?>


      --

      Comment

      • Jane

        #4
        Re: Building thumbnail page (mysql)

        Its usually better to scan the folder for image files.

        Finaly i need to store more information then only the file names.
        So i think i need a database. Right ?

        For now, i only need some help to display the thumbnail pages (e.g. 6
        thumbnails per page)

        (I know there are very good ready to use galleries, but i'd like to develop
        one as an exercise ;-) )

        Thanks in advance,

        Jane


        Comment

        • Pedro Graca

          #5
          Re: Building thumbnail page (mysql)

          Jane wrote:
          I need some help (php rookie) to build a thumbnail page using php.
          I'v a mysql database containing links to the original image files.
          No thumbnails created so far.
          >
          It would be nice when the thumbnail contains a link to the original file :-)
          So you already have the filenames in the database.
          I'd add to the database a new column for the thumbnail

          alter table XXX add column thumbnail enum('no', 'yes');
          update table XXX set thumbnail='no';

          Then I'd create the thumbnails for existing images:

          <?php
          $sql = "select filename from XXX where thumbnail='no'" ;
          $res = mysql_query($sq l);
          while ($row = mysql_fetch_row ($res)) {
          create_thumbnai l($row[0]);
          $sql = "update XXX set thumbnail='yes' where filename='{$row[0]}'";
          mysql_query($sq l);
          }
          ?>

          where `create_thumbna il()` creates the thumbnail and stores it in a
          "thumbs" directory.

          When you want to display thumbnails just fetch them:

          <?php
          function thumbname($path ) {
          // input: http://www.example.com/images/photo00042.jpg
          // output: http://www.example.com/images/thumbs/photo00042.jpg
          return dirname($path) . '/thumbs/' . basename($path) ;
          }

          // ...

          $sql = "select URL from XXX where thumbnail='yes' order by rand() limit 6";
          $res = mysql_query($sq l);
          while ($row = mysql_fetch_row ($res)) {
          echo '<a href="' . $row[0] . '">';
          echo '<img src="' . thumbname($row[0]) . '">';
          echo '</a>';
          }
          ?>

          --
          I (almost) never check the dodgeit address.
          If you *really* need to mail me, use the address in the Reply-To
          header with a message in *plain* *text* *without* *attachments*.

          Comment

          • Geoff Berrow

            #6
            Re: Building thumbnail page (mysql)

            Message-ID: <ehvjs8$er8$1@l ocalhost.locald omainfrom Jane contained the
            following:
            >
            >I need some help (php rookie) to build a thumbnail page using php.
            >I'v a mysql database containing links to the original image files.
            >No thumbnails created so far.
            >
            >It would be nice when the thumbnail contains a link to the original file :-)
            Here's one without a database






            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Jane

              #7
              Re: Building thumbnail page (mysql)

              Pedro,

              I'll give it a try. It point's me in the right direction !
              Is it necessary to create thumbnails beforehand, or is it possible to resize
              the original images at runtime.

              Regards,

              Jane


              Comment

              • Pedro Graca

                #8
                Re: Building thumbnail page (mysql)

                Jane wrote:
                Is it necessary to create thumbnails beforehand, or is it possible to resize
                the original images at runtime.
                It is possible to resize at runtime. However you only want to do the
                resizing once for each original image (why would you want to misuse
                resources by resizing the same images every now and again?)

                My previous post was meant to run once only (after the change in the
                database) for existing images. When you acquire a new image, do the
                resizing when you deal with the post so that all images have a
                thumbnail.

                --
                I (almost) never check the dodgeit address.
                If you *really* need to mail me, use the address in the Reply-To
                header with a message in *plain* *text* *without* *attachments*.

                Comment

                • Krustov

                  #9
                  Re: Building thumbnail page (mysql)

                  <comp.lang.ph p>
                  <Geoff Berrow>
                  <Sun, 29 Oct 2006 01:45:47 +0100>
                  <79u7k2p5g9uop3 r6mrc5g4qhc2b11 ru7fd@4ax.com>
                  I need some help (php rookie) to build a thumbnail page using php.
                  I'v a mysql database containing links to the original image files.
                  No thumbnails created so far.

                  It would be nice when the thumbnail contains a link to the original file :-)
                  >
                  Here's one without a database
                  >

                  >

                  >
                  The following doesnt need a database either , Although the user could
                  put the filenames into the database if they wanted there would be little
                  point as it scans the folder every time for new images & creates a
                  thumbnail if there isnt one already available .

                  - change the folder name

                  - change the number of columns (if wanted)

                  First draft php code - but seems to work ok .




                  <?php

                  $dir="test/images"; $columns=6;

                  $batman=0;

                  $joker=opendir( $dir);
                  while (false!==($boyw onder=readdir($ joker)))
                  {
                  $files[]=$boywonder;
                  $batman=$batman +1;
                  }



                  $temp=0;
                  while ($temp<$batman)
                  {
                  $demo=$files[$temp];

                  $riddler=strlen ($demo);
                  $penguin=substr ($demo,$riddler-4,4);
                  $catwoman=subst r($demo,$riddle r-10,10);

                  $img_name="$dir " . "/" . "$demo";
                  $thumb_name=str _replace(".jpg" ,"_thumb.jpg",$ img_name);

                  if ($penguin==".jp g" && $catwoman<>"_th umb.jpg")
                  {

                  $pass=1;
                  $filename=$thum b_name;
                  if (!file_exists($ filename)) {$pass=0;}

                  if ($pass==0)
                  {
                  $max_width=150; $max_height=80;
                  $size=GetImageS ize($img_name);
                  $width_ratio=($ size[0] / $max_width); $height_ratio=( $size[1] /
                  $max_height);
                  if ($width_ratio>= $height_ratio) {$ratio=$width_ ratio;} else {$ratio=
                  $height_ratio;}
                  $new_width=($si ze[0] / $ratio); $new_height=($s ize[1] / $ratio);
                  $src_img=ImageC reateFromJPEG($ img_name);
                  $thumb=ImageCre ateTrueColor($n ew_width,$new_h eight);
                  ImageCopyResamp led($thumb,$src _img,0,0,0,0,($ new_width-1),($new_height-
                  1),$size[0],$size[1]);
                  ImageJPEG($thum b,$thumb_name,1 00);
                  ImageDestroy($s rc_img);
                  ImageDestroy($t humb);
                  }

                  }

                  $temp=$temp+1;
                  }



                  $homer=0;

                  $joker=opendir( $dir);
                  while (false!==($boyw onder=readdir($ joker)))
                  {
                  $marge[]=$boywonder;
                  $homer=$homer+1 ;
                  }

                  $lisa=0;

                  $temp=0;
                  while ($temp<$homer)
                  {
                  $demo=$marge[$temp];

                  $riddler=strlen ($demo);
                  $penguin=substr ($demo,$riddler-4,4);
                  $catwoman=subst r($demo,$riddle r-10,10);

                  if ($catwoman=="_t humb.jpg")
                  {
                  $thumb_name="$d ir" . "/" . "$demo";
                  $img_name=str_r eplace("_thumb. jpg",".jpg",$th umb_name);
                  $millhouse[$lisa]=$thumb_name;
                  $diamondjoe[$lisa]=$img_name;
                  $lisa=$lisa+1;
                  }

                  $temp=$temp+1;
                  }

                  ?>

                  <?php

                  print "<table border=0 cellspacing=5 cellpadding=0 align=center>";

                  $loopy=0;

                  while ($loopy<$lisa)
                  {

                  print "<tr valign=top>";

                  $tempxx=0;
                  while ($tempxx<$colum ns)
                  {

                  if ($loopy<$lisa)
                  {
                  $size=GetImageS ize($millhouse[$loopy]);
                  print "<td><a href=$diamondjo e[$loopy]><img src=$millhouse[$loopy]
                  width=$size[0] height=$size[1] border=0></a><br></td>";
                  }

                  $tempxx=$tempxx +1;
                  $loopy=$loopy+1 ;
                  }

                  print "</tr>";

                  }


                  print "</table>";

                  ?>




                  Comment

                  • Krustov

                    #10
                    Re: Building thumbnail page (mysql)

                    <comp.lang.ph p>
                    <Geoff Berrow>
                    <Sun, 29 Oct 2006 01:45:47 +0100>
                    <79u7k2p5g9uop3 r6mrc5g4qhc2b11 ru7fd@4ax.com>
                    $filename="main ";
                    if (!file_exists($ filename)) {mkdir($filenam e,0777);}

                    $filename="thum bs";
                    if (!file_exists($ filename)) {mkdir($filenam e,0777);}

                    Although i'm watching the snooker final - i had a look between frames at
                    your gallery script .

                    Why not put the above at the start of the index.php or create a
                    install.php file ? .


                    Any text files that need created could be done using touch .

                    $filename="blah/demo.txt";
                    if (!file_exists($ filename))
                    {
                    touch($filename );
                    chmod($filename ,0766);
                    $temp="Nowt Yet";
                    $fp=fopen($file name,"w");
                    fwrite($fp,$tem p);
                    fwrite($fp,"\n" );
                    fclose($fp);
                    }

                    You suggested ws_ftp to me the other day on another newsgroup to set the
                    file permissions and i thought you where trying to take the piss given
                    you are much more experienced at php than i am .


                    --

                    Comment

                    • Geoff Berrow

                      #11
                      Re: Building thumbnail page (mysql)

                      Message-ID: <MPG.1faedb444f 3a71df98a12e@ne ws.newsreader.c omfrom
                      Krustov contained the following:
                      >
                      >Why not put the above at the start of the index.php or create a
                      >install.php file ? .

                      Lack of tuits.


                      --
                      Geoff Berrow (put thecat out to email)
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • peter

                        #12
                        Re: Building thumbnail page (mysql)

                        I'll give it a try. It point's me in the right direction !
                        Is it necessary to create thumbnails beforehand, or is it possible to
                        resize the original images at runtime.
                        best doing it when the images are uploaded or added to the application.
                        Toying with images can be stressfull for the server


                        Comment

                        Working...