gpt4 book ai didi

php - 图片调整大小 PHP

转载 作者:可可西里 更新时间:2023-10-31 22:46:03 24 4
gpt4 key购买 nike

<分区>

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);

感谢大家的帮助

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com