无法在PHP中调整多个图像的大小

无法在PHP中调整多个图像的大小,php,image,resize,Php,Image,Resize,我正在尝试将各种图像上载到服务器上动态创建的文件夹中,然后在将其上载到文件夹中的同时拍摄每个图像并调整其大小,同时创建一个新图像和一个新名称。。示例:image.jpg(原始图像)和image-resized.jpg(缩略图图像) 我搞不懂的是如何调整所有图像的大小。我不确定我是否应该把它放在一个循环中。我所需要的是我上传的每一张图片(每次可以上传5张)。它循环并调整所有图像的大小,而不仅仅是单个图像。这是我的代码,任何帮助都将不胜感激 创建文件夹并将图片移动到这些文件夹的代码: // Desi

我正在尝试将各种图像上载到服务器上动态创建的文件夹中,然后在将其上载到文件夹中的同时拍摄每个图像并调整其大小,同时创建一个新图像和一个新名称。。示例:image.jpg(原始图像)和image-resized.jpg(缩略图图像)

我搞不懂的是如何调整所有图像的大小。我不确定我是否应该把它放在一个循环中。我所需要的是我上传的每一张图片(每次可以上传5张)。它循环并调整所有图像的大小,而不仅仅是单个图像。这是我的代码,任何帮助都将不胜感激

创建文件夹并将图片移动到这些文件夹的代码:

// Desired folder structure
        $structure = './Fotos/'.$newfolder;

        // To create the nested structure, the $recursive parameter 
        // to mkdir() must be specified.

        if (!mkdir($structure, 0, true)) {
        die('Failed to create folders...');
}else{
    $placefoldername = mysql_query("INSERT INTO datefolders (FolderDate) VALUES ('$newfolder')") or die(mysql_error());
    echo "<div class=\"success\">El folder fue agregado con exito.<input type=\"button\" name=\"close\" value=\"X\" class=\"close\" /></div>";
    }}

// ...

    }

if(isset($_POST['upload'])){
    $FolderDate = $_POST['fecha-folder'];
    $FolderName = $_POST['FolderName'];
    $hour = $_POST['hour'];

        // Desired folder structure
        $structure = './Fotos/'.$FolderDate.'/'.$hour.'/'.$FolderName;

        // To create the nested structure, the $recursive parameter 
        // to mkdir() must be specified.


        for($i=0;$i<count($_FILES['fileupload']['name']);$i++) {
            $names = $_FILES['fileupload']['name'][$i];
            $target_path = "Fotos/".$FolderDate."/".$hour."/".$FolderName."/";

            $target_path = $target_path . basename( $_FILES['fileupload']['name'][$i]); 

            if(move_uploaded_file($_FILES['fileupload']['tmp_name'][$i], $target_path)) {
             $success = 1;
//所需的文件夹结构
$structure='./Fotos/'.$newfolder;
//要创建嵌套结构,$recursive参数
//必须指定to mkdir()。
if(!mkdir($structure,0,true)){
die('未能创建文件夹…');
}否则{
$placefoldername=mysql_查询(“插入到datefolders(FolderDate)值('$newfolder'))或死(mysql_error());
echo“El folder fue agregado con exito.”;
}}
// ...
}
如果(isset($_POST['upload'])){
$FolderDate=$_POST['fecha-folder'];
$FolderName=$_POST['FolderName'];
$hour=$_POST['hour'];
//所需的文件夹结构
$structure='./Fotos/'.$FolderDate'./'.$hour'./'.$FolderName;
//要创建嵌套结构,$recursive参数
//必须指定to mkdir()。

对于($i=0;$i为什么要在
for
循环中定义函数
resizeImage
?每次循环迭代时都会重新定义它。这可能是问题的一部分。在循环外定义函数,看看是否可行。

您可以尝试


当您的第一个映像处理结束时使用
imagedestroy()
然后将处理第二个图像。

您的函数声明实际上是在foreach循环中,还是复制/粘贴错误?如果它在循环中,那么您将遇到双重函数定义错误。错误!!刚刚取下:PSorry我刚取下..这只是我在玩的东西,之前忘了取下它重新发布。我找到了!!!我需要将函数从for循环中取出,然后只调用for循环中的函数。无论如何,谢谢大家!我已经在原始映像和临时映像上使用imagedestroy了。我是否应该将此添加到$img和$imgPath?非常感谢!
$img = $names;   
        $imgPath = $structure;


        function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
    //Create a thunbnail image by resizing the picture
    // Open the original image.
    $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
    list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");

    // Determine new width and height.
    $newWidth = ($width/$by);
    $newHeight = ($height/$by);

    // Resample the image.
    $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
    imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");

    // Create the new file name.
    $newNameE = explode(".", $img);
    $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';

    // Save the image.
    imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");

    // Clean up.
    imagedestroy($original);
    imagedestroy($tempImg);
    return true;
}
$resize = resizeImage($img, $imgPath, "-resized", 23, 100);