PHP映像上传到文件夹并制作缩略图

时间:2018-10-21 11:55:24

标签: php

我有此代码可以将文件上传到文件夹(上传)。 在文件fileupload.php中,我输入了多个文件类型。 我使用包含以下代码的文件(upload.php)将文件(图像)上传到文件夹(upload),并且工作正常。 但我也想创建缩略图以放入(上传/缩略图)。 而且我还需要设置这些缩略图的大小。谢谢。

到目前为止,我的代码:

<?php
ready = "OK, Uppladdat";
uploaddir = "images/" . $imagepath;
uploaddir_th = "images/thumbs/" . $imagepath;
allowed = array('jpg','jpeg','gif','png');
max_size = 5048 * 1024;
while (list ($key, $val) = each ($_FILES))
{
if ($_FILES[$key]['size'] <= $max_size) 
{
$file_ext  = pathinfo($_FILES[$key]['name'],PATHINFO_EXTENSION);
$file_name = basename($_FILES[$key]['name'],'.'.$file_ext);
if (in_array(strtolower($file_ext),$allowed)) 
{
$name = $_FILES[$key]['name'];
$x = 1;
while (file_exists($uploaddir.'/'.$name)) 
{
   $name = $file_name.'['.$x.'].'.$file_ext;
   $x++;
}
if (move_uploaded_file($_FILES[$key]['tmp_name'],$uploaddir.'/'.$name))
{
   chmod($uploaddir.'/'.$name, 0644);
}
else
{
die(error_get_last());
}
}
else
{
   die("Invalid file type");
}
}
else
{
  die("File size too big");
}

//thumbnail image making part

list($width, $height) = getimagesize($uploaddir.'/'.$name);
$modwidth = 200;
$modheight = 140;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($uploaddir.'/'.$name);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight);
/* echo "Thumbnail: <img src='images/thumbs".$imagepath."'>"; */
imagejpeg($image, $uploaddir_th.'/'.$name, 100);
}
echo $ready; 
?>

1 个答案:

答案 0 :(得分:1)

您可以使用此PHP模块并设置缩略图的大小,还可以向图像http://php.net/manual/en/imagick.thumbnailimage.php

添加不同的效果

http://php.net/manual/en/book.imagick.php

相关问题