Image Display

He has: 1,380 posts

Joined: Feb 2002

Hey guys, I have no idea where to even start on this. All I would like would be a few hints or examples of code, nothing complete. I wanna try this one on my own again...

I would like to have all the images in a directory displayed individually and neatly, and then have them link to images in another directory (in PHP). For example: a picture of Eminem (thumbnail) is displayed on the page (from the /img/thumb dir), click on it, and it shows the full picture (from the /img/pic dir). Any ideas? Thanks.

P.S. Would there be any way to associate text with the thumb/full picture without putting it on the picture?

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Can be done in PHP:
(Don't have time to write it now, here's how you'd do it)
Get the contents of the directory
Split it into an array
Foreach the array into images and split that into groups of 10s
Show thumbnails with links

readdir()
The user comment there could be of use

Laughing out loud

He has: 1,380 posts

Joined: Feb 2002

wow, way over my head.

any links to check out other than the readdir() or examples that you can give me? the php.net link didn't make alot of sense, and I would rather try and write my own than copy someone's.

He has: 1,758 posts

Joined: Jul 2002

I have a script for doing this... I'll be back in a min with the code. Smiling

He has: 1,758 posts

Joined: Jul 2002

<?php
$SCRIPT_NAME
=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//    the directory name
$handle=opendir('full/');

//    initialize variables
$pics=array();
$count=0;
//    read directory into pics array
while (($file = readdir($handle))!==false) {
   
//    filter for jpg, gif or png files...
   
if (substr($file,-4) == \".jpg\" || substr($file,-4) == \".gif\" || substr($file,-4) == \".png\" || substr($file,-4) == \".JPG\" || substr($file,-4) == \".GIF\" || substr($file,-4) == \".PNG\"){
       
$pics[$count] = $file;
       
$count++;
    //    don't forget to close the filter conditions here!
    }
}
closedir(
$handle);

//    done reading, sort the filenames alphabetically, comment these lines if you want no sorting
sort(
$pics);
reset(
$pics);

        // loop over images
        for (
$f=0;$f<=sizeof($pics)-1;$f++){

            echo \"<a href=\\"
full/$pics[$f]\\" target=\\"_blank\\"><img src=\\"thumbs/$pics[$f]\\" hspace=\\"5\\" vspace=\\"5\\" border=\\"0\\"><br></a>\";
            // make linebreaks every 2 times!
            //
$isbr = strpos((($f+1)/2),\".\");
            //    if (!
$isbr){echo \"<br>\";}

            }
?>

I think it'll do what you need... let me know if you have any questions. Smiling

Andy

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Ya beat me to it Andy
Good work

He has: 1,380 posts

Joined: Feb 2002

cool...

is there a way, that when someone uploads a picture/photo, it copies it to another directory, but resizes the image?

Example:
I upload a picture, named 'me.jpg'
It copies it to 'menew.jpg', and resizes it from 800x600 to 400x300

thanks for the help.

They have: 238 posts

Joined: May 2002

<?php
$picture
= $_FILES[\"userfile\"][\"name\"]; // Assign the uploaded file to $picture

$size = getimagesize(\"$picture\"); // Get the size of the image
$width = $size[0];
$height = $size[1];
$newheight = $newheight / 2; // Halve the height
$newwidth = $newwidth / 2; // Halve the width


$oldimage = ImageCreateFromJPEG(\"$picture\");
// New image will be based on old image (hence \"create from\")

$newimage = ImageCreateTrueColor($newwidth,$newheight);
// Create a new JPEG image with new height and width

imagecopyresampled(
$newImage,$oldImage,0,0,0,0,$newwidth,$newheight,$width,$height);
ImageJPEG(
$newimage,\"$picture\");
// Old image will be added/pasted onto to the new image
// Create the image

copy(
$picture, \"PATH TO WHERE IMAGE WILL IMAGE BE COPIED\");
// Copy it to another directory

unlink(\"
$picture\");
// Delete the image
?>

I think that should work..

He has: 1,380 posts

Joined: Feb 2002

alright, well i put it in with the upload script, and it doesn't work. (i had to mod it a little bit)...here's the errors i get (i tried to upload mountains.jpg):

Warning: getimagesize: Unable to open 'mountains.jpg' for reading. in /.../do_upload.php on line 86

Warning: imagecreatefromjpeg: Unable to open 'mountains.jpg' for reading in /.../do_upload.php on line 93

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /.../do_upload.php on line 99

Warning: imagejpeg: unable to open 'mountains.jpg' for writing in /.../do_upload.php on line 100

Warning: Unable to open 'mountains.jpg' for reading: No such file or directory in /.../do_upload.php on line 104
'

and the php (including the final upload part of my code):

<?php
if ($_FILES['img1'] != \"\") {
   
    copy(
$_FILES['img1']['tmp_name'], \"$ufolder\".$_FILES['img1']['name'])
        or die(\"Couldn't copy the file!\"); 
   
} else {
   
    die(\"No input file specified\");
}
if (
$_FILES['img1'] != \"\") {

   
   
$picture = $_FILES[\"img1\"][\"name\"]; // Assign the uploaded file to $picture

   
$size = getimagesize(\"$picture\"); // Get the size of the image
   
$width = $size[0];
   
$height = $size[1];
   
$newheight = $newheight / 2; // Halve the height
   
$newwidth = $newwidth / 2; // Halve the width


   
$oldimage = ImageCreateFromJPEG(\"$picture\");
    // New image will be based on old image (hence \"create from\")

   
$newimage = ImageCreateTrueColor($newwidth,$newheight);
    // Create a new JPEG image with new height and width

    imagecopyresampled(
$newImage,$oldImage,0,0,0,0,$newwidth,$newheight,$width,$height);
    ImageJPEG(
$newimage,\"$picture\");
    // Old image will be added/pasted onto to the new image
    // Create the image

    copy(
$picture, \"/home/swimtige/public_html/img/thumbs/\");
    // Copy it to another directory

} else {

    die(\"Could not copy and resize image.\");
}
?>

thanks.

They have: 238 posts

Joined: May 2002

Heres the working code straight from my image gallery script:

<?php
$uploaddir
= \"/home/swimtige/public_html/img/thumbs/\";

move_uploaded_file(
$_FILES[\"userfile\"][\"tmp_name\"], $uploaddir . $_FILES[\"userfile\"][\"name\"])

$picture = $uploaddir . $_FILES[\"userfile\"][\"name\"];
$picture_name = $_FILES[\"userfile\"][\"name\"];

$size=getimagesize(\"$picture\");
$width=$size[0];
$height=$size[1];
$newHeight=100;
$newWidth=100;


$oldImage=ImageCreateFromJPEG(\"$picture\");
$newImage=ImageCreateTrueColor($newWidth,$newHeight);
imagecopyresampled(
$newImage,$oldImage,0,0,0,0,$newWidth,$newHeight,$width,$height);
ImageJPEG(
$newImage,\"TN_\".\"$picture_name\");
$picture3=\"TN_$picture_name\";
copy(
$picture3, \"/home/swimtige/public_html/img/thumbs/$picture3\");
unlink(\"PATH TO THE FOLDER WHICH THE SCRIPT IS IN/
$picture3\");
?>

This basically uploads an image, puts it in a folder and creates a thumbnail of it in the same directory. Hopefully it'll work this time..

They have: 238 posts

Joined: May 2002

If this doesn't work I give up. I tested it on my server, by setting up an exact directory structure as you. Make sure you give write permissions to the thumb, full and upload dirs.

<?php
$uploaddir
= \"/home/swimtige/public_html/img/pics/\"; // Path to full image folder

if (move_uploaded_file(
$_FILES[\"userfile\"][\"tmp_name\"], $uploaddir . $_FILES[\"userfile\"][\"name\"]))

{

// Move the original uploaded file to the pics directory

$picture = $uploaddir . $_FILES[\"userfile\"][\"name\"]; // /home/swimtige/public_html/img/pics/pic.jpg
$picture_name = $_FILES[\"userfile\"][\"name\"]; // pic.jpg
// Set a few variables


$size = getimagesize(\"$picture\");
$width = $size[0];
$height = $size[1]; // Find out the height and width attriutes of image
$newHeight = 100;
$newWidth = 100; // Set the new height and width of thumbnail


$oldimage = ImageCreateFromJPEG(\"$picture\");
$newimage = ImageCreateTrueColor($newWidth,$newHeight);
imagecopyresampled(
$newimage,$oldimage,0,0,0,0,$newWidth,$newHeight,$width,$height);

ImageJPEG(
$newimage,\"$picture_name\");
// Thumbnail picture is created in /upload/ dir

copy(
$picture_name, \"/home/swimtige/public_html/img/thumbs/$picture_name\");
// So move it to /thumbnail/ dir

unlink(\"home/swimtige/public_html/upload/
$picture_name\");
// Delete the useless thumbnail in /upload/ dir

}

else {
echo \"Image upload failed!\";
}
?>

He has: 1,380 posts

Joined: Feb 2002

ok...i'll try it

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.