PHP实现图片上传并压缩,
本文实例讲解了PHP图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下
使用到三个文件
- connect.php:连接数据库
- test_upload.php:执行SQL语句
- upload_img.php:上传图片并压缩
三个文件代码如下:
连接数据库:connect.php
<?php $db_host = ''; $db_user = ''; $db_psw = ''; $db_name = ''; $db_port = ''; $sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name); $q="set names utf8;"; $result=$sqlconn->query($q); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } ?>
执行SQL语句:test_upload.php
<?php require ("connect.php"); require ("upload_img.php"); $real_img=$uploadfile; $small_img=$uploadfile_resize; $insert_sql = "insert into img (real_img,small_img) values (?,?)"; $result = $sqlconn -> prepare($insert_sql); $result -> bind_param("ss", $real_img,$small_img); $result -> execute(); ?>
上传图片并压缩:upload_img.php
<?php //设置文件保存目录 $uploaddir = "upfiles/"; //设置允许上传文件的类型 $type=array("jpg","gif","bmp","jpeg","png"); //获取文件后缀名函数 function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //生成随机文件名函数 function random($length) { $hash = 'CR-'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen($chars) - 1; mt_srand((double)microtime() * 1000000); for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } $a=strtolower(fileext($_FILES['filename']['name'])); //判断文件类型 if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)) { $text=implode(",",$type); $ret_code=3;//文件类型错误 $page_result=$text; $retArray = array('ret_code' => $ret_code,'page_result'=>$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 < $heightratio) { $ratio = $widthratio; } else { $ratio = $heightratio; } $newwidth = $width * $ratio; $newheight = $height * $ratio; */ $newwidth = $width * $i; $newheight = $height * $i; if(function_exists("imagecopyresampled")) { $uploaddir_resize = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); } else { $uploaddir_resize = imagecreate($newwidth, $newheight); imagecopyresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); } ImageJpeg ($uploaddir_resize,$name); ImageDestroy ($uploaddir_resize); } else { ImageJpeg ($uploadfile,$name); } } if($_FILES["filename"]['size']) { if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg") { //$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']); $im = imagecreatefromjpeg($uploadfile); } elseif($file_type == "image/x-png") { //$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']); $im = imagecreatefromjpeg($uploadfile); } elseif($file_type == "image/gif") { //$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']); $im = imagecreatefromjpeg($uploadfile); } else//默认jpg { $im = imagecreatefromjpeg($uploadfile); } if($im) { ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize); ImageDestroy ($im); } } ?>
请按照现实情况更改connect.php,test_upload.php中对应的信息。
以上就是PHP实现图片上传并压缩的方法,希望对大家的学习php程序设计有所帮助
您可能感兴趣的文章:
- PHP图片上传类带图片显示
- 简单的PHP图片上传程序
- php 图片上传类代码
- php图片上传存储源码并且可以预览
- PHP 图片上传代码
- thinkphp实现图片上传功能分享
- PHP实现图片压缩的两则实例
- php上传图片并压缩的实现方法

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3汉化版
中文版,非常好用

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器