在不同文件夹中的PHP存储中创建缩略图

在不同文件夹中的PHP存储中创建缩略图,php,image,file,upload,Php,Image,File,Upload,您好,我正在创建一个缩略图以及我的主图像。我想它被存储在一个不同的文件夹比原来的,是一个最大高度为400px。我到处找了找,但这一切似乎太复杂了。我在找一些相当简单的东西。我是新的文件上传的东西。下面是我目前拥有的代码 // Configuration $allowed_filetypes = array('.jpg', '.JPG', '.bmp', '.png', '.gif'); $max_filesize = 100000000; $upload_path = 'artima

您好,我正在创建一个缩略图以及我的主图像。我想它被存储在一个不同的文件夹比原来的,是一个最大高度为400px。我到处找了找,但这一切似乎太复杂了。我在找一些相当简单的东西。我是新的文件上传的东西。下面是我目前拥有的代码

// Configuration
  $allowed_filetypes = array('.jpg', '.JPG', '.bmp', '.png', '.gif');
  $max_filesize = 100000000;
  $upload_path = 'artimages/';

  $filename = $_FILES['userfile']['name'];
  $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

// Random Number
  $randomnumber = rand(1, 1000000);
  $filename = $randomnumber.$filename;

// File Size
  if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
     die('The file you attempted to upload is too large.');

// File Type
  if(!in_array($ext,$allowed_filetypes))
     die('The file you attempted to upload is not allowed.');

// Check CHMOD 777
  if(!is_writable($upload_path))
     die('Fail');

// Upload File
  if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
        echo 'Success';
     else
        echo 'Fail';

下面的代码需要PHP-GD

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

你可以这样做:

$tmp_path = /*the temp path of your picture*/;
$imagePath = /*the path of you uploaded picture*/;
$Thumbnail = /*the path of your thumbnail folder*/;

move_uploaded_file($tmp_path, $imagePath);
copy($imagePath, $thumbnailPath);

因此,您将上载图像,然后将其复制到其他位置。

我还需要调整缩略图的大小。此答案不会调整图像的大小。哦,是的,对不起!因此,科林·肖恩发布的答案将是完美的。它奏效了,谢谢。如果原始图像小于所选的拇指宽度,会发生什么情况?