1

I am uploading an image that is included in the $_FILES object. However, when I attempt to get its size, I get the error that it is not a string or alternatively not a resource. How can I get the size of this image and, in turn, modify it in GD? Do I have to convert it to a string? Or what do these methods want as their input?

$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)
2
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. Commented Jan 12 at 22:35
  • To explain my boiler plate advice above: questions here (and their answers) are recorded for posterity. To that end, we ask authors to omit fluff and chat, leaving succinct material for readers to consume. It is thus standard practice to keep questions clear of thanks and other similar conversational material. Commented Jan 12 at 22:37

1 Answer 1

2

Both imagesx and imagesy require an opened image, i.e. already loaded into memory.
getimagesize requires a filename but $image is an array. You have to pass $_FILES['uploaded_file']['tmp_name'] to it.
Also, you have to create an image resource from the uploaded image before you can resample it.

$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);
Sign up to request clarification or add additional context in comments.

2 Comments

I will give it a try. Out of curiosity, where is the actual image data?
imagedetails in lines 4 and 5 need $ signs but with that correction this worked. I guess, the $_FILES array does not have the actual data, only the location from which you can get it as a string using imagecreatefromstring(file_get_contents($imagefile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.