搜索
首页php教程PHP源码php 创建缩略图实例教程

<script>ec(2);</script>

本教程将介绍如何创建使用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 .= "

";

      $counter += 1;
      if ( $counter % 4 == 0 ) { $output .= "

"; }
    }
  }
  // close the directory
  closedir( $dir );

  $output .= "

";
  $output .= "
";
      $output .= "php 创建缩略图实例教程";
      $output .= "
";
  $output .= "";
  $output .= "";


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

  fclose( $fhandle );
}

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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用