图像大小调整PHP [英] Image resize PHP

查看:130
本文介绍了图像大小调整PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

任何人都可以建议最好的图像调整脚本在PHP?


我仍然是一个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


imagecopyresized():提供的参数
不是
中的有效图像资源



在此先感谢




感谢您的所有意见,改变它到这个

  $ oldImage = imagecreatefromstring(file_get_contents($ _ FILES ['image'] ['tmp_name'])); 
$ copy = imagecopyresized($ imgHolder,$ oldImage,0,0,0,$ newWidth,$ newHeight,$ size [0],$ size [1]);
if(!move_uploaded_file($ copy,$ newname)){
$ errors = 1;



$ b

没有得到一个PHP日志错误,但没有保存:(



有什么想法?

再次感谢




结果

以下作品

  $ oldImage = imagecreatefromjpeg($ img); 
$ imageHolder = imagecreatetruecolor($ newWidth,$ newHeight);
imagecopyresized($ imageHolder,$ oldImage,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'])); 


Possible Duplicate:
Can anybody suggest the best image resize script in php?

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:

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆