Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP/ MySQL Picture Gallery

Posted on 2011-02-17
6
Medium Priority
?
976 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:Cornelia Yoder
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:Cornelia Yoder
Cornelia Yoder earned 400 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1600 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 111

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

926 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