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

$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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值