hi, i am creating a php based image gallery, after long search on google i found a good decent code for it.
but when i change the opendir() location in code, the thumbnail image shows blank image instead of orignal image!

here is the code!

<?php
    # SETTINGS


    $max_width = 200;
    $max_height = 200;
    $per_page = 9;

    $page = $_GET['page'];

    $has_previous = false;
    $has_next = false;

    function getPictures() {
        global $page, $per_page, $has_previous, $has_next;
        if ( $handle = opendir("gallery/.") ) {
            $lightbox = rand();
            echo '<ul id="pictures">';

            $count = 0;
            $skip = $page * $per_page;

            if ( $skip != 0 )
                $has_previous = true;

            while ( $count < $skip && ($file = readdir($handle)) !== false ) {
                if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                    $count++;
                    }

            $count = 0;
            while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
                if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                    if ( ! is_dir('thumbs') ) {
                        mkdir('thumbs');
                    }
                    if ( ! file_exists('thumbs/'.$file) ) {
                        makeThumb( $file, $type );
                    }

                    echo '<li><a href="singlepost.php?i='.$file.'">';

                    echo '<img src="thumbs/'.$file.'" alt="" />';

                    echo '<div class="fb">view</div></a></li>';


                    $count++;

                }

            }

            echo '</ul>';

            while ( ($file = readdir($handle)) !== false ) {
                if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                    $has_next = true;
                    break;
                }
            }
        }
    }

    function getPictureType($file) {
        $split = explode('gallery/.', $file); 
        $ext = $split[count($split) - 1];
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }

    function makeThumb( $file, $type ) {
        global $max_width, $max_height;
        if ( $type == 'jpg' ) {
            $src = imagecreatefromjpeg($file);
        } else if ( $type == 'png' ) {
            $src = imagecreatefrompng($file);
        } else if ( $type == 'gif' ) {
            $src = imagecreatefromgif($file);
        }
        if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
            $newW = 220;
            $newH = $max_height;
        } else {
            $newW = $max_width;
            $newH = 200;
        }
        $new = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
        if ( $type == 'jpg' ) {
            imagejpeg($new, 'thumbs/'.$file);
        } else if ( $type == 'png' ) {
            imagepng($new, 'thumbs/'.$file);
        } else if ( $type == 'gif' ) {
            imagegif($new, 'thumbs/'.$file);
        }
        imagedestroy($new);
        imagedestroy($src);
    }
?>

what mistake i did in code??

Recommended Answers

All 9 Replies

Member Avatar for diafol

ONly just scanned this:

if ( $handle = opendir("gallery/.") ) {

What about:

if ( $handle = opendir("gallery/") ) {

Is 'gallery' correct with regard to a relative reference?

It should be '../gallery/' or something similar?

it says:
Warning: imagecreatefrompng(12731_474471135931166_1099382360_n.png): failed to open stream: No such file or directory

"gallery" folder placed in root folder. . i think that only

if ( $handle = opendir("gallery/") ) {

is used?

Member Avatar for diafol

Gallery may be in the root folder, but where's this script being run from?

from js folder

is you talking about javascript?

Make sure, as diafol has pointed out, that you have the correct path which is what the error is suggesting.

Use something like this to make sure that you are not only in the working directory but also your servers root directory.

$_SERVER['DOCUMENT_ROOT'].'/gallery/'

I find that it won't always work to direct to the root folder using, /gallery/ and that when working in PHP it prefers to have the complete path.

now by doing that it dont shows any image!

You have to make sure that the gallery folder is in the same folder with the application.

Or at least ensure that you enter the correct path to these directories.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.