简体   繁体   English

在PHP中提取缩略图

[英]Extract thumbnails in PHP

I have a slider (flexslider) that I'm using to display images in the form shown in the below jsfiddle... I optimized the slider so that it extracts images (which are named using numbers eg:12364, 50046) dynamically from a certain directory based on its names. 我有一个滑块(flexslider),我用它来显示以下jsfiddle中显示的形式的图像...我优化了滑块,以便它动态地从一个提取图像(使用数字命名,例如:12364,50046)某个目录基于其名称。

JSFIDDLE: https://jsfiddle.net/atkumqpk/1/ JSFIDDLE: https ://jsfiddle.net/atkumqpk/1/

Code for extracting the images: 提取图像的代码:

<?php
function get_slide_images($folder, $images_per_slide = 10, $starts_with = '') {

    $slide_images = false;

    // valid extensions
    $extensions = array(
        "jpg",
        "gif",
        "jpeg",
        "svg",
        "png",
        "bmp",
        "JPG"
    );

    // Implode the extensions array into a string:
    $extensions = implode(',', $extensions);

    if (file_exists($folder)) {
        // Get all the files with a valid extension in $folder:
        // (optionally filtered by $starts_with)
        foreach (glob($folder.'/{'.$starts_with.'}*.{'.$extensions.'}', GLOB_BRACE) as $filename) {
            $slide_images[$filename] = "<img src='{$filename}' alt='{$filename}' />";
        }

        if (!empty($slide_images)) {
            ksort($slide_images);
            $slide_images = array_chunk($slide_images, $images_per_slide);
        }

    }

    return $slide_images;
}
?>

<div id="logo" class="logo" ><img src="logo.png"/></div>
<p class="custom-class"><a href="">Go to the main website</a></p>

<div id="menu" class="menu">
    <ul class="headlines">
         <li id="item1">
        <button>aaaaaaaa</button>
        </li>
        <li id="item2">
            <button>bbbbbbb</button>
        </li>
        <li id="item3">
            <button>ccccccc</button>
        </li>
        <li id="item4">
            <button>dddddddd</button>
        </li>
        <li id="item5">
            <button>eeeeeee eee.</button>
        </li>
        <li id="item6">
            <button>ffffff</button>
        </li>
        <li id="item7">
            <button>ggggggg</button>
        </li>

    </ul>
</div>


<div id="container">
    <div id="first" class="inner-container">
        <div id="item11" class="item">
            <a name="item11"></a>
            <div class="flexslider">
                <ul class="slides">
<?php
$slider_kvp = get_slide_images("images", 10, "1");

/**
* Here we are going to generate the SLIDES
*/
if ($slider_kvp) {

    $slider_list_html = array();

    foreach($slider_kvp as $slider_key => $slide_images) {
        $html_LI_list = "";
        $html_LI_list = "<li>";

        // Go through each image ...
        foreach($slide_images as $image_key => $image_value) {
            $html_LI_list .= $image_value;
        }

        $html_LI_list .= "</li>";
        $slider_list_html[$slider_key] = $html_LI_list;
    }

    // OUR SLIDES!
    $rendered_slider_list_html = implode(' ', $slider_list_html);
    echo "<ul class='slides'>{$rendered_slider_list_html}</ul>";
}
?>

                </ul>
            </div>
        </div>
    </div>
</div>

Now the problem is that when I had the original slider (before optimizing it) I connected it to "fancybox" to display thumbnails and hidden images. 现在的问题是,当我有原始滑块(优化之前)时,我将它连接到“fancybox”以显示缩略图和隐藏图像。 However now I have no idea on how to connect it to images that are being extracted using php. 但是现在我不知道如何将它连接到使用php提取的图像。

Code of the Fancybox. Fancybox的代码。 JSFIDDLE: http://jsfiddle.net/ny9ytae5/2/ JSFIDDLE: http//jsfiddle.net/ny9ytae5/2/

Case: inside the directory images (which I'm extracting the images from) i have images that are named in numbers (eg:54236), and for each image an equivalent folder with the same name (eg: for image 54236 there is a folder 54236). 案例:在目录图像内部(我正在从中提取图像)我有以数字命名的图像(例如:54236),并且对于每个图像,具有相同名称的等效文件夹(例如:对于图像54236,存在文件夹54236)。 The content of the folder 54236 are the thumbnails that needs to be connected to the image 54236. 文件夹54236的内容是需要连接到图像54236的缩略图。

I was informed by stackoverflow member "JFK" that i can do this:" $slide_images[$filename] = "<img src='{$filename}' alt='{$filename}' />"; could be changed into this $slide_images[$filename] = "<a class='fancybox' data-fancybox-group='thumb1' href='{$filename}'><img src='{$filename}' alt='{$filename}' /></a>"; ... the only issue is that you would be using the same image as thumbnail, which will be adding an overhead to your pageload. " stackoverflow成员“JFK”告诉我,我可以这样做:“ $slide_images[$filename] = "<img src='{$filename}' alt='{$filename}' />";可以改成这个$slide_images[$filename] = "<a class='fancybox' data-fancybox-group='thumb1' href='{$filename}'><img src='{$filename}' alt='{$filename}' /></a>"; ...唯一的问题是你将使用与缩略图相同的图像,这将增加页面加载的开销。”

And suggested using this tutorial: http://www.picssel.com/create-a-filtered-image-gallery-with-jquery-and-fancybox-part-2-create-image-thumbnails-with-php/ 建议使用本教程: http//www.picssel.com/create-a-filtered-image-gallery-with-jquery-and-fancybox-part-2-create-image-thumbnails-with-php/

However I failed to make it work. 但是我没能成功。 Any help please? 有什么帮助吗?

In "JFK" suggestion, you should point the src property to your thumbnail image (and let the href property to the big size image) 在“JFK”建议中,您应该将src属性指向缩略图图像(并将href属性设置为大尺寸图像)

Something like 就像是

$slide_images[$filename] = "<a class='fancybox' data-fancybox-group='thumb1' href='{$filename}'><img src='{$filename}/thumbnail.jpg' alt='{$filename}' /></a>";

Hence the thumbnail will be loaded imediately, but the big image will be loaded only when needed. 因此,缩略图将立即加载,但只有在需要时才会加载大图像。

I think you can start with something like this 我认为你可以像启动

if you want to make it on the fly you simply can pass the height or width (I suggest you to calculate on the fly the other so you don't lose the proportions); 如果你想在飞行中制作你只需要通过高度或宽度(我建议你在飞行中计算另一个,这样你就不会失去比例); if you want to cache it just save the img and check if you have the image (you can use a custom name like title-width-height.jpg and check if it exists) 如果你想缓存它只是保存img并检查你是否有图像(你可以使用自定义名称,如title-width-height.jpg并检查它是否存在)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM