简体   繁体   English

缩略图生成在服务器上不起作用-PHP Gallery

[英]Thumbnail Generation not working on server - PHP Gallery

This works perfectly fine on localhost but doesn't work on my server, could anyone tell me why? 这在localhost上可以正常运行,但在我的服务器上不起作用,有人可以告诉我为什么吗? I've got php-gd (php-gd-5.1.6-32.el5.x86_64) on my Centos 5 box. 我在Centos 5盒子上安装了php-gd(php-gd-5.1.6-32.el5.x86_64)。

I have absolutely no idea why it wouldn't be working on the server so, if anyone has any ideas... 我绝对不知道为什么它不能在服务器上工作,如果有人有任何想法...

Here's the code; 这是代码;

    <?php

if (isset($_GET['img'])){
    // make thumbnail
    if(file_exists($_GET['img'])){
        ignore_user_abort(true);
        set_time_limit(120);
        ini_set('memory_limit', '512M');

        $src_size = getimagesize($_GET['img']);

        if($src_size === false){
            die('Thats not an image!');
        }

        $thumb_width    = 250;
        $thumb_height   = 200;

        if($src_size['mime'] === 'image/jpeg'){
            $src = imagecreatefromjpeg($_GET['img']);
        }else if($src_size['mime'] === 'image/png'){
            $src = imagecreatefrompng($_GET['img']);
        }else if($src_size['mime'] === 'image/gif'){
            $src = imagecreatefromgif($_GET['img']);
        }

        $src_aspect = round(($src_size[0] / $src_size[1]), 1);
        $thumb_aspect = round(($thumb_width / $thumb_height), 1);

        if($src_aspect < $thumb_aspect){
            //higher
            $new_size = array($thumb_width,($thumb_width / $src_size[0]) * $src_size[1]);
            $src_pos = array(0,(($new_size[1] - $thumb_height) * ($src_size[1] / $new_size[1])) / 2);
        }else if ($src_aspect > $thumb_aspect){
            //wider.
            $new_size = array(($thumb_width / $src_size[1]) * $src_size[0],$thumb_height);
            $src_pos = array(($new_size[0] - $thumb_width) / 2, 0);
        }else{
            // same shape
            $new_size = array($thumb_width, $thumb_height);
            $src_pos = array(0, 0);
        }

        if($new_size[0] < 1) $new_size[0] = 1;
        if($new_size[1] < 1) $new_size[0] = 1;

        $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
        imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]);

        if($src_size['mime'] === 'image/jpeg'){
            imagejpeg($thumb, "thumbs/{$_GET['img']}");
        }else if($src_size['mime'] === 'image/png'){
            imagepng($thumb, "thumbs/{$_GET['img']}");
        }else if($src_size['mime'] === 'image/gif'){
            imagegif($thumb, "thumbs/{$_GET['img']}");
        }

        header("Location: thumbs/{$_GET['img']}");
    }   
    die();
}

if (is_dir('./thumbs') === false){
    mkdir('./thumbs', 0744);
}

$images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE);

?>
<!DOCTYPE>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div>
            <?php

            foreach($images as $image){
                if(file_exists("./thumbs/{$image}")){
                    echo "<a href=\"{$image}\" target=\"_blank\"><img style='border: 4px solid black;' src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
                }else{
                    echo "<a href=\"{$image}\" target=\"_blank\"><img style='border: 4px solid black;' src=\"?img={$image}\" alt=\"{$image}\" /></a>";
                }
            }

            ?>
        </div>
    </body>
</html>

您确定服务器上还安装了gd库吗?

According to my knowledge I feel there could be few reasons for this. 据我所知,我认为可能没有什么原因。

  1. GD library is not installed - You can find installation instruction from here. 尚未安装GD库-您可以在此处找到安装说明。 http://www.php.net/manual/en/image.installation.php http://www.php.net/manual/zh/image.installation.php
  2. It could be a permission issue. 这可能是权限问题。 You need write permission in particular directory to create thumbnail. 您需要在特定目录中具有写权限才能创建缩略图。
  3. PHP execution not having sufficient memory. PHP执行没有足够的内存。 You can try with bellow code to set the memory limit manually. 您可以尝试使用下面的代码手动设置内存限制。 Put this at the beginning of the function. 将其放在函数的开头。 ini_set('memory_limit', -1); And reset it at the end of the function before return results. 并在返回结果之前在函数末尾对其进行重置。 ini_restore('memory_limit');

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

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