我正在上传包含在 $_FILES 对象中的图像。但是,当我尝试获取其大小时,我收到错误消息,提示它不是字符串或不是资源...我如何获取此图像的大小,然后在 GD 中修改它。我必须将其转换为字符串吗?或者这些方法需要什么作为输入。
$image = $_FILES['uploaded_file'];
var_dump($image);
$oldw = imagesx($image);
$oldh = imagesy($image);
$imagedetails = getimagesize($image);
$width = "width".$imagedetails[0];
$height = "height".$imagedetails[1];
$neww = 512;
$newh = 512;
$temp = imagecreatetruecolor($neww, $newh);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $neww, $newh, $oldw, $oldh);
//Error messages:
imagesx() expects parameter 1 to be resource
imagesy() expects parameter 1 to be resource
getimagesize() expects parameter 1 to be string
//Here is what the var_dump of image
array(5) {
["name"]=>
string(14) "image.png"
["type"]=>
string(24) "application/octet-stream"
["tmp_name"]=>
string(14) "/tmp/phpLlon22"
["error"]=>
int(0)
["size"]=>
int(2743914)
提前感谢任何建议。
imagesx
和都imagesy
需要打开图像,即已加载到内存中。getimagesize
需要文件名,但是$image
是数组。您必须传递$_FILES['uploaded_file']['tmp_name']
给它。此外,您必须从上传的图像创建图像资源,然后才能对其进行重新采样。