在PHP商店中的不同文件夹中创建缩略图

时间:2014-10-29 04:06:58

标签: php image file upload

您好我正在寻找与主图像一起创建缩略图。我希望它存储在与原始文件夹不同的文件夹中,最大高度为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';

2 个答案:

答案 0 :(得分:1)

以下代码需要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}" );
}

答案 1 :(得分:-1)

你可以这样做:

$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);

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