Page 1 of 1

Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 8:29 am
by simonmlewis
We are trying to make sure people upload an image into the system that is exactly the dimensions we provide.

To do this, I've tried this code for the width:

Code: Select all

$uploadedfile = $_FILES['photo']['tmp_name'];
$width = imagesx($uploadedfile);
if ($width > 1920)
{
	     echo " <script>
  window.location.replace('/a_productedit&id=$id&error=width')
  </script>";
}
But it doesn't work. It just uploads it anyway.

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 8:56 am
by requinix
If you look at the documentation for imagesx then you should see that you're nowhere close to using it correctly.

Try getimagesize instead.

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 9:02 am
by Celauran
simonmlewis wrote:We are trying to make sure people upload an image into the system that is exactly the dimensions we provide.
Wouldn't it be easier to just resize it?

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 9:04 am
by simonmlewis
Nope - as we don't want to distort the image. And we have been told the client will upload and not to resize it in code.

I know.... !! Hence I just wana grab the size if it and tell them off if they are wrong!

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 9:14 am
by rahi
Someone above suggested php getimagesize function. It will give you all image attributes and you can extract height and width for validation. Make sure to check for file existence first otherwise getimagesize function will run forever.

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 9:16 am
by simonmlewis
Where did they mention that? How do you use that then?

Re: Image Width and Height into PHP Variable

Posted: Thu Apr 14, 2016 9:18 am
by Celauran
simonmlewis wrote:Where did they mention that?
requinix wrote:Try getimagesize instead.

Re: Image Width and Height into PHP Variable

Posted: Fri Apr 29, 2016 10:45 am
by thinsoldier
simonmlewis wrote:We are trying to make sure people upload an image into the system that is exactly the dimensions we provide.

To do this, I've tried this code for the width:

Code: Select all

$uploadedfile = $_FILES['photo']['tmp_name'];
$width = imagesx($uploadedfile);
if ($width > 1920)
{
	     echo " <script>
  window.location.replace('/a_productedit&id=$id&error=width')
  </script>";
}
But it doesn't work. It just uploads it anyway.
I assume that after this section of code you have another section of code that copies the uploaded file from its ['tmp_name'] location to a folder within your project where it will live permanently. If that is the case you are not doing anything within this code that will stop the next section of code if the image is the wrong size.

Code: Select all

if ( temporary file was uploaded )
{
  if ( temporary file width or height is wrong )
  {
     then ( show error message and abort any further execution of code via exit() or return )
  }
  else 
  {
    there was nothing wrong with image size so move temporary file to permanent location via move_uploaded_file()
  }
}