SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Photo Gallery

  1. #1
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Photo Gallery

    I' am creating a photo gallery. And I was wondering how I would go about making a script that created s of the pictures. And if possible make the size of the pictures smaller too.

    I still am not sure whether to make the script so that its online and you upload the pictures up. Please help me!

    Thanks in advance!

    Santosh Sankar
    Bullish Bankers

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you take all the images that were uploaded and create a database entry that says in what album the image is and maybe some other information. Then you resize each image and name them after the id they got in the database. Then you place all the images, big and small in the appropriate folders and when people ask for some images from album you simply get all the id's that belong to that album and you know the path to the thumbnail/large image.

    GD library is an image library often used for this. Another more advanced one is called ImageMagick but I have never used it.

    There do exist endless tutorials that explain in details how this is done. Take a look at www.phpbuilder.com, www.devshed.com, www.hotscripts.com and then Google is always there (search for 'Image resizing with PHP and GD')

    If you run into any trouble on the way, simply post back here and you will be helped.
    - website

  3. #3
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I tried doign this last time, I thought to put it in a database. But people told me it's not efficient. So can you tell me how to?

    I make a data base...and a table...and....what should the cell types be?

    Santosh Sankar
    Bullish Bankers

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...

    Lets create a table in the database and call it Images.
    In that table we have the following columns:
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    Album_id INT NOT NULL

    then we have table Album and in it we have the following columns
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    name VARCHAR(255) NOT NULL

    Then you would upload the images, see http://www.phpfreaks.com/tutorials/36/0.php
    And then you would say in the form in what Album the image belongs etc. Then you would save the image in a directory accessable from the internet and you would also create a thumbnail which you would place there to.

    Some info:
    http://www.hotscripts.com/PHP/Tips_a...ion/index.html

    To answear your question placing them in the database is not efficient I have heard (never tried though).

    If you run into any problems don't hesitate to ask.
    - website

  5. #5
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    man thanks...this database stuff is new to me

    INT is integer right?

    Santosh Sankar
    Bullish Bankers

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yip. Good Luck...

  7. #7
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This good?


    PHP Code:

    CREATE TABLE images
    (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY album id INT NOT NULL );

    CREATE TABLE album(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY album id VARCHAR(255NOT NULL); 

    Santosh Sankar
    Bullish Bankers

  8. #8
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ???

    Santosh Sankar
    Bullish Bankers

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm... Need something that would hold a lot more info that that about an IMG no ? Not got the time just now but I'll get back to you on this later ?

  10. #10
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    website's Post #4 said todo this...

    Santosh Sankar
    Bullish Bankers

  11. #11
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Then you would upload the images, see http://www.phpfreaks.com/tutorials/36/0.php
    i used that code and nothing happens; i mean, no echo, no upload...nothing.
    Here's what i've:
    PHP Code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-type"
    content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
    <form name="form1" method="post" action="" enctype="multipart/form-data"> 
    <input type="file" name="imagefile"> 
    <input type="submit" name="Submit" value="Submit"> 
     

    <?php  
    if(isset( $Submit )) 


    if (
    $_FILES['imagefile']['type'] == "image/gif"){ 
            
     
    echo(
    'its a gif!');//I UPLOAD A .GIF AND NO ECHO...
        
    copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile'] ['name']) //HERE I USED THE FILE WHERE I WANT THE UPLOADED IMAGES TO STORE :copy ($_FILES['imagefile']['tmp_name'], "mYuPLOADfILE/".$_FILES['imagefile'] ['name']) 
        
    or die ("Could not copy"); 

            echo 
    ""
            echo 
    "Name: ".$_FILES['imagefile']['name'].""
            echo 
    "Size: ".$_FILES['imagefile']['size'].""
            echo 
    "Type: ".$_FILES['imagefile']['type'].""
            echo 
    "Copy Done...."
            } 

            else { 
                echo 
    ""
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")"
            } 

    ?>

     </form> 
    </body>
    </html>

  12. #12
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now i got it working, because i added this, at the beguining of the script:
    PHP Code:
    $Submit=$_POST['Submit']; 
    My question now is this: since many times lately i've to add code similar to this in articles i try to see working, i wonder if i've something wrong or bad configured in my php.ini.
    Could this be the case or the article code was incomplete?
    The last time i had the very same problem i posted it here: http://www.sitepointforums.com/showt...hreadid=114896

  13. #13
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dun think its ur config file...

    try:

    echo "phpinfo()";

    get the version and stuff...but incomplete code is possible...

    Santosh Sankar
    Bullish Bankers

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rfl - in your post you were looking for the variable $Submit being set correct ? This didn't work as you didn't use $_POST['Submit'] obviously. From what I can tell you were using a pre 4.2.x article/tutorial with globals on... Nowadays you have globals off (4.2.x + - see article by kevin yank) so you need to use $_POST, $_GET and $_REQUEST etc etc to read FORM/HTTP variables as you've now found out In fact this would be better:

    PHP Code:
    if(isset($_POST['Submit'])) {
    # FORM posted

    else {
    # FORM not posted
    }
    .


  15. #15
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good!
    thank you

  16. #16
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by netkid
    website's Post #4 said todo this...
    Ok, this is the neccisary information, you can add comments and endless extra stuff but I thought we would just make it simple now, you can then add extra stuff when you know this.

    rfl, ok, I'll just go through this, this tutorial might not have been the best.

    First lets create the database (I added field extension):
    Code:
    CREATE TABLE Albums (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL);
    
    CREATE TABLE Images (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Album_id INT NOT NULL,
    extension VARCHAR(10));
    Then the form for creating the album (page createAlbum.php):
    PHP Code:
    <?
    if (strcasecmp($_POST['submit'], 'submit') === 0) {
      
    //assuming you have already connected to the database...
      
    if (mysql_query('INSERT INTO Albums SET name = \'' addslashes($_POST['name']) . '\'')) {
          echo(
    'Album has been created successfully');
      } else {
          echo(
    'Error: ' mysql_error());
      }
    } else {
    ?>
    <form action="<? echo($_SERVER['PHP_SELF']); ?>" method="post">
      Album name:<br><input name="name" type="text" /><br>
      <input type="submit" name="submit" value="Submit" />
    </form>
    <?

    }
    ?>
    Then to upload the images (page addImage.php):
    PHP Code:
    <?
    define
    ('LARGE_IMAGE_DIR''some/path');
    define('THUMBNAIL_DIR''some/path');


    if (
    strcasecmp($_POST['submit'], 'submit') === 0) {
        
    $albumid = (int) $_POST['albumid'];

        if (
    is_uploaded_file($_FILES['image']['tmp_name'])) {
            
    $type getImageSize($_FILES['image']['tmp_name']);
            
    $type $type[2];
            
    //$type = exif_imagetype($_FILES['image']['tmp_name']);
            
    switch ($type) {
                case 
    2:
                    
    $ext 'jpeg';
                break;
                case 
    3:
                default:
                    
    $ext 'png';
                break;
            }
            
    $result mysql_query('INSERT INTO Images SET extension = \'' $ext '\', Album_id = ' $albumid) or die(mysql_error());
            
            
    $filename mysql_insert_id() . '.' $ext;
            
            
    copy($_FILES['image']['tmp_name'], LARGE_IMAGE_DIR $filename);
        
            
    $maxx 100;
            
    $maxy 100;
            if (
    $ext == 'jpeg')
                
    $src_img ImageCreateFromJPEG(LARGE_IMAGE_DIR $filename);
            else if (
    $ext == 'png')
                
    $src_img ImageCreateFromPNG(LARGE_IMAGE_DIR $filename);

            
            
            
    $oldx imageSX($src_img);
            
    $oldy imageSY($src_img);

            if (
    $oldx $maxx)
                
    $maxx $oldx;
            if (
    $oldy $maxy)
                
    $maxy $oldy;

            
    $hlut1 $maxx $maxy;
            
    $hlut2 $oldx $oldy;

            if(
    $hlut2 $hlut1) {
                
    $hlut $maxx $oldx;
                
    $breidd $maxx;
                
    $haed $oldy $hlut;
            } elseif (
    $hlut1 $hlut2) {
                
    $hlut $maxy $oldy;
                
    $haed $maxy;
                
    $breidd $oldy $hlut;
            } elseif (
    $hlut1 == $hlut2) {
                
    $haed $maxy;
                
    $breidd $maxx;
            }

            
    $dst_img=ImageCreateTrueColor($breidd,$haed);
            
    ImageCopyResampled($dst_img,$src_img,0,0,0,0,$breidd,$haed,$oldx,$oldy);
            if (
    $ext == 'jpeg')
                
    ImageJPEG($dst_imgTHUMBNAIL_DIR 'thumb_' $filename);
               else if (
    $ext == 'png')
                
    ImagePNG($dst_imgTHUMBNAIL_DIR 'thumb_' $filename);


            
    Imagedestroy($dst_img);
            
    Imagedestroy($src_img);
            echo(
    'Success');
        }
    } else {    

        
    $result mysql_query('SELECT id, name FROM Albums');
        
        
    ?><form action="<? echo($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
        
        File: <input type="file" name="image" /><br />
        Album: <select name="albumid">
        <?
        
    while ($arr mysql_fetch_assoc($result)) {
            
    ?>
            <option value="<? echo($arr['id']); ?>"><? echo($arr['name']); ?></option><br />
            <?
        
    }
        
    ?>
        </select><br />
        <input type="submit" name="submit" value="Submit" />
        
        </form><?
    }
    ?>
    This does not support .gif images. This script should work, I have tested it, all left now is to display them, try to figure that out yourself
    - website

  17. #17
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol ok...

    but gimme a few days

    i want to go through the php code and actually understand whats going on

    Santosh Sankar
    Bullish Bankers

  18. #18
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do i have to do some alterations in php.ini?
    I mean in respect to have GD (which i dont know what it is)?
    Because i get this error message:
    Fatal error: Call to undefined function: imagecreatefromjpeg() in C:\Programas\Apache Group\Apache2\htdocs\PHP\v1\addImage.php on line 45

    here's the line:
    PHP Code:
    $src_img imagecreatefromjpeg(LARGE_IMAGE_DIR $filename); 
    at the beguyining of the script, i put the path to the big images:
    PHP Code:
    define('LARGE_IMAGE_DIR''normal/'); 
    In the manual (http://pt2.php.net/manual/en/function.imagecreatefromjpeg.php)
    someone says to alter things in the php.ini, i did that, but got error messages too, thought diferent ones.

  19. #19
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it!
    have to change the path to extensions: (in my case)
    "extension_dir = C:\PHP\php-4.3.1-Win32\extensions"
    and uncoment
    'extension=php_gd2.dll'

  20. #20
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    This does not support .gif images. This script should work, I have tested it(...) [img]images/smilies/smile.gif[/img]
    me too...
    PHP Code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-type"
    content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
    <?php 
    include ('bd.php'); 
    ?>
    <?php 
     $result 
    mysql_query('SELECT id, name FROM Albums'); 
         
        
    ?>
      <form action="<? echo($_SERVER['PHP_SELF']); ?>" method="post" > 
            
        Album: <select name="albumid"> 
        <? 
        
    while ($arr mysql_fetch_assoc($result)) { 
            
    ?> 
            <option value="<? echo($arr['id']); ?>"><? echo($arr['name']); ?></option><br /> 
            <? 
        

        
    ?> 
        </select><br /> 
        <input type="submit" name="submit" value="Submit" /> 
         
        </form>
    <?php
    $album
    =$_POST['albumid'];
    $sql="select * from images where album_id="$album;
    ?>
    <table border="1">
    <tr><td>fotos</td></tr>
    <?php
    $resultado 
    = @mysql_query($sql);
    if (!
    $resultado) {
      echo(
    '</table>');
      die(
    '<p>Erro ao obter conteúdo da base de dados!<br />'.
          
    'Erro: ' mysql_error() . '</p>');
    }
    while (
    $tudo mysql_fetch_array($resultado)) {
      echo(
    "<tr valign='top'>\n");
     
    $extensao  = ($tudo['extension']);
      
    $id       $tudo['id'];
     echo(
    "<td><img src =normal/".$id.".".$extensao." /></td>\n");
      echo(
    "</tr>\n");
    }
    ?>
    </table>
    </body>
    </html>
    Thanks to website and everyone who helped me

  21. #21
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol im gonan print the code so i can read it and go through it properly...

    Santosh Sankar
    Bullish Bankers

  22. #22
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's my include:
    PHP Code:
    <?php 
        $dbcnx 
    = @mysql_connect('...''...''...');
        if (!
    $dbcnx) {
          die( 
    '<p>De momento não é possível localizar o servidor de dados.</p>' );
        }
        if (! @
    mysql_select_db('...')) {
          die( 
    '<p>De momento não é possível localizar a base de dados.</p>' );
        }  
    ?>

  23. #23
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    must confess i didnt pay atention to the miniature stuff, though it would be nice if it could be stored too, since in that case one could show them and hiperlink dinamically each miniature to the normal pic size page

  24. #24
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that i looked at it again, i correct myself.
    That can be done.
    I mean, show the hiperlinked miniatures and link each one to the normal one.
    Here it is how i did it:
    I have this page (mini.php) that shows all miniatures given a choice in the album;
    the code:
    PHP Code:
    <html>
    <head>
    <title>galeria</title>
    <meta http-equiv="Content-type"
    content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
    <?php 
    include ('bd.php'); 
    ?>
    <?php 
    if ($_POST['albumid']) {
    $result mysql_query('SELECT id, name FROM Albums'); 

    ?>
    <form action="<? echo($_SERVER['PHP_SELF']); ?>" method="post" > 

    Album: <select name="albumid"> 
    <? 
    while ($arr mysql_fetch_assoc($result)) { 
    ?> 
    <option value="<? echo($arr['id']); ?>"><? echo($arr['name']); ?></option><br /> 
    <? 

    ?> 
    </select><br /> 
    <input type="submit" name="submit" value="Submit" /> 

    </form>
    <?php
    $album
    =$_POST['albumid'];
    $sql="select * from images where album_id="$album;
    ?>
    <table border="1">
    <tr><td>Miniaturas</td></tr>
    <?php
    $resultado 
    = @mysql_query($sql);
    if (!
    $resultado) {
    echo(
    '</table>');
    die(
    '<p>Erro ao obter conteúdo da base de dados!<br />'.
    'Erro: ' mysql_error() . '</p>');
    }
    while (
    $tudo mysql_fetch_array($resultado)) {
    echo(
    "<tr valign='top'>\n" );
    $extensao $tudo['extension'];
    $id $tudo['id'];
    [
    'titulo'].'</a><br />'."\n" );
    echo(
    "<td><a href=""\"galeria2.php?id=$id&extensao=$extensao\"><img border=0 src =miniatura/thumb_".$id.".".$extensao." /></a></td>\n" );
    echo(
    "</tr>\n" );//miniatura is the folder of miniatures
    }
    }
    ?>
    </table>
    </body>
    </html>
    Then, that page links to page (galeria2.php)
    The code for this page:
    PHP Code:
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-type"
    content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
    <?php 
    $id
    =$_GET['id'];
    $extensao=$_GET['extensao'];
    echo(
    "<img src = normal/".$id.".".$extensao ." />" );//normal is the folder of normal pics
    ?>
    </body>
    </html>
    And it works!

  25. #25
    SitePoint Evangelist netkid's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    website...where do i put in the database access stuff in ur version??

    Santosh Sankar
    Bullish Bankers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •