您的位置:首页网页设计PHP教程 → 教你php生成缩略图的源代码

教你php生成缩略图的源代码

时间:2010/4/2 14:37:00来源:本站整理作者:我要评论(0)

教你php生成缩略图的源代码

一个非常简单的PHP生成缩略图的代码程序,参数及代码都算得上精简,有兴趣的朋友可以试下它的功能,有不太完善的地方还请指正。

<?
$FILENAME="image_name";

// 生成图片的宽度
$RESIZEWIDTH=400;

// 生成图片的高度
$RESIZEHEIGHT=400;

function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){

$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}

if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); www~phperz~com
ImageDestroy ($im);
}
}

?>

<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>

<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="浏览"><p>
<input type="submit" value="上传图片">
</form>

</body>
</html>

相关视频

    没有数据

相关阅读 php输出内容乱码解决方法php批量获取首字母(汉字、数字、英文)我的php文件怎么打开_如何打开php文件的办法破解防盗链图片的php函数php显示错误信息方法如何在IIS7下设置支持PHP程序PHP技巧--通过COM使用ADODB成就PHP高手的五个必经之路

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 PHP入门教程书籍介绍用可牛为MM无痛纹身刺 编译apache的rewrite模块实例Apache中URL Rewrite技术实现方式在PHP中有urldecode()等函数来解决网页URL编PHP中的mb_convert_encoding转换编码与icon

人气排行 在PHP中有urldecode()等函数来解决网页URL编dede 完美分页效果! pagelist修改,实现门PHP入门教程书籍介绍用IdHTTP获取UTF-8编码的网页apache泛域名解析+泛域名指向处理文件ThinkTemplate模板引擎的设计和使用方法如何用php将任何格式视频转为flv破解防盗链图片的php函数