Solved

PHP/ MySQL Picture Gallery

Posted on 2011-02-17
6
935 Views
Last Modified: 2012-05-11
I am looking to build a facebook like photo gallery for my site.But I'm looking for some advise to make a good start. My site is PHP based with a MySQL back end any good advise or code to get me started would be great.

Thank you
0
Comment
Question by:patrickm12981
  • 2
  • 2
  • 2
6 Comments
 
LVL 27

Expert Comment

by:yodercm
ID: 34923214
You can see my picture site here:   www.corneliayoder.com

If you like it, I used Image Thumbnail Viewer II script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
0
 

Author Comment

by:patrickm12981
ID: 34923283
Thats pretty nice, I am running a car racing site. The users arent exactly computer savy but most know how to upload videos and/or pictures to facebook so I am looking for something as close as possible. I will ne crating the albums, if I allow the users to create the albums it will become complete chaos. But they do nee to be able to upload photos or videos to the albums.
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 100 total points
ID: 34923322
That code is for displaying images.  The upload is done through simple php functions.  

Here is a tutorial for using php to upload images:  http://www.reconn.us/file_uploading.html
and you can find many more by a quick search.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 34926480
Online photo galleries have kind of "been done" already.  If I were starting this today, I would use the existing social media like Flickr.

The snippet has code to upload and resize a photo.  You will need some kind of client log-in to avoid album collisions.  The article here shows the principles of that part of things.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

HTH, ~Ray
<?php // RAY_upload_resize_example.php
error_reporting(E_ALL);


// UPLOAD AN IMAGE AND RESIZE IT TO FIT SOME PREDEFINED SIZES
// http://php.net/manual/en/ref.image.php
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php
// http://php.net/manual/en/function.getimagesize.php
// http://php.net/manual/en/function.imagejpeg.php


// A FUNCTION TO DETERMINE IF GD IS AT LEVEL 2 OR MORE
function get_gd_info($display=FALSE)
{

    // IS GD INSTALLED AT ALL?
    if (!function_exists("gd_info"))
    {
        if ($display) echo "<br/>GD NOT INSTALLED\n";
        return FALSE;
    }

    // IF GD IS INSTALLED GET DETAILS
    $gd = gd_info();

    // IF DISPLAY IS REQUESTED, PRINT DETAILS
    if ($display)
    {
        echo "<br/>GD DETAILS:\n";
        foreach ($gd as $key => $value)
        {
            if ($value === TRUE)  $value = 'YES';
            if ($value === FALSE) $value = 'NO';
            echo "<br/>$key = $value \n";
        }
    }

    // RETURN THE VERSION NUMBER
    $gd_version = preg_replace('/[^0-9\.]/', '', $gd["GD Version"]);
    return $gd_version;
}


// A FUNCTION TO MAKE AN IMAGE INTO THE RIGHT WIDTH FOR PAGE DISPLAY
// WILL WORK IF GD2 NOT INSTALLED, BUT WILL MAKE BETTER IMAGES WITH GD2
// INPUT IS THE IMAGE FILE NAME, OUTPUT IS AN IMAGE RESOURCE, OR FALSE IF NO RESIZE NEEDED
function create_right_size_image($image, $width=720)
{
    // IS GD HERE?
    $gdv = get_gd_info();
    if (!$gdv) return FALSE;

    // GET AN IMAGE THING
    $source = imagecreatefromjpeg("$image");

    // GET THE X AND Y DIMENSIONS
    $imageX = imagesx($source);
    $imageY = imagesy($source);

    // IF NO RESIZING IS NEEDED
    if ($imageX <= $width)
    {
        return FALSE;
    }

    // THE WIDTH IS TOO GREAT - MUST RESIZE
    $tnailX = $width;
    $tnailY = (int) (($tnailX * $imageY) / $imageX );

    // WHICH FUNCTIONS CAN RESIZE / RESAMPLE THE IMAGE?
    if ($gdv >= 2)
    {
        // IF GD IS AT LEVEL 2 OR ABOVE
        $target = imagecreatetruecolor($tnailX, $tnailY);
        imagecopyresampled ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
    }
    else
    {
        // IF GD IS AT A LOWER REVISION LEVEL
       $target = imagecreate($tnailX, $tnailY);
       imagecopyresized   ($target, $source, 0, 0, 0, 0, $tnailX, $tnailY, $imageX, $imageY);
    }
    return $target ;
}
/* ********************************************************************************************* */


// ESTABLISH THE NAMES OF THE DIRECTORIES
$s = 'RAY/storage/';
$p = 'RAY/pics/';
$t = 'RAY/thumbs/';

// ESTABLISH THE LARGEST FILE WE WILL UPLOAD
$max_file_size = '5000000'; // A BIT MORE THAN 4MB

// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED in $_FILES[]["error"]
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;

// IF THERE IS INFORMATION POSTED
if (!empty($_POST))
{
    // IF THERE ARE ERRORS
    $error_code    = $_FILES["userfile"]["error"];
    if ($error_code)
    {
        die($errors[$error_code]);
    }

    // SYNTHESIZE THE NEW FILE NAME FOR TEMPORARY STORAGE
    $f = basename($_FILES['userfile']['name']);
    $my_new  = getcwd() . '/' . $s . $f;
    $my_temp =                  $s . $f;

    // MOVE THE FILE INTO THE STORAGE DIRECTORY
    if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $my_new))
    {
        die("MOVE TO $my_new FAILED");
    }

    // SAVE THE PIC
    if ($image_resource = create_right_size_image($my_temp, 450))
    {
        imagejpeg($image_resource, $p . $f);
        imagedestroy($image_resource);
    }

    // SAVE THE THUMB
    if ($image_resource = create_right_size_image($my_temp, 100))
    {
        imagejpeg($image_resource, $t . $f);
        imagedestroy($image_resource);
    }

    // DISPOSE OF THE UNWANTED ORIGINAL
    unlink($my_new);

    // SHOW THE LINKS TO THE NEW FILES
    echo '<br/><a href="' . $p . $f . '">PIC</a>';
    echo '<br/><a href="' . $t . $f . '">THUMB</a>';
}


// CREATE THE FORM FOR INPUT (USING HEREDOC SYNTAX)
$form = <<<ENDFORM
<form enctype="multipart/form-data" method="post">
<!-- MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD -->
<input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size" />
Find a Photo to Upload: <input name="userfile" type="file" />
<input type="submit" value="Upload" />
</form>
ENDFORM;

echo $form;

Open in new window

0
 

Author Comment

by:patrickm12981
ID: 34926512
Thanks both of you , I have found an instllation called phpwebgallery that seems to be a good start. With that and the above script I thnk I may be able to get what I need.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34926538
Thanks for the points.  And best of luck with your project, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now