【www.gdgbn.com--php安装】

本教程将介绍如何创建使用PHP的飞行缩略图。此外您将学习如何处理的图像整个文件夹,并创建自己的缩略图。因为这需要GD库,您将需要至少有一个广东2.0.1 PHP安装启用。

下面我们将创建一个PHP脚本,它包含两种功能。第一个扫描所提供的任何目录。JPG图像,以及对每一个,创建一个指定的文件夹缩略图使用GD的图像功能。第二个函数中创建一个作为脚本,它包含了一些原始图像的链接的缩略图同一目录中的所有HTML文件。这可能是先进的图片库软件的基础。

下面的代码创建一个名为createThumbs,将获得3个参数的函数。第一和第二的是相应的目录,其中包含原始图像和进行该缩略图将被放置的目录路径。第三个参数是你的宽度为缩略图想要的。


function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info["extension"]) == "jpg" )
    {
      echo "Creating thumbnail for {$fname}
";

      // 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}" );
    }
  }
  // close the directory
  closedir( $dir );
}
createThumbs("upload/","upload/thumbs/",100);
?>

先,我们通过它打开的图像和遍历目录,都在寻找。JPG文件。接下来,我们创建的目录中的每个图像的缩略图。要创建一个缩略图,我们在阅读使用imagecreatefromjpeg()函数文件,并计算新的缩略图大小。 imagesx()和imagesy()函数返回的宽度和高度分别形象。接下来,我们创建一个新的形象,用imagecreatetruecolor()。最后,我们复制并调整其大小与imagecopyresized()函数的原始文件和保存imagejpeg缩略图()。

代码的第二部分,创建一个名为createGallery它获取两个参数的函数(在目录中的图片和缩略图的存储位置),并创建一个HTML网页,其中包含了一些原始图像的链接的缩略图所有的相对路径。


function createGallery( $pathToImages, $pathToThumbs )
{
  echo "Creating gallery.html
";

  $output = "";
  $output .= "Thumbnails";
  $output .= "";
  $output .= "";
  $output .= "";

  // open the directory
  $dir = opendir( $pathToThumbs );

  $counter = 0;
  // loop through the directory
  while (false !== ($fname = readdir($dir)))
  {
    // strip the . and .. entries out
    if ($fname != "." && $fname != "..")
    {
      $output .= "";
      $output .= "";
      $output .= "";

      $counter += 1;
      if ( $counter % 4 == 0 ) { $output .= ""; }
    }
  }
  // close the directory
  closedir( $dir );

  $output .= "";
  $output .= "";
  $output .= "";
  $output .= "";


  $fhandle = fopen( "gallery.html", "w" );
 
  fwrite( $fhandle, $output );

  fclose( $fhandle );
}

createGallery("upload/","upload/thumbs/");
?>

本文来源:http://www.gdgbn.com/jiaocheng/23509/