<?php
$photo="sunflower.jpg";
$cmd = "convert $photo -quality 50 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
独自のページで、1 つの画像のみを表示できます。
<img src="php_page_containing_above_code.php?photo=sunflower.jpg">
そしてもっとたくさんの画像があります。コードの先頭にある $photo 変数をコメントアウトします。現時点では正確なコードを思い出せず、仕事中なのでテストできません。$photo = $_GET['photo']; は必要ないと思います。しかし、私はこの方法を使用していないので、思い出せません。
OPが最初の部分が機能しなかったと言ったときに追加されたコード例。
これを image.php として保存します
<?php
$photo = $_REQUEST['photo'];
$cmd = "convert $photo -quality 50 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
これを好きなように保存して実行します:
<?php
// Directory to read images from
$dir = "background/";
// Read the directory and sellect EVERYTHING
$filenames = glob( "$dir*" );
// Start the loop to display the images
foreach ( $filenames as $filename ) {
// Display the images
echo "<img src=\"image.php?photo=".$filename."\" />\n";
}
?>
代替コード:
ファイル 1 - 画像のサイズを変更します (ディレクトリで 2 回以上実行しないでください。そうしないと、サムの量が 2 倍になります!)
// Read the directory and sellect jpg only
$filenames = glob("$dir*.{jpg,JPG}", GLOB_BRACE);
// Start the loop to resize the images
foreach ( $filenames as $filename ) {
// New name
$name = explode ( '/', $filename );
$newname = $name[0]."/th_".$name[1];
//Resize and save in the same directory
//exec("convert $filename -resize 400x400 -quality 50 $newname");
}
?>
ファイル 2 - 画像の表示
// Read all the image files into an array that start with th_
$filenames = glob("$dir/th_*.{jpg,JPG}", GLOB_BRACE);
// Display the array contents
foreach ( $filenames as $value ){ echo "<img src=\"$value\"><br>"; }
?>