Image Width and Height into PHP Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Image Width and Height into PHP Variable

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image Width and Height into PHP Variable

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Width and Height into PHP Variable

Post 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?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Width and Height into PHP Variable

Post 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
rahi
Forum Newbie
Posts: 1
Joined: Thu Apr 14, 2016 9:06 am

Re: Image Width and Height into PHP Variable

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Image Width and Height into PHP Variable

Post by simonmlewis »

Where did they mention that? How do you use that then?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Image Width and Height into PHP Variable

Post by Celauran »

simonmlewis wrote:Where did they mention that?
requinix wrote:Try getimagesize instead.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Image Width and Height into PHP Variable

Post 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()
  }
}
Warning: I have no idea what I'm talking about.
Post Reply