<?php
define("dir","imgs/"); //存储的目录文件
if(is_uploaded_file($_FILES['file_image']['tmp_name'])){
$image_type = $_FILES['file_image']['type'];
if($image_type != "image/pjpeg"){
echo "不支持这种格式";
}else{
$name=$_POST['name'];
$do=$_POST['do'];
$ydz=dir.date("his",time()).".jpg";
$dz=dir.date("his",time())."_pep_s.jpg";
$result2=move_uploaded_file($_FILES['file_image']['tmp_name'],dir.date("his",time()).".".fileext($_FILES['file_image']['name']));
cutphoto(dir.date("his",time()).".jpg",dir."".date("his",time())."_pep_s.jpg",145,97);// 图片的宽,高
if($result2==1) echo "<font color=#00CC00><strong>恭喜!图片压缩成功!</strong></font>";
else echo "fuck";
}
}
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//生成缩略图函数
function cutphoto($o_photo,$d_photo,$width,$height){
$temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img); //取得原图宽
$o_height = imagesy($temp_img); //取得原图高
//判断处理方法
if($width>$o_width || $height>$o_height){ //原图宽或高比规定的尺寸小,进行压缩
$newwidth=$o_width;
$newheight=$o_height;
if($o_width>$width){
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
}
if($newheight>$height){
$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
}else{ //原图宽与高都比规定尺寸大,进行压缩后裁剪
if($o_height*$width/$o_width>$height){ //先确定width与规定相同,如果height比规定大,则ok
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ //否则确定height与规定相同,width自适应
$newwidth=$o_width*$height/$o_height;
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0;
}
//缩略图片
$new_img = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);
imagejpeg($new_img , $d_photo);
imagedestroy($new_img);
$temp_img = imagecreatefromjpeg($d_photo);
$o_width = imagesx($temp_img); //取得缩略图宽
$o_height = imagesy($temp_img); //取得缩略图高
//裁剪图片
$new_imgx = imagecreatetruecolor($width,$height);
imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);
imagejpeg($new_imgx , $d_photo);
imagedestroy($new_imgx);
}
}
unlink($ydz);
?>
<br />
<br />
图片地址:<input name="imgurl" type="text" id="imgurl" size="60" value="/sup/<?php
echo $dz;
?>">
<br />
<br />
<img src="/sup/<?php
echo $dz;
?>" />
<br />
<br />
<form action="do.php" method="post" enctype="multipart/form-data" name="s"><input name="do" type="hidden" value="<?php
echo $dz;
?>" /><input name="an" type="submit" value="重新生成" />
</form>
<a href="javascript:self.close()" >OK了!关闭窗口</a>
PHP生成缩略图,不变形,不保存原图
4星 · 超过85%的资源 需积分: 10 139 浏览量
2009-11-13
15:15:50
上传
评论
收藏 45KB RAR 举报
卤迅
- 粉丝: 0
- 资源: 5
最新资源
- 19ec93431a5b148599c01eda2a0de94f
- 一个简单的Python计算器程序 这个程序将使用基本的Python语法和功能,实现加、减、乘、除四种基本运算
- 三菱PLC例程源码FX与日立SJ300变频通讯,有注解
- Ai智能写作文章助手一键生成原创文章文案,支持安卓,PC,小程序
- 三菱PLC例程源码FX和EMERSON通讯案例FxModbustest
- 三菱PLC例程源码FX和EMERSONModbus通讯案例
- 推荐GitKraken - 超好用的 Git 可视化工具
- 一个简单的Python计算器程序案例,目的是用Python最简单的写法让大家明白Python的语法怎么用
- 三菱PLC例程源码FX防洪闸门控制程序
- pyecharts操作3.ipynb
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈