访问 PHP 中上传的文件信息并使用 GD 进行修改

问题描述 投票:0回答:1

我正在上传 $_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)

预先感谢您的任何建议。

php image file-upload multipartform-data gd
1个回答
0
投票

imagesx
imagesy
都需要打开的图像,即已经加载到内存中。
getimagesize
需要文件名,但
$image
是一个数组。您必须将
$_FILES['uploaded_file']['tmp_name']
传递给它。
此外,您必须先从上传的图像创建图像资源,然后才能重新采样。

$imagefile = $_FILES['uploaded_file']['tmp_name'];
var_dump($image);

$imagedetails = getimagesize($imagefile);
$oldw = imagedetails[0];
$oldh = imagedetails[1];

$width = "width".$imagedetails[0];
$height = "height".$imagedetails[1];
$neww = 512;
$newh = 512;
$image = imagecreatefromstring(file_get_contents($imagefile));
$temp = imagecreatetruecolor($neww, $newh);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $neww, $newh, $oldw, $oldh);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.