dongxue9997 2012-03-28 17:14
浏览 37

GD缩略图无法在较大的文件大小上运行

I am using GD Thumbnails in order to create and store thumbnails of uploaded images. Now it all works fine when small image sizes are used, but when the files start to get bigger, say around 3MB, the thumbnails are not created.

The limit on my file upload is 4MB, and the actual image itself is uploaded, it's just that the thumbnail isn't created.

I'm not very familiar with the script so I'm not really sure why it's not working. It may also be worth noting that the thumbnails (when they are created) are uploading to two different directories, but this may be something I have missed out along the way.

Any ideas would be much appreciated.

<?php 
$imagefolder='images';
$thumbsfolder='thumbs';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
    foreach ($pics as $p)
    {
        createthumb($p,"tn_".$p,182,182);
    }
}

/*
    Function ditchtn($arr,$thumbname)
    filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
    foreach ($arr as $item)
    {
        if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
    }
    return $tmparr;
}

/*
    Function createthumb($name,$filename,$new_w,$new_h)
    creates a resized image
    variables:
    $name       Original filename
    $filename   Filename of the resized image
    $new_w      width of resized image
    $new_h      height of resized image
*/  
function createthumb($name,$filename,$new_w,$new_h)
{
    $system=explode(".",$name);
    if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) 
    {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    }
    if ($old_x < $old_y) 
    {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    }
    if ($old_x == $old_y) 
    {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    }
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    if (preg_match("/png/",$system[1]))
    {
        imagepng($dst_img,$filename); 
    } else {
        imagejpeg($dst_img,$filename); 
    }
    imagedestroy($dst_img); 
    imagedestroy($src_img); 
}

/*
        Function directory($directory,$filters)
        reads the content of $directory, takes the files that apply to $filter 
        and returns an array of the filenames.
        You can specify which files to read, for example
        $files = directory(".","jpg,gif");
                gets all jpg and gif files in this directory.
        $files = directory(".","all");
                gets all files.
*/
function directory($dir,$filters)
{
    $handle=opendir($dir);
    $files=array();
    if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
    if ($filters != "all")
    {
        $filters=explode(",",$filters);
        while (($file = readdir($handle))!==false)
        {
            for ($f=0;$f<sizeof($filters);$f++):
                $system=explode(".",$file);
                if ($system[1] == $filters[$f]){$files[] = $file;}
            endfor;
        }
    }
    closedir($handle);
    return $files;
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据