简体   繁体   中英

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? 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>

您确定服务器上还安装了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. http://www.php.net/manual/en/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. 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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