教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 创建缩略图实例教程

php 创建缩略图实例教程

发布时间:2016-10-28   编辑:jiaochengji.com
教程集为您提供php 创建缩略图实例教程等资源,欢迎您收藏本站,我们将为您提供最新的php 创建缩略图实例教程资源

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

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

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


<?php
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} <br />";

      // 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网页,其中包含了一些原始图像的链接的缩略图所有的相对路径。


<?php
function createGallery( $pathToImages, $pathToThumbs )
{
  echo "Creating gallery.html <br />";

  $output = "<html>";
  $output .= "<head><title>Thumbnails</title></head>";
  $output .= "<body>";
  $output .= "<table cellspacing="0" cellpadding="2" width="500">";
  $output .= "<tr>";

  // 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 .= "<td valign="middle" align="center"><a href="{$pathToImages}{$fname}">";
      $output .= "<img src="{$pathToThumbs}{$fname}" border="0" />";
      $output .= "</a></td>";

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

  $output .= "</tr>";
  $output .= "</table>";
  $output .= "</body>";
  $output .= "</html>";


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

  fclose( $fhandle );
}

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

您可能感兴趣的文章:
php如何实现调整图片大小或创建缩略图
php 创建缩略图实例教程
php缩略图填充白边的示例代码
php绘图不显示图片怎么办
PHP原生写的生成图片缩略图类
php生成缩略图的例子
PHP等比例缩放图片生成缩略图函数的例子
php 缩略图类(附调用示例)
windows下开启PHP GD库的方法
php图片文件上传类(可自动生成缩略图)

[关闭]
~ ~