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

PHP图像上传到文件夹并制作缩略图,php,Php,我有这个代码上传文件到一个文件夹(上传)。 在fileupload.php文件中,我有一个类型为file multiple的输入。 我使用包含以下代码的th文件(upload.php)将文件(图像)上载到文件夹(upload),这很好。 但我还想创建缩略图,以将其放入(上传/缩略图)。 我还需要设置这些缩略图的大小。多谢各位 到目前为止,我的代码是: <?php ready = "OK, Uppladdat"; uploaddir = "images/" . $imagepath; upl

我有这个代码上传文件到一个文件夹(上传)。 在fileupload.php文件中,我有一个类型为file multiple的输入。 我使用包含以下代码的th文件(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; 
?>

您可以使用此PHP模块设置缩略图的大小,还可以为图像添加不同的效果


到目前为止,您的目标解决方案是什么?Stackoverflow不是教程服务或代码编写服务。我们在这里帮助解决代码问题-这不是问题-这是一个不需要自己尝试就可以为您编写功能的请求。好的,谢谢。我已经在第一篇文章中更改了代码。现在我在“图像”文件夹中得到一张图像,在“图像/拇指”文件夹中得到一张图像。但问题是,最终位于thumbs文件夹中的图像的像素与主图像的像素一样大,并且实际大小越来越大(kb),原始图像约为78kb,但缩略图图像约为100kb。那么这里发生了什么。谢谢。谢谢,我还没有测试过imagick模块,但我马上就到了。我已经在第一篇文章中更改了代码。现在我在“图像”文件夹中得到一张图像,在“图像/拇指”文件夹中得到一张图像。但问题是,最终位于thumbs文件夹中的图像的像素与主图像的像素一样大,并且实际大小越来越大(kb),原始图像约为78kb,但缩略图图像约为100kb。那么这里发生了什么。谢谢。试试这个页面,它有很多方法。我想你在图像质量和重采样方面也有一些问题(imagemagick处理方法)