php - 图片调整大小 PHP

标签 php image file-upload resize

<分区>

Possible Duplicate:
Can anybody suggest the best image resize script in 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

我得到的错误是:

imagecopyresized(): supplied argument is not a valid Image resource in

提前致谢


谢谢你的意见,我已经改成了这个

$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;
}

没有收到 PHP 日志错误,但没有保存 :(

有什么想法吗?

再次感谢


结果

以下作品。

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

感谢大家的帮助

最佳答案

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

如果您不知道文件类型,也不会丢失所有内容。您可以将文件作为字符串读取并使用 imagecreatefromstring(自动检测文件类型)加载它,如下所示:

$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));

关于php - 图片调整大小 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3530946/

相关文章:

php - 使用mysql数据库中的最新值获取选择框以设置默认值

html - 是否可以更改文件上传html控件中 "Browse"按钮的样式?

iOS 7后台上传和POST请求

excel - 从使用 XLSX 上传的 excel 中获取 header

php - 从 Android 连接到 PHP 和 MySQL

php - 正则表达式的问题

swift - ALCameraViewController,用于裁剪图像的库

c++ - 在 C++ 中旋转 PNG 图像

c++ - 如何将一张图片叠加在另一张图片上? C++

php - Yii2模型规则,如何在使用存在的同时使用另一个表?