How can i create a PHP gallery

Hello friends,

I have a website that has users, the users can upload images, but i now have to add the option for adding and displaying videos.

the code i use for adding images

} elseif (isset($_POST[‘upload_photos’])) {

                for ($i=0; $i < count($_FILES['photos']['name']); $i++) {

                    $file_name = $_FILES['photos']['name'][$i];

                    $file_error = $_FILES['photos']['error'][$i];

                    $file_size = $_FILES['photos']['size'][$i];

                    $file_tmp_name = $_FILES['photos']['tmp_name'][$i];

        

                    $file_ext = explode('.', $file_name);

                    $actual_file_ext = strtolower(end($file_ext));

        

                    $allowed = array('jpg', 'jpeg', 'png', 'gif', 'mp4' , 'MOV');

        

                    if (in_array($actual_file_ext, $allowed)) {

                        if ($file_error === 0) {

                            if ($file_size < 5000000) {

                                $file_name_new = bin2hex(random_bytes(12)).'.'.$actual_file_ext;

                                $file_destination = 'photos/'.$file_name_new;

        

                                if (file_exists($file_destination)) {

                                    echo "This file is already in our database!";

                                } else{

                                    move_uploaded_file($file_tmp_name, $file_destination);

                                            $src = $file_destination;

                                            $dest = 'thumbnail' . $file_name_new;

                                            $desired_width = '400';

                                            /* read the source image */

                                            $stype = explode(".", $src);

                                            $stype = $stype[count($stype)-1];

                                            switch($stype) {

                                                case 'gif':

                                                $source_image = imagecreatefromgif($src);

                                                break;

                                                case 'jpg':

                                                $source_image = imagecreatefromjpeg($src);

                                                break;

                                                case 'jpeg':

                                                $source_image = imagecreatefromjpeg($src);

                                                break;

                                                case 'png':

                                                $source_image = imagecreatefrompng($src);

                                                break;

                                            }

                                            // $source_image = imagecreatefromjpeg($src);

                                            $width = imagesx($source_image);

                                            $height = imagesy($source_image);

                                            /* find the "desired height" of this thumbnail, relative to the desired width  */

                                            $desired_height = floor($height * ($desired_width / $width));

                                            /* create a new, "virtual" image */

                                            $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

                                            /* copy source image at a resized size */

                                            imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

                                            /* create the physical thumbnail image to its destination */

                                            imagejpeg($virtual_image, 'photos/'.$dest);

                                    if ($user->add_photos($logged_user['user_id'], $file_name_new, $dest)) {

                                        if ($logged_user['user_profile_photo'] == 'user_default_photo.jpg') {

                                            $user->set_as_profile($file_name_new, $logged_user['user_username']);

                                        }

                                        ?>

                                            <script type="text/javascript">window.location = "<?php echo $_SERVER['PHP_SELF']; ?>";</script>

                                        <?php

                                    }

                                }

                            } else {

                                echo "File size is too big for uploading! (Max size 5MB)";

                            }

                        } else {

                            echo "An error has occurred!";

                        }

                    } else {

                        echo "Format isn't supported!";

                    }

                }

and this is the code i use in my profile.php all the images are stored in a folder called photos on my hosting

            <h1><?php echo $selected_user['user_showname']; ?></h1>

            <div id="gallery_container">

                <div class="slideshow_container">

                    <?php 

                       for ($i=0; $i < count($selected_user_photos); $i++) { 

                          if ($selected_user['user_profile_photo'] == $selected_user_photos[$i]['photo_name']) { 

                          

                          ?>

                                <div class="slides fade">

                                  <a data-lightbox="images" href="photos/<?php echo $selected_user_photos[$i]['photo_name'] ?>">

                                        <img class="gallery_image" src="photos/<?php echo $selected_user_photos[$i]['photo_name'] ?>">

                                    </a>

                                </div>

                                                                   

                    <?php 

                            } else{

                            }

                        } ?>

                    <?php 

                        for ($i=0; $i < count($selected_user_photos); $i++) { 

                            if ($selected_user_photos[$i]['photo_delete'] == 0 && $selected_user['user_profile_photo'] != $selected_user_photos[$i]['photo_name']) { ?>

                                <div class="slides fade">

                                    <a data-lightbox="images" href="photos/<?php echo $selected_user_photos[$i]['photo_name'] ?>">

                                        <img src="photos/<?php echo $selected_user_photos[$i]['photo_name'] ?>">

                                    </a>

                                </div>

                    <?php 

                            } 

                        } ?>

                    <!-- Next and previous buttons -->

                    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>

                    <a class="next" onclick="plusSlides(1)">&#10095;</a>

                </div>

                

                <!-- The dots/circles -->

                <div class="gallery_dots">

                <?php for ($i=1; $i < count($selected_user_photos) + 1; $i++) { ?>

                    <span class="dot" onclick="currentSlide(<?php echo $i; ?>)"></span>

                <?php } ?>

                </div>

            </div>

It’s the same process for uploading, but you will hit size limitations faster. You can chunk larger files, https://code-boxx.com/upload-large-files-php/

Plus, I would resize your images to your server to make them more manageable and have less resolution as you really only need 72 DPI for the web unless you are making a portfolio and it doesn’t sound like you are.

Sponsor our Newsletter | Privacy Policy | Terms of Service