Jump to content

Checking uploaded image's pixel dimensions


Leao

Recommended Posts

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

[code]<?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.";
}
}
?>[/code]
Link to comment
Share on other sites

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.

[code]// 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;
}
}[/code]

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

[code]
//copy the file to permanent location
copy($import, "path/to/images/$VarName.jpg");
[/code]
Link to comment
Share on other sites

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?

[code=php: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";
}
[/code]

Cheers,

Leao
Link to comment
Share on other sites

[quote]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?[/quote]

Yup. The first line of code posted earlier:

[code]list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($filename);[/code]
Link to comment
Share on other sites

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.com/toh/technical/imageresize/index.php

Lite...
Link to comment
Share on other sites

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: [quote]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[/quote]

The php I tried was:
[code=php:0]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;
}[/code]


I probably entered it incorrectly.

thanx leo
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.