图像调整PHP大小

Ste*_*cus 10 php resize file-upload image

可能重复:
有人可以在php中建议最好的图像大小调整脚本吗?

在PHP中,我仍然是关于图像处理或文件处理的新手.

非常感谢有关以下内容的任何意见

我使用简单的html表单发布图像文件并通过php上传.当我尝试更改我的代码以容纳更大的文件(即调整大小)时,我收到一个错误.一直在网上搜索,但无法找到任何非常简单的东西.

$size = getimagesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size == FALSE)
{
    $errors=1;
}else if($size[0] > 300){   //if width greater than 300px
    $aspectRatio = 300 / $size[0];
    $newWidth = round($aspectRatio * $size[0]);
    $newHeight = round($aspectRatio * $size[1]);
    $imgHolder = imagecreatetruecolor($newWidth,$newHeight);
}

$newname= ROOTPATH.LOCALDIR."/images/".$image_name; //image_name is generated

$copy = imagecopyresized($imgHolder, $_FILES['image']['tmp_name'], 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
move_uploaded_file($copy, $newname); //where I want to move the file to the location of $newname
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

imagecopyresized():提供的参数不是有效的Image资源

提前致谢


感谢您的所有输入,我已将其更改为此

$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
$copy = imagecopyresized($imgHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]);
if(!move_uploaded_file($copy, $newname)){
    $errors=1;
}
Run Code Online (Sandbox Code Playgroud)

没有得到PHP日志错误但它没有保存:(

有任何想法吗?

再次感谢


结果

以下作品.

$oldImage = imagecreatefromjpeg($img);
$imageHolder = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($imageHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($imageHolder, $newname, 100);
Run Code Online (Sandbox Code Playgroud)

感谢大家的帮助

Sam*_*war 5

imagecopyresized将图像资源作为其第二个参数,而不是文件名.您需要先加载文件.如果您知道文件类型,则可以使用imagecreatefromFILETYPE它来加载它.例如,如果它是JPEG,请使用imagecreatefromjpeg并传递文件名 - 这将返回图像资源.

如果您不知道文件类型,则不会丢失所有文件.您可以以字符串形式读取文件并使用imagecreatefromstring(自动检测文件类型)加载它,如下所示:

$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
Run Code Online (Sandbox Code Playgroud)