在不同的php页面上显示图像

时间:2013-01-01 20:40:02

标签: php

我正在创建一个PHP图片库,经过长时间的搜索,我在谷歌上发现了一个很好的PHP图像库代码。当我点击它发送到原始图像位置的图像的缩略图,但我想要将用户发送到同一图像显示的不同页面而不是原始图像!

这是代码!

<?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(".") ) {
                $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="'.$file.'" rel="lightbox['.$lightbox.']">';

                        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('.', $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);
        }
    ?>
抱歉,我的英语不好,希望你理解我的问题!

1 个答案:

答案 0 :(得分:2)

此代码在图片链接中包含一个rel =“lightbox ['。$ lightbox。']。它希望在浏览器中使用Lightbox来显示图像.Lightbox是一个JavaScript库(有很多克隆,也就是说,它会在现有页面上显示图像并使背景变灰。这非常好。您可能想要使用Lightbox来调查以显示图像。

否则,您需要修改该行:

echo '<li><a href="'.$file.'" rel="lightbox['.$lightbox.']">';

以便href指向您想要的页面,并将$ file作为URL中的GET值。如果你这样做,你应该取出rel =因为你不会使用它。它不会伤害任何东西,但如果你不使用Lightbox,它就会变得杂乱无章。例如:

echo '<li><a href="display.php?image='.$file.'">';

其中display.php是显示图像的页面。