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 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题