• We just launched and are currently in beta. Join us as we build and grow the community.

Facebook Like Drag Background Cover Changer using Jquery

A61a

SEO Keyword Architect
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
115
Likes
79
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will show you how to create a Facebook Like Drag Background Cover Changer using Jquery by uploading, positioning and adjusting the image. And if you are looking for a application that can upload images this one is very interesting and suits for your system, and it has a combination of many features. See the example code below.

Sample Code

Index.php - This file is for displaying the background image based on the login user session id.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Facebook Like Drag Background Cover Changer using Jquery</title>
  5. <link href='timeline.css' rel='stylesheet' type='text/css'/>
  6. <script src="js/jquery.min.js"></script>
  7. <script src="js/jquery-ui.min.js"></script>
  8. <script src="js/jquery.wallform.js"></script>
  9. <script>
  10. $(document).ready(function()
  11. {
  12. $('body').on('change','#bgphotoimg', function()
  13. {
  14. $("#bgimageform").ajaxForm({target: '#timelineBackground',
  15. beforeSubmit:function(){},
  16. success:function(){
  17. $("#timelineShade").hide();
  18. $("#bgimageform").hide();
  19. },
  20. error:function(){

  21. } }).submit();
  22. });
  23. $("body").on('mouseover','.headerimage',function ()
  24. {
  25. var y1 = $('#timelineBackground').height();
  26. var y2 = $('.headerimage').height();
  27. $(this).draggable({
  28. scroll: false,
  29. axis: "y",
  30. drag: function(event, ui) {
  31. if(ui.position.top >= 0)
  32. {
  33. ui.position.top = 0;
  34. }
  35. else if(ui.position.top <= y1 - y2)
  36. {
  37. ui.position.top = y1 - y2;
  38. }
  39. },
  40. stop: function(event, ui)
  41. {
  42. }
  43. });
  44. });
  45. $("body").on('click','.bgSave',function ()
  46. {
  47. var id = $(this).attr("id");
  48. var p = $("#timelineBGload").attr("style");
  49. var Y =p.split("top:");
  50. var Z=Y[1].split(";");
  51. var dataString ='position='+Z[0];
  52. $.ajax({
  53. type: "POST",
  54. url: "image_saveBG_ajax.php",
  55. data: dataString,
  56. cache: false,
  57. beforeSend: function(){ },
  58. success: function(html)
  59. {
  60. if(html)
  61. {
  62. $(".bgImage").fadeOut('slow');
  63. $(".bgSave").fadeOut('slow');
  64. $("#timelineShade").fadeIn("slow");
  65. $("#timelineBGload").removeClass("headerimage");
  66. $("#timelineBGload").css({'margin-top':html});
  67. return false;
  68. }
  69. }
  70. });
  71. return false;
  72. });
  73. });
  74. </script>
  75. </head>
  76. <body>
  77. <ul>
  78. <li style="margin-left: 165px;"><a href="#home">Facebook</a></li>
  79. <li style="margin-left: 743px;"><a href="#profile"><img src="#">Sourcodester</a></li>
  80. <li><a href="#home">Home</a></li>
  81. <li><a href="#logout">Logout</a></li>
  82. </ul>
  83. <div id="container">
  84. <div id="timelineContainer">
  85. <div id="timelineBackground">
  86. <img src="<?php

    echo

    $path

    .

    $profile_background

    ;

    ?>

    " class="bgImage" style="margin-top: <?php

    echo

    $profile_background_position

    ;

    ?>

    ;">
  87. </div>
  88. <div style="background:url(images/timeline_shade.png);" id="timelineShade">
  89. <form id="bgimageform" method="post" enctype="multipart/form-data" action="image_upload_ajax.php">
  90. <div class="updateInfo timelineInfo">
  91. <input type="button" class="updateInfo" value="Update Info" alt="Update Info">
  92. </div>
  93. <div class="uploadFile timelineUploadBG">
  94. <input type="file" name="photoimg" id="bgphotoimg" class=" custom-file-input" original-title="Change Cover Picture">
  95. </div>
  96. </form>
  97. </div>
  98. <div id="timelineProfilePic"><img src="images/profilepic1.jpg" style="width: 170px;height: 170px;"></div>
  99. <div id="timelineTitle"><?php

    echo

    $name

    ;

    ?>

    </div>
  100. <div id="timelineNav">
  101. <div class="buttonside">
  102. <input type="button" class="button" value="Timeline">
  103. <input type="button" class="button button2" value="About">
  104. <input type="button" class="button button3" value="Friends">
  105. <input type="button" class="button button4" value="Photos">
  106. <input type="button" class="button button5" value="More">
  107. </div>
  108. </div>
  109. </div>
  110. </div>
  111. </body>
  112. </html>

UserUpdates.php - This simple PHP class contains three functions called userDetails, userBackgroundUpdate and userBackgourndPositionUpdate.

  1. <?php
  2. class

    userUpdates
  3. {
  4. private

    $db

    ;

  5. public

    function

    __construct(

    $db

    )
  6. {
  7. $this

    ->

    db

    =

    $db

    ;
  8. }
  9. public

    function

    userDetails(

    $user_id

    )
  10. {
  11. $query

    =

    mysqli_query

    (

    $this

    ->

    db

    ,

    "SELECT username,email,name,profile_background,profile_background_position FROM users WHERE user_id='$user_id

    ' "

    )

    or die

    (

    mysqli_error

    (

    $this

    ->

    db

    )

    )

    ;
  12. $data

    =

    mysqli_fetch_array

    (

    $query

    ,

    MYSQLI_ASSOC)

    ;
  13. return

    $data

    ;
  14. }
  15. public

    function

    userBackgroundUpdate(

    $user_id

    ,

    $actual_image_name

    )
  16. {
  17. $query

    =

    mysqli_query

    (

    $this

    ->

    db

    ,

    "UPDATE users SET profile_background='$actual_image_name

    ' WHERE user_id='$user_id

    '"

    )

    or die

    (

    mysqli_error

    (

    $this

    ->

    db

    )

    )

    ;
  18. return

    $query

    ;
  19. }
  20. public

    function

    userBackgroundPositionUpdate(

    $user_id

    ,

    $position

    )
  21. {
  22. $position

    =

    mysqli_real_escape_string

    (

    $this

    ->

    db

    ,

    $position

    )

    ;
  23. $query

    =

    mysqli_query

    (

    $this

    ->

    db

    ,

    "UPDATE users SET profile_background_position='$position

    ' WHERE user_id='$user_id

    '"

    )

    or die

    (

    mysqli_error

    (

    $this

    ->

    db

    )

    )

    ;
  24. return

    $query

    ;
  25. }
  26. }
  27. ?>

ImageUploadAjax.php - This is for the uploading image file into uploads folder and inserting background image name into users table. And every image that the user uploads has a restricted size of 1mb.

  1. <?php
  2. include

    'db.php'

    ;
  3. session_start

    (

    )

    ;
  4. $session_uid

    =

    '1'

    ;

  5. include

    'userUpdates.php'

    ;
  6. $userUpdates

    =

    new

    userUpdates(

    $db

    )

    ;

  7. include_once

    'getExtension.php'

    ;
  8. $valid_formats

    =

    array

    (

    "jpg"

    ,

    "png"

    ,

    "gif"

    ,

    "bmp"

    ,

    "jpeg"

    ,

    "PNG"

    ,

    "JPG"

    ,

    "JPEG"

    ,

    "GIF"

    ,

    "BMP"

    )

    ;
  9. if

    (

    isset

    (

    $_POST

    )

    &&

    $_SERVER

    [

    'REQUEST_METHOD'

    ]

    ==

    "POST"

    &&

    isset

    (

    $session_uid

    )

    )
  10. {
  11. $name

    =

    $_FILES

    [

    'photoimg'

    ]

    [

    'name'

    ]

    ;
  12. $size

    =

    $_FILES

    [

    'photoimg'

    ]

    [

    'size'

    ]

    ;
  13. if

    (

    strlen

    (

    $name

    )

    )
  14. {
  15. $ext

    =

    getExtension(

    $name

    )

    ;
  16. if

    (

    in_array

    (

    $ext

    ,

    $valid_formats

    )

    )
  17. {
  18. if

    (

    $size

    <

    (

    1024

    *

    1024

    )

    )
  19. {
  20. $actual_image_name

    =

    time

    (

    )

    .

    $session_uid

    .

    "."

    .

    $ext

    ;
  21. $tmp

    =

    $_FILES

    [

    'photoimg'

    ]

    [

    'tmp_name'

    ]

    ;
  22. $bgSave

    =

    '<div id="uX'

    .

    $session_uid

    .

    '" class="bgSave wallbutton blackButton">Save Cover</div>'

    ;
  23. if

    (

    move_uploaded_file

    (

    $tmp

    ,

    $path

    .

    $actual_image_name

    )

    )
  24. {
  25. $data

    =

    $userUpdates

    ->

    userBackgroundUpdate

    (

    $session_uid

    ,

    $actual_image_name

    )

    ;
  26. if

    (

    $data

    )
  27. echo

    $bgSave

    .

    '<img src="'

    .

    $path

    .

    $actual_image_name

    .

    '" id="timelineBGload" class="headerimage ui-corner-all" style="top:0px"/>'

    ;
  28. }
  29. else
  30. {
  31. echo

    "Fail upload folder with read access."

    ;
  32. }
  33. }
  34. else
  35. echo

    "Image file size max 1 MB"

    ;
  36. }
  37. else
  38. echo

    "Invalid file format."

    ;
  39. }
  40. else
  41. echo

    "Please select image..!"

    ;
  42. exit

    ;
  43. }
  44. ?>

fb1.png

Hope that you learn from this tutorial and don't forget to Like & Share. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top