怎么新建MySQL数据库

发布时间:2022-09-20 作者:admin
阅读:382
这篇文章主要介绍“用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();
}
?>
<p><b>test_upload.php</b></p>
<?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怎样写上传图片并且压缩的功能内容,欢迎关注群英网络技术资讯频道,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

PHP
二维码-群英

长按识别二维码并关注微信

更方便到期提醒、手机管理

7*24 全天候服务

售前 400-678-4567

售后 0668-2555666

售后 400 678 4567

信息安全 0668-2555 118

域名空间 3004329145