Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

show image with its size on php

Posted on 2004-10-26
29
Medium Priority
?
254 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
Comment
Question by:Xtry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 2
  • +5
29 Comments
 

Author Comment

by:Xtry
ID: 12413250
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
ID: 12413278
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
ID: 12413474
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:cLFlaVA
ID: 12413481
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
ID: 12413486
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
KvdnBerg earned 200 total points
ID: 12413568
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
ID: 12413654
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:
hernst42 earned 200 total points
ID: 12413937
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
ID: 12413985
thanks hernst.  hadn't noticed that.
0
 
LVL 7

Expert Comment

by:BenMorel
ID: 12414042
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
ID: 12414074
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
ID: 12414508
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
ID: 12414570
Like  hernst42  said, scandir is a php5 function. He suggests another way to handle it.
0
 
LVL 13

Assisted Solution

by:cLFlaVA
cLFlaVA earned 400 total points
ID: 12414688
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
ID: 12414789
thank you guys
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:Xtry
ID: 12414919
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
ID: 12414953
1 2 3 4 5 6 7 8 etc...... until i stop it
0
 
LVL 13

Expert Comment

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

Expert Comment

by:cLFlaVA
ID: 12415044
which was my mistake.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12415138
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
ID: 12415164
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
ID: 12415245
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
ID: 12415278
*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
ID: 12415366
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
suresh_asp earned 1200 total points
ID: 12419368
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
ID: 12420569
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
ID: 12422161
thank you very much!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

597 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