图像大小调整-PHP中的imagecopyreseampled错误


image resizing - imagecopyresampled error in PHP

我在这里不知所措。我已经成功地上传了所有内容,并使用imagejpeg()命令调整了文件的质量,然而,我的imagecopyresampled函数似乎给了我一个错误:

警告:imagecopyresampled((要求参数1为资源,在第394行给出字符串

    $imgRaw = $_FILES[$file]['name'];
    $imgRawTemp = $_FILES[$file]['tmp_name'];
    $nameExtract = explode(".", $imgRaw);
    $ext = $nameExtract[count($nameExtract)-1];
    $imgAll = getimagesize($_FILES[$file]['tmp_name']);
    $uploadedName = time().uniqid()."_original.";
    $dir = "usrPld/";
    $thisImg = $dir.$uploadedName.$ext;
    if($imgAll['mime'] == 'image/jpeg' || $imgAll['mime'] == 'image/png')
    {
        if(move_uploaded_file($imgRawTemp, $thisImg))
        {
            list($width, $height, $type, $attr) = $imgAll;
            $thumbnailWidth = 250;
            $viewingWidth = 910;
            $thumbHeight = $thumbnailWidth*($height/$width);
            $viewingHeight = $viewingWidth*($height/$width);
            if($imgAll['mime'] == 'image/jpeg')
            {
                $image = imagecreatefromjpeg($thisImg);
            }
            else if($imgAll['mime'] == 'image/png')
            {
                $image = imagecreatefrompng($thisImg);
            }
            $newName = time().uniqid().".jpg";
            $newName2 = time().uniqid().".jpg";
            if($width > $viewingWidth)
            {
                if(imagejpeg($image, $dir.$newName, 100))
                {
                    if(imagecopyresampled($dir.$newName2, 
                            $dir.$newName, 
                            0, 0, 0, 0, 
                            $viewingWidth, 
                            $viewingHeight, 
                            $width, 
                            $height))
                    {
                        unlink($thisImg);
                        unlink($dir.$newName);
                    }
                }
            }
            else
            {
                if(imagejpeg($image, $dir."no_".$newName, 100))
                {
                    unlink($thisImg);
                }
            }
        }
    }
    else
    {
        return "format error";
    }

奇怪的是,我检查了$height(因为这是错误指向的地方(,它输出了一个应该输出的数字。

感谢您提前提供的帮助。

$height不是您的问题。您将目录路径作为前两个参数传递给imagecopyrasampled((,但它们必须是图像资源。所以你需要先做这样的事情:

$destImage = imagecreatetruecolor($viewingWidth, $viewingHeight);
$sourceImage = imagecreatefromjpeg($dir.$newName);

然后将它们传递到您的函数中:

if(imagecopyresampled($destImage, 
                        $sourceImage, 
                        0, 0, 0, 0, 
                        $viewingWidth, 
                        $viewingHeight, 
                        $width, 
                        $height))

然后,大概您想将$destImage写入$dir$newName2路径。