dtsps00544 2012-04-02 11:53
浏览 24
已采纳

缩略图生成无法在服务器上运行 - PHP库

This works perfectly fine on localhost but doesn't work on my server, could anyone tell me why? I've got php-gd (php-gd-5.1.6-32.el5.x86_64) on my Centos 5 box.

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>
  • 写回答

2条回答 默认 最新

  • dphnn333971 2012-04-02 11:56
    关注

    Are you sure gd libraries are installed also on the server?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器