您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP实现图片上传并压缩_PHP

2024/4/30 10:13:11发布15次查看
本文实例讲解了php图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下
使用到三个文件
connect.php:连接数据库 test_upload.php:执行sql语句 upload_img.php:上传图片并压缩
三个文件代码如下:
连接数据库:connect.php
query($q);if (mysqli_connect_errno()) { printf(connect failed: %s\n, mysqli_connect_error()); exit();}?>
执行sql语句:test_upload.php
prepare($insert_sql);$result -> bind_param(ss, $real_img,$small_img);$result -> execute();?>
上传图片并压缩:upload_img.php
$page_result); $retjson = json_encode($retarray); echo $retjson; return;} //生成目标文件的文件名 else{ $filename=explode(.,$_files['filename']['name']); do { $filename[0]=random(10); //设置随机数长度 $name=implode(.,$filename); //$name1=$name..mcncc; $uploadfile=$uploaddir.$name; } while(file_exists($uploadfile)); if (move_uploaded_file($_files['filename']['tmp_name'],$uploadfile)) { if(is_uploaded_file($_files['filename']['tmp_name'])) { $ret_code=1;//上传失败 } else {//上传成功 $ret_code=0; } } $retarray = array('ret_code' => $ret_code);$retjson = json_encode($retarray);echo $retjson;}//压缩图片$uploaddir_resize=upfiles_resize/;$uploadfile_resize=$uploaddir_resize.$name;//$pic_width_max=120;//$pic_height_max=90;//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩$file_type=$_files[filename]['type'];function resizeimage($uploadfile,$maxwidth,$maxheight,$name){ //取得当前图片大小 $width = imagesx($uploadfile); $height = imagesy($uploadfile); $i=0.5; //生成缩略图的大小 if(($width > $maxwidth) || ($height > $maxheight)) { /* $widthratio = $maxwidth/$width; $heightratio = $maxheight/$height; if($widthratio
请按照现实情况更改connect.php,test_upload.php中对应的信息。
以上就是php实现图片上传并压缩的方法,希望对大家的学习php程序设计有所帮助
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product