用户名:
密 码:

php 上传图片并按比例生成指定大小图

php 上传图片并按比例生成指定大小图
广州域名注册 金山域名注册 闵行域名注册 北京域名注册 徐汇域名注册 上海域名注册

这是一款图象缩略函数哦,把上传的新图片给$srcfile然后进行文件按$thumbwidth 缩小图宽最大尺寸 与$thumbheitht 缩小图高最大尺寸 生成小图。
图象缩略函数
参数说明:

$srcfile 原图地址;
$dir  新图目录
$thumbwidth 缩小图宽最大尺寸
$thumbheitht 缩小图高最大尺寸
$ratio 默认等比例缩放 为1是缩小到固定尺寸。
*/
function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0)
{
 //判断文件是否存在
if (!file_exists($srcfile))return false;
 //生成新的同名文件,但目录不同
$imgname=explode(‘/‘,$srcfile);
$arrcount=count($imgname);
$dstfile = $dir.$imgname[$arrcount-1];
//缩略图大小
$tow = $thumbwidth;
$toh = $thumbheight;
if($tow < 40) $tow = 40;
if($toh < 45) $toh = 45;   
 //获取图片信息
    $im =‘‘;
    if($data = getimagesize($srcfile)) {
        if($data[2] == 1) {
            $make_max = 0;//gif不处理
            if(function_exists("imagecreatefromgif")) {
                $im = imagecreatefromgif($srcfile);
            }
        } elseif($data[2] == 2) {
            if(function_exists("imagecreatefromjpeg")) {
                $im = imagecreatefromjpeg($srcfile);
            }
        } elseif($data[2] == 3) {
            if(function_exists("imagecreatefrompng")) {
                $im = imagecreatefrompng($srcfile);
            }
        }
    }
    if(!$im) return ‘‘;
    $srcw = imagesx($im);
    $srch = imagesy($im);
    $towh = $tow/$toh;
    $srcwh = $srcw/$srch;
    if($towh <= $srcwh){
        $ftow = $tow;
        $ftoh = $ftow*($srch/$srcw);
    } else {
        $ftoh = $toh;
        $ftow = $ftoh*($srcw/$srch);
    }
    if($ratio){
        $ftow = $tow;
        $ftoh = $toh;
    }
    //缩小图片
    if($srcw > $tow || $srch > $toh || $ratio) {
        if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
            imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
        } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
        } else {
            return ‘‘;
        }
        if(function_exists(‘imagejpeg‘)) {
            imagejpeg($ni, $dstfile);
        } elseif(function_exists(‘imagepng‘)) {
            imagepng($ni, $dstfile);
        }
    }else {
        //小于尺寸直接复制
    copy($srcfile,$dstfile);
    }
    imagedestroy($im);
    if(!file_exists($dstfile)) {
        return ‘‘;
    } else {
        return $dstfile;
    }
}
?>

 

域名频道资讯中心
.

  来源:互联网
阅读:
日期:2010-9-3
 
收藏此文 】 【 打印此文 】   
  • 上一篇:js如何获取上传文件大小
  • 下一篇:js获取内容层内容原始尺寸 滚动条高度 页面实际大小
  • TAG: php 上传图片并按比例生成指定大小图
    • 官方网址: dns110.com
    • CopyRight © 2010 域名频道 版权所有
    • 地址:上海市松江区新松江路1188弄37号 邮编:201620
    • 电话: 021-67820741 67820742 67820743 传真: 转分机805 值班电话: 021-67820743
    • 在线支持QQ: 219854  客户服务QQ:714658643  MSN:chenlh@dns110.com  Email: support@dns110.com
    • 沪ICP备06024974号