문제

I'm using this function, to create thumbnails of images uploaded by the user, that I found here: 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 );
}

This function works fine and does exactly what I want it to, but, despite this, I'm still getting errors from it. See errors below:

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

The problem, I think, is that I'm passing an actual file, not just a directory, into the parameters for the function. This is the case for $pathtoimages and $pathtothumbs. The function is supposed to search through the directory passed to it to find all images with the .jpg extension. But I would like to just perform the function on the one image uploaded at the time of upload. Is there someway to edit this function to allow for this?

Thanks in advance

도움이 되었습니까?

해결책 2

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}" );

}

Think I prematurely posted this question. Thanks for help from everyone.

@csw looks like your solution may have worked, but I got mine working too so I didn't test it.

다른 팁

the $pathToImage must point to image file
remove
$dir = opendir( $pathToImages );
if (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);

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

replace {$pathToImages}{$fname} with $pathToImages only, since its the image file.

btw, this code is not verbose.

Quick and dirty:

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}" );
}

You have to use that function like this:

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

Replacing the arguments. Notice the word "path", it's a directory, like "../images/02", and you are using probably the path along with the picture name, like this:

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

And it should be:

createThumbs("images/008/01/" ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top