Jump to content


Photo

Checking uploaded image's pixel dimensions


  • Please log in to reply
9 replies to this topic

#1 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 August 2006 - 08:27 PM

Hi, I'm using a form to upload a jpeg image to my server via PHP (see code). I'd like to rename the jpeg as title.jpg before uploading it to the server and also I'd like to reject the image if it isn't exactly 500x375 pixels.

I've searched the net but can only find a few tutorials about renaming files with random file names in order to avoid overwriting old ones but I actually want to overwrite old image files so I want to rename files to a specific name rather than to a random one. I can't find much either about checking an image's pixel dimensions either.

Thanks – leao

<?php

$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;


if ($uploaded_size > 70000)
{
echo "Your file is too large.<br>";
$ok=0;
}

if (!($uploaded_type=="image/jpeg")) {
echo "You may only upload JPEG files.<br>";
$ok=0;
}

if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}


else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".

basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>


#2 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 23 August 2006 - 09:00 PM

I built something about a couple years ago that did this exact thing. here's a some bits and pieces. If you need more, let me know.

// GET IMAGE ATTRIBUTES
list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($import);



// CHECK IMAGE MIME TYPE TO ENSURE IT'S A JPEG
if ($ImageMimeType != 2) {
	echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> This image does not appear to be JPG format.";
	
	if ($ImageMimeType == 1) {$WrongType = 'GIF';}
	if ($ImageMimeType == 3) {$WrongType = 'PNG';}
	if ($ImageMimeType == 4) {$WrongType = 'SWF';}
	if ($ImageMimeType == 5) {$WrongType = 'PSD';}
	if ($ImageMimeType == 6) {$WrongType = 'BMP';}
	if ($ImageMimeType == 7 || $ImageMimeType == 8) {$WrongType = 'TIFF';}
	if ($ImageMimeType > 8) {$WrongType = 'UNKNOWN FORMAT';}
	
	echo "<P>It appears to be a $WrongType. Please upload JPG format only.";
	echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>";
	unlink($import);
	exit;
}


// CHECK FILE WIDTH and HEIGHT FOR ACCURACY
if ($ImportWidth > $ImportHeight) {


	if ($ImportWidth != 1125 && $ImportHeight != 675) {
		echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> The image you are uploading is not the proper size.";
		echo "<P>It appears to be $ImportWidth x $ImportHeight.";
		echo "<P>It should be 1125 x 675 for a horizontal card at 300dpi. <P>Please correct and re-upload.</FONT>";
		echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>";
		unlink($import);
		exit;
	}
}
else {
	
	if ($ImportWidth != 675 && $ImportHeight != 1125) {
		echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> The image you are uploading is not the proper size.";
		echo "<P>It appears to be $ImportWidth x $ImportHeight.";
		echo "<P>It should be 1125 x 675 for a vertical card at 300dpi.<P>Please correct and re-upload.</FONT>";
		echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>";
		unlink($import);
		exit;
	}
}

You will need GD installed, of course. As for renaming the upload file, here you go:

//copy the file to permanent location
copy($import, "path/to/images/$VarName.jpg");


#3 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 August 2006 - 09:32 PM

The file is renamed as it's uploaded now, thanks!

I'm new to PHP so I'm afraid your code for checking the image's pixel dimensions is perplexing. Is there a way of checking an image's pixel dimensions in a similar way to how I check the image's file size below, using getimagesize() for example?

if (!($uploaded_type=="image/jpeg")) {
echo "You may only upload JPEG files.<br>";
$ok=0;
}

if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

Cheers,

Leao

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 August 2006 - 09:35 PM

Is there a way of checking an image's pixel dimensions in a similar way to how I check the image's file size below, using getimagesize() for example?


Yup. The first line of code posted earlier:

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($filename);

Legend has it that reading the manual never killed anyone.
My site

#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 23 August 2006 - 09:39 PM

A little cavet, you mentioned a specific size (height and width). If you set both parameters, it will most likely result in an image that is somewhat distorted.

Just in case, here is a little ditty that should be able to handle the majority if not all of what you are looking to accomplish.

http://www.nstoia.co...esize/index.php

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#6 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 August 2006 - 10:07 PM

I don't really want to resize images, just to check if they're the correct dimensions and then ask the user to reupload the images at the right dimensions if they're not.

I tried the getimagesize() function but it didn't work. Even if the image was exactly 500 pixels in width I still got the 'Your file is not the correct width...' error message and also:

PHP Warning: getimagesize(image.jpg): failed to open stream: No such file or directory in C:\hshome\mydomain\mydomain.org\test\upload.php on line 7


The php I tried was:
list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['name']) ; 

if ($ImportWidth != 500)
{
echo "Your file is not the correct width of 500 pixels.<br>";
$ok=0;
}


I probably entered it incorrectly.

thanx leo

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 August 2006 - 10:46 PM

if ($ImportWidth != 500)
Legend has it that reading the manual never killed anyone.
My site

#8 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 23 August 2006 - 10:52 PM

Hi, thanks. When I copied and pasted the code to PHP Freaks I changed the variables to suit those you had used but forgot to change one of them. It doesn't work even with the proper variables.

Any ideas?

#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 10:56 PM

If you want it to be smaller than 500 px, wouldn't it be:

if ($ImportWidth >= 500)

and the list line should be:

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;

not $_FILES['uploaded']['name'].

#10 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 24 August 2006 - 12:11 AM

Thank you very much : )

It works great!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users