【问题标题】:PHP trying to understand how to generate thumbnail from directoryPHP试图了解如何从目录生成缩略图
【发布时间】:2016-03-18 05:33:58
【问题描述】:

这是我目前拥有的。当我将它包含在我的 index.php 中,然后在页面加载时调用该函数时,我得到一个空白页面。所以这里出了点问题,但我不知道是什么。我觉得我真的很亲近。我只想在一个目录中创建图像的缩略图,然后在 HTML 中将它们显示为一个图像列表,您可以单击它来触发灯箱。

我在 PHP 中仍然很不稳定。我正试图全神贯注地编辑目录中的图像。

<?php
function buildThumbGallery(){

    $h = opendir('/Recent_Additions/'); //Open the current directory
    while (false !== ($curDir = readdir($h))) {
        if (!file_exists('/Recent_Additions/thumbs/')) {
            $thumbDir = mkdir('/Recent_Additions/thumbs/', 0777, true);
        }else{
            $thumbDir = '/Recent_Additions/thumbs/';
        }
        $width = 200;
        $height = 200;
        foreach ($curDir as $image) {
            $filePath = $curDir."/".$image;
            $genThumbImg = $image->scaleImage($width, $height, true);
            $newThumb = imagejpeg($genThumbImg, $thumbDir, 100);

            echo '<li> <a class="fancybox" data-fancybox-group="'.basename($curDir).'" href="'.$filePath.'" title="'.basename($curDir)." ".strpbrk(basename($filePath, ".jpg"), '-').'"><img src="'.$newThumb.'"/>'.basename($curDir).'</a>';
            imagedestroy($newThumb);
        }echo '</li>';
}
?>

【问题讨论】:

  • 看起来您在处理图像后没有保存图像...也许您错过了一些对保存方法或类似方法的调用。
  • 前段时间写了一篇相关的帖子picssel.com/…

标签: php jquery fancybox-2


【解决方案1】:

你做错了几件事:

  • 您没有关闭 while 循环。
  • Readdir 已经遍历了一个目录,你的 foreach 没有做任何事情。
  • 您的回声中缺少引号。
  • 您正在对字符串调用方法scaleImage,我认为您的意思是调用函数imagescale

你错过和误解了很多东西,看看如何在这里创建缩略图:https://stackoverflow.com/a/11376379/4193448 还要看看你是否可以启用 PHP 错误,当你的代码充满错误时得到一个空白页面并没有真正的帮助是吗?

【讨论】:

  • 感谢您的建议。我会根据你的建议再试一次。我很感激。
  • 如果您有时间,我已经发布了一个新的答案,其中包含有效的代码,但不显示我的图像,我很感谢您的意见。谢谢!
【解决方案2】:

::EDIT::

在@swordbeta 的帮助下,我的脚本正常工作了。以下是供将来参考的代码:

<?php
function buildThumbGallery(){

    $curDir = "./Recent_Additions/";
    $thumbsPath = $curDir."thumbs/";

    if (!file_exists($thumbsPath)) {
        mkdir($thumbsPath, 0777, true);
    }
    foreach(scandir($curDir) as $image){
        if ($image === '.' || $image === '..' || $image === 'thumbs') continue;
        if(!file_exists($thumbsPath.basename($image, ".jpg")."_thumb.jpg")){
            // Max vert or horiz resolution
            $maxsize=200;

            // create new Imagick object
            $thumb = new Imagick($curDir.$image);  //'input_image_filename_and_location'

            $thumb->setImageFormat('jpg'); 

            // Resizes to whichever is larger, width or height
            if($thumb->getImageHeight() <= $thumb->getImageWidth()){
            // Resize image using the lanczos resampling algorithm based on width
                $thumb->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
            }else{
                // Resize image using the lanczos resampling algorithm based on height
                $thumb->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
            }

            // Set to use jpeg compression
            $thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
            $thumb->setImageCompressionQuality(100);
            // Strip out unneeded meta data
            $thumb->stripImage();
            // Writes resultant image to output directory
            $thumb->writeImage($thumbsPath.basename($image, ".jpg")."_thumb.jpg");  //'output_image_filename_and_location'
            // Destroys Imagick object, freeing allocated resources in the process
            $thumb->destroy();
        }else{
            echo '<a class="fancybox" data-fancybox-group="'.basename($curDir).'" href="'.$curDir.basename($image, "_thumb.jpg").'" title="Recent Addition - '.basename($image, ".jpg").'"><img src="'.$thumbsPath.basename($image, ".jpg")."_thumb.jpg".'"/></a>';
            echo '<figcaption>'.basename($image, ".jpg").'</figcaption>' . "<br/>";   
        }
    } 
}
?>

::原帖::

好的,在返回并从@swordbeta 进行更多研究和建议之后,我得到了一些有用的东西。我现在唯一的问题是我无法在 index.php 中显示图像。稍后我将在 CSS 中设置输出样式,现在我只想查看缩略图,然后将它们构建到灯箱 href 链接中:

<?php
function buildThumbGallery(){

    $curDir = "./Recent_Additions/";
    $thumbsPath = $curDir."/thumbs/";

    if (!file_exists($thumbsPath)) {
        mkdir($thumbsPath, 0777, true);
    }

    $width = 200;

    foreach(scandir($curDir) as $image){
        if ($image === '.' || $image === '..') continue;
        if(file_exists($thumbsPath.basename($image)."_thumb.jpg")){
            continue;
        }else{
            // Max vert or horiz resolution
            $maxsize=200;

            // create new Imagick object
            $thumb = new Imagick($curDir.$image);  //'input_image_filename_and_location'

            // Resizes to whichever is larger, width or height
            if($thumb->getImageHeight() <= $thumb->getImageWidth()){
            // Resize image using the lanczos resampling algorithm based on width
                $thumb->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
            }else{
                // Resize image using the lanczos resampling algorithm based on height
                $thumb->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
            }

            // Set to use jpeg compression
            $thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
            $thumb->setImageCompressionQuality(100);
            // Strip out unneeded meta data
            $thumb->stripImage();
            // Writes resultant image to output directory
            $thumb->writeImage($thumbsPath.basename($image)."_thumb.jpg");  //'output_image_filename_and_location'
            // Destroys Imagick object, freeing allocated resources in the process
            $thumb->destroy();
        }
    }   echo '<img src="'.$thumbsPath.basename($image)."_thumb.jpg".'" />' . "<br/>";
}
?>

目前,echo 的输出没有显示任何内容,但脚本的其余部分工作正常(即在 thumbs 目录中生成缩略图)。

我猜我没有正确格式化我的回声。这个脚本在我的 index.php 中被称为 &lt;?php buildThumbGallery(); ?&gt; 在一个样式化的 &lt;div&gt; 标记内。

【讨论】:

  • 好的,在进行了一些挖掘并最终确定 PHP 生成了一个 error_log 之后,似乎在 $thumb = new Imagick($curDir.$image); 处,Imagick 存在问题。错误是:没有此图像格式的解码委托经过一番谷歌搜索后,似乎我的服务器不支持 imagick 或其他什么?但它仍然能够创建我的缩略图,它只是抛出一个致命的错误,当它被调用时会破坏我的整个布局......我不知道该怎么做。有什么想法吗?
  • 我认为您收到该错误是因为您正在尝试从目录创建缩略图,因为您正在循环浏览最近的添加目录,该目录也恰好有一个 thunbs 目录。使用$image == 'thumbs' 添加或条件以跳过它。
  • 当然!根据我的循环逻辑,我现在完全明白了。非常感谢你的帮助。我希望我能给你投票,但我不会让我投票,因为我的帐户太新了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2018-09-04
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多