I have a photo upload script. In general everything is similar to general concepts. Also it is similar to Facebook's profile image logic. Differently I set user's profile picture by latest uploaded image. More clearly;
Steps:
user uploads profile picture
he/she can see the picture in some criteria comes from Mysql : ORDER BY add_date DESC so newest picture can fetched by php
Also I have a delete image button. If I use Mysql's DELETE keyword because of ORDER BY DESC newest picture is changing.
How can I set default picture after I click delete image button
as you can see from picture user 5 has added 3 different images in three different time.
mysql query is : SELECT path FROM profile_image WHERE user_id = ".$_SESSION['SES_USER_ID']." ORDER BY add_date DESC;" showing picture to user and for fetching. earliest date is user's active profile image. (June 28 )
if I delete June 28, June 27's photo will be active profile image, not the default profile image.
How can I set default image?
Have another table with fields userid, isset - ( with default value of 1 for all users)
If user clicks the remove profile pic( wishes to have the default pic) , change the value in isset to 0 for the respective user.
Check this table everytime before fetching the latest image from the profile pics table.
Alternatively you can have the isset field in the profile pics table itself. This would require you to change the isset value of every occurrence of the user's profile pic if he wishes to have the default pic.
Related
I have a form for users to sign up. This form has a file input which will upload images in a directory in server by ajax instantly as user selects a file. There's also a button which will submit the form and saves user info in user table in database.
Each user can have multiple images. Ajax also save address of images in photos table when file uploads. this table has user_id column. but since the user_id will be generated when submit button is hit and user is not registered yet, I can't fill user_id column in photos table. The id of user is generated when whole form is submitted.
so should I update photos table when form submits by adding id of user to user_id column?
or I must save photos with ajax in temporary_photos table and then when submit is hit save them in main photos table and delete them from temporary table?
or is there another approach?
what if user uploads files, but closes the window without hitting submit button? how I find out the window is close and registration is canceled so i will delete images?
You are trying to solve a problem that you created. If user_id is a dependency for the photo to be saved, let the user be saved first.If you do it the other way , it you will over complicate the workflow.
So, i suggest you register the user first, save the user id in session (or log the user in) and then upload the photo.
If you must do it together, then let the ajax request register the user and then save the photo and save the user_id in a session variable. Next time you upload a photo you could get the user from session variable and save the photo.
function savePhoto(Request $input){
if(Session::has('user_id'){
$user = User::find(Session::get('user_id'));
}
else{
$user = User::create($input->all());
}
$user->photo->save($input->all());
}
You can try this way-
Since an user can have multiple images that means the relation is one to many between. So better if you use a images table.
Table: Images
Column(s): id, user_id, image, uploaded_at
Make user_id field is nullable and image column will save the name of uploaded images. Upon successful uploading and adding iamges record to database save last inserted ids in a session array. Once the user submit the form, create a record to user table, grab the last insert it, check if there are any data in session array, if found, loop through the array and select those images record by id and update user_id field. This will solve your problem.
You also asked for a way to delete images for which users didn't submit the form. To do this, you can run a cronjob/laravel task schedular. A script that will remove images and delete records for those images which were uploaded 5 mins (for example) ago and user_id is still null.
I don't really understand properly the concept behind uploading multiple files with preview. I checked DropzoneJS and it looks nice and I feel I can do a lot of stuff with it.
I have this example: I have a user (id: 801) that creates a new post (id: TBD since the post is not yet created). I have a table image_post which holds the images of the specific post, and an image table which holds the image details. Now, I have the Dropzone form which is supposed to upload images to a folder that I specify in the .php file that is implemented to deal with the ajax request coming from Dropzone. Let's say the user uploads 2 images and I store them into an temp folder. The user submits the creating of the new post, it receives an ID: 10001. The temporary files are on the disk (but should they be stored in the DB as well?) but don't see how to link the post to the images.
What are the exact steps that are required to be able to (after the files were uploaded with Dropzone ajax request) link those images to the actual post id?
Your system will need to create a database record for the post prior to them uploading images. Consider the following table structure, where an i prefix indicates an INT column, s a VARCHAR/TEXT column, and dt a DATETIME.
Table posts: iPostId | iUserId | iStatus | sComments | dtCreated
Table photos: iPhotoId | iPostId | dtUploaded
When a user begins to start a new post, create a record in the posts table with their user id, an empty comments, a datetime such as '0000-00-00 00:00:00', and a status of 0, and then add a hidden form input to the photo upload form so that the assigned post id is known to your photo upload script, e.g.:
<input type="hidden" name="iPostId" value="22" />
The photo upload script could create a new record in the photos table using the post id and then save the photo in a particular folder using the post id in the filename, for example if the photo_id for the record created was 2821 you would save the file as 2821.jpg in your chosen folder.
When the user finishes their post and clicks to save/submit, you could use an UPDATE query to modify the original record on the posts table, assigning their entered text to the sComments column, the current date and time to the dtCreated column, and changing the iStatus to 1.
On your pages where you display a list of posts simply modify your query to something like this to exclude those currently in a draft status (0): SELECT * FROM posts WHERE status=1;, and then when viewing the post you can run a query such as SELECT * FROM photos WHERE post_id=22; to get an array of the photos to be displayed.
Obviously this can be expanded upon but hopefully gives you a good starting point.
So I'm trying to create a system where the user can go to a page where a photo is displayed. The photos have a column in the MySQL db for views. How would I be able to view the top viewed photo with a link on the page for the photo with the 2nd most viewed photo, which has a link for the 3rd most viewed photo, etc. Each photo would have a dedicated page.
I'm using CakePHP, if there are any CakePHP specific strategies for doing this. Any suggestions?
I have never had the experience of using cakePHP, but with PHP and MySQL, you can use a query like this:
SELECT image_link, view_count
FROM `tablename`
ORDER BY view_count DESC
LIMIT 1
OFFSET $i;
and then, using a GET variable to pass on the incremental/decremental value of $i.
You can collect statistics for each image using
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$url = $_SERVER['HTTP_REFERER'];
?>
where $url - is a full path for your image.
well i don't have knowledge of cakephp, but i am telling you the logic to achieve that you want,
just send some parameter on the photo click and set a unique id of the photos, so that on the click event on the photo you will get the unique id of the photo and get which photo is viewed and update the database on every click.
beside that the link you will genrate according to your requiement to fetch the image on view filed on descending order
I needed to know how one would go about setting up a default avatar image for a user upon registration and allow the user to upload one of their own later. Like in facebook.
My user table fields(shortened):
user_id, first_name, last_name, username, tel_no etc.
My pictures table:
Pic_id, pic_large_url, pic_thumb_url, user_id, avatar, timestamp
I need to know what kind of php code I need to be able to accomplish this. Do I put my default_avatar.png image in a folder and reference it in the pictures table and where do I go from there?
I wanted to use an avatar (bit) field 1 if it is a user's avatar, 0 if it is not.
I also have an updates table that allows users to upload text and images in their updates or just text only as follows:
avatar pic - update text - update image(if image)
avatar pic - update text - default image(if update image not uploaded)
default avatar pic - update text - default image(if update image not uploaded)
I guessing if i find out how to put a default avatar, ill find out how to do the above.
I will appreciate any help out there.
In your avatar database field, either setup the default to point towards your default avatar, or you can check if its null.
If it is null, render the default avatar image :)
If you're using the same default image for everyone then I wouldn't bother storing it in your database. Just check if the user has a custom avatar, if so return that url, otherwise return the default path.
function displayAvatar($userId)
{
$query = getUserInfo($userId) // this is a function that would query the db to get a user's info, or to get info from your pictures table, lets assume it returns an array
if($query['avatar']===1) {
return $query['pic_thumb_url'];
}
return '/path/to/default/avatar.jpg';
}
Then in your html you could do something like this:
<div class="avatar"><img src="<?php echo displayAvatar($user); ?>"/></div>
I'm assuming here that you have some sort of user object with access to the pictures table.
I'm building an app where each user can have multiple profiles. Users can upload multiple photos for each profile, etc.
The folder structure will be something like
public_html/
upload/
user-123/
profile-199/
profile-321/
images/
123423.png
I'm going to have a one to many relationship between each profile and each photo, but I'm trying to see how I should flag a photo as special and the users "profile photo".
My proposed table structure is something like this
id
profile_id
filename (or maybe extension only, since filename will most likely just be the primary id for that photo)
profile (boolean field of whether this a profile pic or not)
date_added
How should I treat special images, like profile pictures?
Edit: Sorry if I wasnt clear. I want each user to be able to have multiple profiles, each profile to have many photos. Just like for Facebook, you can have many photos, but only one profile pic at any time.
That would work but it would have a problem that more than one photo could be marked as a profile photo unless you take special care to prevent this.
An alternative design is to have a table with columns user_id and special_photo_id where the photo_id is a foreign key into the photos table. This has a slightly different issue: now it is possible to have a special photo that doesn't belong to the user.
You could have a field in your users table(where its username and other info is stored) with a profile_id field init pointing to the profiles table. That way you get one possibility for the profile and away to identify it.
Thinking about it that will only work if you do have a users table and a separated profiles table.
How about the image table looks like this:
id
profile_id
filename
date_added
and add the column 'profile_image_id' in the profile table? This will be your special image's id.
I imagine you are going to have a lot more non-special images than special. So, most of the time the field 'profile(boolean)' that you proposed will be 0.
Edit: I believe #Iznogood is saying the same thing.