问题描述
我仍然是一个PHP的图像处理或文件处理的新手。
以下
我使用简单的html表单发布图像文件并通过php上传。
当我尝试和改变我的代码,以适应更大的文件(即调整大小)我得到一个错误。
已经在网上搜索,但无法找到任何真正简单的东西。
$ $ $ $ $ $ $ $ $ $ $文件格式($ _ FILES ['image' ] [ 'tmp_name的值']);
//将大小与我们定义的最大大小进行比较,如果大于
则打印错误if($ size == FALSE)
{
$ errors = 1;
} else if($ size [0]> 300){//如果width大于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生成
$ copy = imagecopyresized($ imgHolder,$ _FILES ['image'] ['tmp_name'],0,0,0,0,$ newWidth,$ newHeight,$ size [0],$ size [1]);
move_uploaded_file($ copy,$ newname); //我要将文件移动到$ newname
的位置,我得到的错误是:
$ b
I'm still a newbie regarding image handling or file handling for that matter in PHP.
Would appreciate any input regarding the following
I post an image file using a simple html form and upload it via php.When i try and alter my code to accomodate larger files (i.e. resize) I get an error.Have been searching online but cant find anything really simple.
$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
The error I get is:
Thanks in advance
Thanks for all your input, i've changed it to this
$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; }
Not getting a PHP log error but its not saving :(
Any ideas?
Thanks again
Result
Following works.
$oldImage = imagecreatefromjpeg($img); $imageHolder = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($imageHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg($imageHolder, $newname, 100);
Thanks for everyones help
imagecopyresized takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE to load it. For example, if it's a JPEG, use imagecreatefromjpeg and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring (which detects file types automatically) to load it as follows:
$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
这篇关于图像大小调整PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!