简体   繁体   English

PHP图像缓存

[英]PHP Image Caching

I am having a problem with php image caching . 我在php图片缓存方面遇到问题。 I have already posted this on the authors issue page, however it isn't active. 我已经将其发布在作者问题页面上,但是它没有激活。

Web server: NGINX 网络服务器: NGINX

PHP Version: 5.5.9-1ubuntu4.14 PHP版本: 5.5.9-1ubuntu4.14

I am getting this error Final image URL is broken . 我收到此错误最终图像URL损坏 The page looks like this 该页面看起来像这样 页 There should be 100 images displayed but it dies at the third. 应该显示100张图像,但在第三张图像上消失。 The page should look something similar to this (this is the localhost design) 该页面应类似于此内容(这是localhost设计) 页面设计

It is returning this URL 它正在返回此URL exampleImage

There should be a / between the mediapage.zips.me and core , however it is a local image so I don't see why it should even need the host name. mediapage.zips.mecore之间应该有一个/ ,但是它是一个本地映像,所以我不明白为什么它甚至需要主机名。

How I am doing this is, 我是怎么做到的

On the top of each page I am calling another page which contains the setup of the class 在每个页面的顶部,我要调用另一个包含该类的设置的页面

<?php
    //Calls the php-image things
    include 'test.php';

?>

Inside test.php 内部test.php

<?php 
    require_once 'core/classes/ImageCache.php';
    $imagecache = new ImageCache();
?>

Image page 图片页面

<?php 
    //Directory NON-CACHED images are stored in
    $dir    =   "i/";

    //Array of files in the Directory
    $files1 =   scandir($dir);
    //Used to limit results (100's of images)
    $count = 0;
    foreach ($files1 as $key) {
        //If the file name is longer than 3 chars and the count is less 
        //than the amount of images I want displayed.
        if (strlen($key) > 3 && $count <= 100){
            $count++;
            //dir2 is the directory/nameofFile.extension
            //Eg: i/testImage.png
            $dir2   =   $dir.''.$key;
            $info = new SplFileInfo($key);
            //Image Cache variable
            $cached_src = $imagecache->cache($dir2);
?>
    <tr>
        <td>
            <!-- Link for light box for full resolution image -->
            <a href="<?php echo $dir2;?>" data-popup="lightbox">
                <!-- src of cached image is outputted and the alt is the name of the uncached image. -->
                <img src="<?php echo $cached_src;?>" alt="<?php echo $key;?>" class="img-rounded img-preview">
            </a>
        </td>
        <!-- More table stuff... -->
    </tr>

<?php
        } //End If
    } //End for
?>

I have tried adding a / to the directory name $dir but it says that it cant find the directory when I do that. 我尝试在目录名$dir添加一个/ ,但是它说在执行该操作时找不到该目录。

        <!-- Link for light box for full resolution image -->
        <a href="<?php echo $dir2; ?>" data-popup="lightbox">
            <!-- src of cached image is outputted and the alt is the name of the uncached image. -->
            <img src="/<?php echo $cached_src; ?>" alt="<?php echo $key; ?>" class="img-rounded img-preview">
        </a>

This should work by simpling prepending a slash before the $cached_src . 这应该通过在$cached_src之前加一个斜杠来$cached_src

I would take a look at ImageCache github page for reference. 我将看看ImageCache github页面以供参考。 If the cached image doesn't exist yet, the script will use $_SERVER['HTTP_HOST'] . 如果缓存的图像尚不存在,脚本将使用$_SERVER['HTTP_HOST'] So verify what is in that. 因此,请验证其中的内容。 It looks like if your $_SERVER['HTTP_HOST'] doesn't end with a slash, then your url won't have one. 看来,如果您的$_SERVER['HTTP_HOST']不以斜杠结尾,则您的网址将没有一个。

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

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