954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating a User Avatar/Icon Option

I am in the process of creating a website in PHP/mySQL and the next thing I wanted to tackle was creating an option for users to be able to upload a small avatar (maybe max of 50x50 let’s say). I’m not as familiar with how images are stored/retrieved in a database – any guidance on getting this up and running?

Thanks!

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

You have two main options. You can save the uploaded image as data in a MySQL blob field or you can save the actual file in a folder and reference it in a MySQL varchar field (filepath).

You can access the file dimensions, file size and file type in order to access/refuse the image. You have a choice as to the dimensions (e.g. 50 x 50), file size (e.g. 100Kb), file type (usually GIF, JPG, JPEG, PNG).

If the dimensions are greater than the allowed, you can simply refuse them or scale them yourself to the maximum allowed size.

Example:

list($w, $h) = getimagesize("$_FILES['upload']['tmp_name']");


Will give you the file dimensions ($w and $h).

$maximagesize = 307200 //(300kb or is that kib - can never remember)
if ($_FILES['upload']['size'] > $maximagesize) { 
...
}


Will check your filesize

$xtn = strtolower(strrchr($_FILES['upload']['name'], ".")); //the reverse is impt because names could involve more than one period
if ($xtn!= ".gif" AND $xtn!= ".jpg" AND $xtn!= ".jpeg" AND $xtn!= ".png") { 
...
}


Will check your extensions. Don't rely on Ajax/JS-based stuff as these can be bypassed.

Here's a scaling script from the php manual: http://www.php.net/manual/en/function.getimagesize.php#97564


Anyway, have fun.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Many people tend to store pictures as external files and keep only a reference in the database.
I'd rather recommend to storing them in the database in blob fields as you won't have to deal with file or directory names, updating image files and the like.
The process for that is:
Upload the file and read it from the local temporary into the database.
Create a PHP script which outputs the image field content to the browser. Include a matching image header into the script (e.g. header("Content-type: image/jpeg").
When generating the HTML with images in it, create references to your image display script instead of references to image file names.

// ardav beat me by 2 minutes ;-(

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

Thank you both for the help, it seems that both of you suggest storing it in the DB as opposed to referencing it? If so, how would I go about creating the SQL to execute to actually store the attached image into the clob field? :?:

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 

First of all it's a blob field. I always use a ref and real files myself. I find the storage and retrieval a bit of a phaph. Anyway, each to his own. Each has its pros and cons.
AS smant said - use a ref to the script, if you go down the blob route, e.g.

<img src="createimg.php?id=634732" />
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: