1

ここで見つけた、ユーザーがアップロードした画像のサムネイルを作成するために、この関数を使用しています: http://webcheatsheet.com/php/create_thumbnail_images.php :

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
if (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
imagecopyresampled( $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 );
}

この関数は正常に動作し、私が望んでいることを正確に実行しますが、それにもかかわらず、まだエラーが発生します。以下のエラーを参照してください。

Warning: opendir(images/008/01/0000288988r.jpg,images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: The directory name is invalid. (code: 267)

Warning: opendir(images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: failed to open dir: No error

Warning: readdir() expects parameter 1 to be resource, boolean given

問題は、ディレクトリだけでなく実際のファイルを関数のパラメーターに渡していることだと思います。これは と の場合$pathtoimagesです$pathtothumbs。この関数は、渡されたディレクトリを検索して、.jpg拡張子を持つすべての画像を見つけることになっています。しかし、アップロード時にアップロードされた1つの画像に対して機能を実行したいと思います。これを可能にするためにこの関数を編集する方法はありますか?

前もって感謝します

4

4 に答える 4

1


$ pathToImageは画像ファイルを指している必要があります
$dir = opendir( $pathToImages );
if (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);

追加$info = pathinfo($pathtoImages); // for the file name
$fname = $info['filename']

画像ファイルなので、only に{$pathToImages}{$fname}置き換えます。$pathToImages

ところで、このコードは冗長ではありません。

于 2013-03-21T15:54:25.570 に答える
1
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}" );
$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
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}" );

}

この質問を時期尚早に投稿したと思います。みんなからの助けに感謝します。

@cswはあなたのソリューションがうまくいったように見えますが、私もうまくいったのでテストしませんでした。

于 2013-03-21T16:01:40.650 に答える
0

速くて汚い:

function createThumb( $pathToImage, $pathToThumb, $thumbWidth )
{
  $fname = $pathToImage;
  echo "Creating thumbnail for {$fname} <br />";

  // load image and get image size
  $img = imagecreatefromjpeg( "{$pathToImage}{$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
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

  // save thumbnail into a file
  imagejpeg( $tmp_img, "{$pathToThumb}{$fname}" );
}
于 2013-03-21T15:33:55.233 に答える
-1

この関数は次のように使用する必要があります。

createThumbs("path_to_image", "path_to_thumb", "thumb_width");

引数を置き換える。「パス」という単語に注意してください。これは「../images/02」のようなディレクトリであり、おそらく次のように画像名とともにパスを使用しています。

createThumbs("images/008/01/0000288988r.jpg", " ......

そしてそれは次のようになります:

createThumbs("images/008/01/" ...
于 2013-03-21T15:34:01.737 に答える