0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サムネール画像を生成する(コピペOK)

0
Posted at

jpegファイルのサムネール画像を生成します。

①以下のコードを書きます
②mkthumbs.phpのサブディレクトリとして pics と thumbs を作成します
③picsにjpeg画像を置きます
④コマンドラインで
  % php mkthumbs.php
と入力します
⑤index.htmlをブラウザで開きます

mkthumbs.php
<?php
$dir = opendir("pics");
$pics = array();
while($fname = readdir($dir)){
  if(preg_match("/[.]jpg$/", $fname))
    $pics[] = $fname;
}
closedir($dir);

foreach($pics as $fname){
  $im = imagecreatefromjpeg("pics/$fname");
  $ox = imagesx($im);
  $oy = imagesy($im);

  $nx = 100;
  $ny = floor($oy * (100 / $ox));

  $nm = imagecreatetruecolor($nx, $ny);

  imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $ox, $oy);

  print $fname." のサムネールを生成します...\n";

  imagejpeg($nm, "thumbs/$fname");
}

print "index.html を生成しています...\n";

ob_start();
?>
<html>
<head><title>サムネール</title></head>
<body>
<table cellspacing="0" cellpadding="2" width="500">
<tr>
<?php
$index = 0;
foreach($pics as $fname){
?>
<td valign="middle" align="center">
<a href="pics/<?php echo($fname); ?>"><img src="thumbs/<?php echo($fname);
  ?>" border="0" /></a>
</td>
<?php
$index += 1;
if($index % 5 == 0) echo("</tr><tr>");
}
?>
</tr>
</table>
</body>
</html>
<?php
$html = ob_get_clean();
$fh = fopen("index.html", "w");
fwrite($fh, $html);
fclose($fh);
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?