【问题标题】:Create thumbnails will not work with 40+ images创建缩略图不适用于 40 多张图片
【发布时间】:2013-11-26 00:29:09
【问题描述】:

这里的总 php 新手....我一直在努力学习并从各种在线教程中编译了一个脚本,用于扫描服务器上的文件夹。为找到的图像生成缩略图,然后根据需要将缩略图放入新创建的 thumbs 文件夹中...然后在 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 秒。您可能会超过服务器上的最大值

标签: php image thumbnails


【解决方案1】:

PHP 的默认超时时间为 30 秒。您可能需要更改此设置:

set_time_limit(0);

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

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

【讨论】:

  • 感谢您的帮助,但仍然是同样的问题...我添加了这个,但仍然没有...但它适用于较小的图像采样...真令人沮丧!
  • 我在处理大量大图像时也遇到了内存问题,但我不知道如何处理它们......我会环顾四周。
  • 我刚刚在我的本地服务器上使用set_time_limit(0); 运行了您的代码,其中包含 80 张图像(2000 像素 x 1500 像素,每个大约 1MB)并且没有任何问题。你是在本地服务器上运行它还是在真实的网络上运行它?该操作在我的计算机上使用了大约 80MB RAM 和 100% CPU 大约 2 分钟。您的主机服务(或本地计算机)可能有超出此脚本的限制。
  • 你可以分解它并多次运行 PHP 文件,一次处理 30 张图像。
  • 刚刚与托管公司谈过,他们说我的限制最大为 74m....当我说 40+ 时,我不知道它停止的数字,因为我做了 30 ---然后 150+....我该如何停止您提到的功能,因为我现在至少遇到错误...致命错误:允许的内存大小为 77594624 字节耗尽(尝试分配 20736 字节)...我假设这会让事情变得更好,而不需要 2 分钟......哈哈,至少这只是我第一次运行它,从那时起缩略图就会在那里
猜你喜欢
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
  • 2013-05-31
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多