创建缩略图不适用于40多个图像


Create thumbnails will not work with 40+ images

这里的php新手总数。。。。我一直在尽力学习,并从各种在线教程中编译了一个脚本,用于扫描服务器上的文件夹。为找到的图像生成缩略图,然后根据需要将缩略图放置到新创建的拇指文件夹中。。。然后在html页面中显示缩略图,缩略图链接回大图。

在我的测试中,一切都很好,除了当我用40多个图像实现它时,我根本没有从脚本中得到任何响应。只是一个没有正文的空白html页面。。。

正如我所说,我是新来的,但我认为这与服务器的超时有关?无法渲染图像数量或其他什么?我看到了一些关于服务器php.ini超时的事情,但不确定这是问题所在。

我只需要弄清楚为什么我不能一次拍摄40多张照片,我就会成为黄金!

<?php
# SETTINGS
$max_width = 300;
$max_height = 300;
function getPictureType($ext) {
    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 getPictures() {
    global $max_width, $max_height;
    if ( $handle = opendir(".") ) {
        $lightbox = rand();
        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) ) {
                $split = explode('.', $file); 
                $ext = $split[count($split) - 1];
                if ( ($type = getPictureType($ext)) == '' ) {
                    continue;
                }
                if ( ! is_dir('thumbs') ) {
                    mkdir('thumbs');
                }
                if ( ! file_exists('thumbs/'.$file) ) {
                    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 = $oldW * ($max_width / $oldH);
                        $newH = $max_height;
                    } else {
                        $newW = $max_width;
                        $newH = $oldH * ($max_height / $oldW);
                    }
                    $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);
                }
                echo "<div class='"box'">";
                echo '<a href="'.$file.'" class="swipebox">';
                echo '<img src="thumbs/'.$file.'" alt="" />';
                echo '</a>';
                echo '</div>';
            }
        }
    }
}
?>
<---HTML CODE BELOW HERE-->
Using this to call function above in html code:
<?php getPictures(); ?>

PHP默认超时为30秒。你可能需要改变这个:

set_time_limit(0);

如果执行脚本需要很长时间,这将阻止脚本超时。

此外,许多网络托管服务对您的页面允许使用的CPU和RAM有限制,您可能会超过这些限制。为了解决这个问题,您可以将脚本分成块,并多次运行PHP文件,一次处理大约30个图像。