Solved

show image with its size on php

Posted on 2004-10-26
218 Views
Last Modified: 2010-05-19
 Hello there experts,
  I have a folder called skating that has 20 pictures,
  I don’t want to make 20 pages like
  image1.html image2.html image3.html etc....
  is there anyway to make some index.php that can output
  for me the image and put 1 2 3 4 5 Next >>> for the other
  images? Lets say i have 20 images and instead of showing them
  all, just show each one per page in the same index.php
  all my images are .jpg so i don’t have to specify which images
  to output, so i would like to have the same size as the image
  if the image is 600x500 or 1200x300 show the size they have
  is that possible? =/
0
Question by:Xtry
    29 Comments
     

    Author Comment

    by:Xtry
    this is the example of my baseball page the image01.html
    I only have 4 images in that folder

    <html>
    <head>
    <title>baseball</title>
    </head>
    <BODY bgcolor="#406F9D">
    <div align="center">
    <img src="01.jpg" width="700" height="500">
    <BR>
      <a href="baseball01.html"><font color="#CCCCCC" size="2" face="verdana">1 </font></a>
      <a href="baseball02.html"><font color="#CCCCCC" size="2" face="verdana">2 </font></a>
      <a href="baseball03.html"><font color="#CCCCCC" size="2" face="verdana">3 </font></a>
      <a href="baseball04.html"><font color="#CCCCCC" size="2" face="verdana">4 </font></a>
      <a href="baseball02.html"><font color="#CCCCCC" size="2" face="verdana">Next >>>
      </font></a></div>
    </body>
    </html>
    0
     

    Author Comment

    by:Xtry
    I'm just looking for an easy way thats all.
    Is that it takes a lot of time to make 100
    .html when I take 100 pictures
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    something like this (untested) may do the trick:

    $id = isset($_GET['i']) ? $_GET['i'] : 1;
    $pic_array = scandir('/path/to/images');
    $count = 1;

    echo '<img src="/path/to/images/' . $id . '">';

    for ($i = 0; Ii < count($pic_array); $i++) {
        if ($pic_array[$i] != '.' && $pic_array[$i] != '..') {
            echo '<a href="index.php?i=' . $pic_array[$i] . '">' . $count . '</a> ';
        }
    }

    echo '<a href="index.php?i=' . $count + 1 . '">Next &raquo;</a> ';
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    Forgot to increase the counter:

    for ($i = 0; Ii < count($pic_array); $i++) {
        if ($pic_array[$i] != '.' && $pic_array[$i] != '..') {
            echo '<a href="index.php?i=' . $pic_array[$i] . '">' . $count . '</a> ';
            $count = $count + 1;
        }
    }
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    And this:

    echo '<a href="index.php?i=' . $count + 1 . '">Next &raquo;</a> ';

    Should really be this:

    echo '<a href="index.php?i=' . $id + 1 . '">Next &raquo;</a> ';
    0
     
    LVL 3

    Assisted Solution

    by:KvdnBerg
    If you're using image names with 2 digits like your example (01 etc), you will need to add a 0 to 1 - 9 in the given code cy CLFlaVA

    So it will become this:

    if (strlen($id) == 1 {
    $id = "0" . $id;
    }
    echo '<img src="/path/to/images/' . $id . '">';
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    I think you are mistaken...

    I have used scandir() which loads an array of filenames from the directory.

    I then looped through the file names, and, as long as they weren't . or .. i use the file name as a whole, passed through the url.

    I then use that file name to display the image.
    0
     
    LVL 48

    Accepted Solution

    by:
    Just as note scandir is only available in PHP5, but ist the same as:

    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
       $pic_array[] = $filename;
    }
    closedir($dir);
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    thanks hernst.  hadn't noticed that.
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    If you want to know the size of the image, you can call getimagesize() (does not require GD lib) :
    <?php
    list ($width, $height) = getimagesize('/path/to/image.jpg');
    // Now you can use $width & $height !
    ?>

    See ya ;)
    Ben
    0
     
    LVL 48

    Expert Comment

    by:hernst42
    To let the images get as big as they are just leave the width and hight attributes away from the img tag. The brwoser then will show them as big as they are. So you don't need to check the sizes via php just use

    <img src="01.jpg">
    0
     

    Author Comment

    by:Xtry
    I get this error
    Fatal error: Call to undefined function: scandir() in /home/website/baseball/index.php on line 8

    <html>
    <head>
    <title>baseball</title>
    </head>
    <body>
    <?PHP
    $id = isset($_GET['i']) ? $_GET['i'] : 1;
    $pic_array = scandir('/home/website/baseball/');
    $count = 1;

    echo '<img src="/home/website/baseball/' . $id . '">';

    for ($i = 0; Ii < count($pic_array); $i++) {
        if ($pic_array[$i] != '.' && $pic_array[$i] != '..') {
            echo '<a href="index.php?i=' . $pic_array[$i] . '">' . $count . '</a> ';
            $count = $count + 1;
        }
    }
    echo '<a href="index.php?i=' . $id + 1 . '">Next &raquo;</a> ';
    ?>
    </body>
    </html>
    0
     
    LVL 3

    Expert Comment

    by:KvdnBerg
    Like  hernst42  said, scandir is a php5 function. He suggests another way to handle it.
    0
     
    LVL 13

    Assisted Solution

    by:cLFlaVA
    utilizing henst42's suggestion:

    <?PHP
    $id = isset($_GET['i']) ? $_GET['i'] : 1;

    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
       $pic_array[] = $filename;
    }
    closedir($dir);

    $count = 1;

    echo '<img src="/home/website/baseball/' . $id . '">';

    for ($i = 0; Ii < count($pic_array); $i++) {
        if ($pic_array[$i] != '.' && $pic_array[$i] != '..') {
            echo '<a href="index.php?i=' . $pic_array[$i] . '">' . $count . '</a> ';
            $count = $count + 1;
        }
    }
    echo '<a href="index.php?i=' . ($id + 1) . '">Next &raquo;</a> ';
    ?>
    0
     

    Author Comment

    by:Xtry
    thank you guys
    0
     

    Author Comment

    by:Xtry
    oh but i still get an error Warning: readdir(): and Warning: closedir():
    0
     
    LVL 7

    Expert Comment

    by:BenMorel
    Did you put a line :
    $dir = "/path/of/your/photo/dir";
    0
     

    Author Comment

    by:Xtry
    yes, but when i put that line it shows me a lot of numbers and the
    scroll bar never stops going down and keeps showing me some
    numbers.
    0
     

    Author Comment

    by:Xtry
    1 2 3 4 5 6 7 8 etc...... until i stop it
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    What is Ii?   It should just be i in the for loop, not Ii
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    which was my mistake.
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Always nice to see people having tested the stuff before the put it on the web *thumbsup*

    -r-
    0
     
    LVL 18

    Expert Comment

    by:arantius
    http://www.google.com/search?q=image+gallery+php
    Results 1 - 10 of about 3,720,000 for image gallery php

    Yes, a few people have wanted to do this before.  There's a lot of free image gallery scripts around.
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    sarcasm necessary.
    my purpose here is not to provide fully operable solutions for people, Roonaan, but rather to point them in the right direction.

    always nice to see people making unnecessary comments at unnecessary times.
    0
     
    LVL 13

    Expert Comment

    by:cLFlaVA
    *especially* when I even specified that I had not tested the code.

    I'll be sure to make you very aware the next time you make a mistake on here.
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Well, of course I understand you want to direct people in a good direction ClFlaVA, and your are indeed doing that realy often.

    I just intended to point out that although scripting everything by hand is one solution, you should work it out properly before posting, or either state - like arantius did -, to use a already thought out widely available gallery.

    This way you would show expertise as to judge when problems need a custom solution or can use a general solution. The general solution offering much more perspective in this case.

    -r-
    0
     
    LVL 2

    Assisted Solution

    by:suresh_asp
    Try this

    simple one

    <?php
    if (isset($_GET['current'])){
          echo "page ".$_GET['current']."<br>";
    ?>
    <img src="/imgs/image<?php echo $_GET['current'];?>.jpg"><br>
    <?php
          list($width, $height) = getimagesize("/home/pricedunk/www/imgs/image".$_GET['current'].".jpg");
          echo "<b>Width: ".$width."</b><br>";
          echo "<b>Height: ".$height."</b><br>";
    }

    $fileCount = 0;
    if ($handle = opendir('/home/pricedunk/www/imgs/')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && !is_dir($file) && substr($file,-3) == "jpg") {
                      $fileCount++;
            }
        }
        closedir($handle);
    }

    for($i=1;$i<=$fileCount;$i++){
    ?>
          <a href="?current=<?php echo $i; ?>"><?php echo $i; ?></a>
    <?php } ?>
    0
     
    LVL 2

    Expert Comment

    by:suresh_asp
    Please note: with respect to above code and change path

    As you are specific regarding image names like

    image1.jpg, image2.jpg etc

    and images path should be changed to your images directory
    0
     

    Author Comment

    by:Xtry
    thank you very much!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    913 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

    15 Experts available now in Live!

    Get 1:1 Help Now