検索
ホームページ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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。