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!
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
I have a form for users to upload image files and they get inserted into mysql database. I currently have restrictions on file types to only allow .jpg, I also have restrictions on image dimensions and file size. I would also like to restrict files from being uploaded that are below 300dpi. How can I achieve this? Current "upload.php" that gets action from the form works perfect, I just want to add a check for dpi of file into it. I realize of course I'll have to add a new field to my mysql Table, create the variable, and gather its data value from the image file. Code below:
<?php
# Check if a file has been uploaded
if(isset($_FILES['uploaded_file']))
{
# Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0)
{
# Connect to the database
$dbLink = mysqli_connect("hostname", "username", "password", "dbname");
if(mysqli_connect_errno())
{
die("MySQL connection failed: ". mysqli_connect_error());
}
# Define Variables and Gather all required data
$limit_size = 2097152;
$max_width = "600";
$max_height = "400";
$legal_type = "image/jpeg";
$legal_typeIE = "image/pjpeg";
$esize = "File size exceeds 2MB. Adjust and try to upload file again please.";
$etype = "File type must be .JPG format. Adjust and try to upload file again please.";
$edim = "File dimensions must equal 600x400 pixels. Adjust and try to upload file again please.";
$edpi = "File resolution must equal 300dpi. Adjust and try to upload file again please.";
$esucc = "File uploaded successfully. Thanks for contributing to the project.";
$eukn = "An unknow error occurred while uploading the file. Please try again.";
$name = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
$mime = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
$size = $_FILES['uploaded_file']['size'];
$data = mysqli_real_escape_string($dbLink, file_get_contents($_FILES ['uploaded_file']['tmp_name']));
# Check File Type
if(($mime != $legal_type) && ($mime != $legal_typeIE))
{
$passit = $etype;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
# Check File Dimensions
list($width, $height) = getimagesize($_FILES['uploaded_file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
$passit = $edim;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
# Check File Size
if ($size >= $limit_size)
{
$passit = $esize;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
exit();
}
else
{
# Create the SQL query
$query = "
INSERT INTO FileStorage (
FileName, FileMime, FileSize, FileData, Created
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
# Execute the query
$result = mysqli_query($dbLink, $query);
}
# Check if it was successfull
if($result)
{
$passit = $esucc;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
# Close the mysql connection
mysqli_close($dbLink);
}
else
{
$passit = $eukn;
header("Location: index.php?option=com_jumi&fileid=3&Itemid=3&passit=$passit");
}
exit();
?>
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
If you restrict the dimensions to 600x400 then isn't that sufficient? As I understand it dpi is something of an abstract measurement: if you print out a 600x400 image the size of a postage stamp then the dpi will be higher than if you print the same image across a sheet of A4. The file size remains the same.
greyhoundcode wrote:If you restrict the dimensions to 600x400 then isn't that sufficient? As I understand it dpi is something of an abstract measurement: if you print out a 600x400 image the size of a postage stamp then the dpi will be higher than if you print the same image across a sheet of A4. The file size remains the same.
If you create a 300dpi file with the same dimensions as a 72dpi file, the 300dpi will have a far larger file size than the 72dpi.
If you print an image at different sizes, the dpi doesn't change (unless you go in and change it before each print).
I'll bow to your greater knowledge, have to admit I'm no expert when it comes to printed material
That said, if you use a package like Inkscape, and you increase the export resolution from 10dpi (for example) to 100dpi, then the dimensions increase 10-fold also, which makes sense to my mind.
As dpi is dots per inch, how else can you quantify it other than by image dimensions and the size of the image when printed? I'm genuinely interested to know.
The reason I was wanting this dpi restriction is because I will be using the uploaded files for print. Where as 72dpi is great for web display, it is recommended that 300dpi be used for print. That being said, I think I've found a better solution than restricting the dpi on the file. I am going to just change my dimension requirments. So instead of 600x400px I am going to raise this so that one of the dimension must be >= to 2200px. This way the file can stay at 72dpi and still be printable for the size I need it. I will then just create smaller image sizes and thumbnails in script of this original uploaded file for display on the site.
greyhoundcode wrote:I'll bow to your greater knowledge, have to admit I'm no expert when it comes to printed material
That said, if you use a package like Inkscape, and you increase the export resolution from 10dpi (for example) to 100dpi, then the dimensions increase 10-fold also, which makes sense to my mind.
As dpi is dots per inch, how else can you quantify it other than by image dimensions and the size of the image when printed? I'm genuinely interested to know.
We're going a bit off topic but...
Dots Per Inch. An inch is a fixed unit of measure. It doesn't increase no matter how many dots you put into it.
Dots Per Inch. An inch is a fixed unit of measure. It doesn't increase no matter how many dots you put into it.
Exactly, an inch doesn't vary, but the number of dots do.
If you create a 300dpi file with the same dimensions as a 72dpi file, the 300dpi will have a far larger file size than the 72dpi.
I really don't see how that works. If a file is 1000x1000 pixels then the only real way the dpi is going to change is if that image is printed out at a different size.
Take a look at this which breaks it down to brass tacks.
greyhoundcode wrote:We are going a bit off-topic I know however ...
Dots Per Inch. An inch is a fixed unit of measure. It doesn't increase no matter how many dots you put into it.
Exactly, an inch doesn't vary, but the number of dots do.
If you create a 300dpi file with the same dimensions as a 72dpi file, the 300dpi will have a far larger file size than the 72dpi.
I really don't see how that works. If a file is 1000x1000 pixels then the only real way the dpi is going to change is if that image is printed out at a different size.
Take a look at this which breaks it down to brass tacks.
I could be wrong but I dont think the dpi changes if the image is printed at different sizes. Its like zooming in to the image on the computer. It gets larger but the dpi of that image doesn't change. You can manually change the dpi (at least in photoshop). If you scan something into your computer, you can set the dpi of that image. It will still print at the same original size but it will be a more detailed scan with a higher dpi. Same width and height, just more pixels crammed into the area.
Definitely straying out of the PHP domain now! But you see to my way of thinking it's when you print an image out at different sizes that you are going to see a change in the dpi.
If an image is only 1000 dots across, and you fit it into a square inch, then that is 1000dpi, but print it over a bigger area like 5"x5", then it is going to be 200dpi (assuming my arithmetic is correct).
I suppose what I'm saying is that 'dpi' is notional, and relies on a set of assumptions about the image size when it is output to paper (or a certain size of screen).
In any case I think we've both done a good job of boring each other, and I'm pretty sure shanerich is no longer interested!