thumbnail generation via php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LockesRabb
    Junior Member
    • Jan 2006
    • 4

    #1

    thumbnail generation via php

    I wrote a thumbnail function that basically did the following:

    1. Check to see if the given image has a corresponding thumbnail
    2. If so, skip to 4
    3. If not, generate thumbnail
    4. Display thumbnail

    The thumbnails aren't being generated. Here's the function, it's driving me bonkers, I seriously can't see what's wrong with it, maybe you guys can... Feel free to use the function for urself if you want. It's commented so you guys can follow it.

    Code:
        function thumbit($filename){
            if($filename){
                if (file_exists($filename)) {
                    $condir = substr($filename, -(strlen(basename($filename))));
                    $origfilename = basename($filename);
                    if(!file_exists($condir.'tmb_'.$origfilename)){
                        #thumbnail doesn't exist, generate it, save it, and display it.
                        #Name you want to save your file as
                        $save = $condir.'tmb_'.$origfilename;
                        $file = $filename;
                        $size = 0.10;
                        header('Content-type: image/jpeg') ;
                        list($width, $height) = getimagesize($file) ;
                        $modwidth = 180; #$width * $size;
                        $modheight = 120; #$height * $size;
                        $tn = imagecreatetruecolor($modwidth, $modheight) ;
                        $image = imagecreatefromjpeg($file) ;
                        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
                        
                        #Here we are saving the .jpg, you can make this gif or png if you want
                        //the file name is set above, and the quality is set to 100%
                        imagejpeg($tn, $save, 100) ;
                    } else {
                        #thumbnail already exists, skip generation and display it
                        $image = imagecreatefromjpeg($condir.'tmb_'.$origfilename);
                        imagejpeg($image, null, 100);
                    }
                } else {
                    echo "ThumbIt Function Error: The file $filename does not exist.";
                }
            } else {
                echo "ThumbIt Function Error: Please provide an image to generate a thumbnail of.";
            }
        }
    Any ideas?
  • Buddha
    Senior Member
    • Mar 2004
    • 825

    #2
    PHP Code:
    $condir substr($filename, -(strlen(basename($filename)))); 
    That returns the filename doesn't it? But you want the directory, right?
    Think you want ...
    PHP Code:
    $condir substr($filename0, -(strlen(basename($filename)))); 
    Originally posted by THE MANUAL
    imagejpeg

    (PHP 3 >= 3.0.16, PHP 4, PHP 5)
    imagejpeg -- Output image to browser or file
    Description
    bool imagejpeg ( resource image [, string filename [, int quality]] )
    ...
    To skip the filename argument in order to provide a quality argument just use an empty string ('').
    Your using null in the second imagejpeg() but that might not matter ... till you get a thumbnail saved.

    Good Luck!
    "Whatcha mean I shouldn't be rude to my clients?! If you want polite then there will be a substantial fee increase." - Buddha

    Comment

    • LockesRabb
      Junior Member
      • Jan 2006
      • 4

      #3
      Thanks for the solution, budda- it saves thumbnails properly now. However, the thumbnail generation for saving thumbnails wasn't displaying thumbnail after saving it, but I figured out a solution. Function works perfectly now. Here's new code:

      Code:
          function thumbit($filename){
              if($filename){
                  if (file_exists($filename)) {
                      $condir = substr($filename, 0, -(strlen(basename($filename))));
                      $origfilename = basename($filename);
                      if(!file_exists($condir.'tmb_'.$origfilename)){
                          #thumbnail doesn't exist, generate it, save it, and display it.
                          //Name you want to save your file as
                          $save = $condir.'tmb_'.$origfilename;
                          $file = $filename;
                          $size = 0.10;
                          header('Content-type: image/jpeg') ;
                          list($width, $height) = getimagesize($file) ;
                          $modwidth = 180; #$width * $size;
                          $modheight = 120; #$height * $size;
                          $tn = imagecreatetruecolor($modwidth, $modheight) ;
                          $image = imagecreatefromjpeg($file) ;
                          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
                          
                          // Here we are saving the .jpg, you can make this gif or png if you want
                          //the file name is set above, and the quality is set to 100%
                          imagejpeg($tn, $save, 100) ;
                          $image = imagecreatefromjpeg($condir.'tmb_'.$origfilename);
                          imagejpeg($image, '', 100);
                      } else {
                          #thumbnail already exists, skip generation and display it
                          $image = imagecreatefromjpeg($condir.'tmb_'.$origfilename);
                          imagejpeg($image, '', 100);
                      }
                  } else {
                      echo "ThumbIt Function Error: The file $filename does not exist.";
                  }
              } else {
                  echo "ThumbIt Function Error: Please provide an image to generate a thumbnail of.";
              }
          }
      For the curious, the function above goes in functions.php, then the following code goes in a file called thumbit.php:

      Code:
      <?
          include 'functions.php';
          #Get thumbnail of given target
          $target = $_REQUEST['target'];
          thumbit($target);
      ?>
      Then whenever you wanna display the thumbnails of images without worrying whether the thumbnail of it exists (it's created on the fly if it doesn't exist), you would display the image something like this:

      Code:
      <img src="thumbit.php?target=<?=$dir.$files1[$i];?>
      Enjoy! Thanks for your help budda!

      Comment

      Working...