好得很程序员自学网
  • 首页
  • 后端语言
    • C#
    • PHP
    • Python
    • java
    • Golang
    • ASP.NET
  • 前端开发
    • Angular
    • react框架
    • LayUi开发
    • javascript
    • HTML与HTML5
    • CSS与CSS3
    • jQuery
    • Bootstrap
    • NodeJS
    • Vue与小程序技术
    • Photoshop
  • 数据库技术
    • MSSQL
    • MYSQL
    • Redis
    • MongoDB
    • Oracle
    • PostgreSQL
    • Sqlite
    • 数据库基础
    • 数据库排错
  • CMS系统
    • HDHCMS
    • WordPress
    • Dedecms
    • PhpCms
    • 帝国CMS
    • ThinkPHP
    • Discuz
    • ZBlog
    • ECSHOP
  • 高手进阶
    • Android技术
    • 正则表达式
    • 数据结构与算法
  • 系统运维
    • Windows
    • apache
    • 服务器排错
    • 网站安全
    • nginx
    • linux系统
    • MacOS
  • 学习教程
    • 前端脚本教程
    • HTML与CSS 教程
    • 脚本语言教程
    • 数据库教程
    • 应用系统教程
  • 新技术
  • 编程导航
    • 区块链
    • IT资讯
    • 设计灵感
    • 建站资源
    • 开发团队
    • 程序社区
    • 图标图库
    • 图形动效
    • IDE环境
    • 在线工具
    • 调试测试
    • Node开发
    • 游戏框架
    • CSS库
    • Jquery插件
    • Js插件
    • Web框架
    • 移动端框架
    • 模块管理
    • 开发社区
    • 在线课堂
    • 框架类库
    • 项目托管
    • 云服务

当前位置:首页>后端语言>PHP
<tfoot draggable='sEl'></tfoot>

php压缩上传的图片 php上传图片文件

很多站长朋友们都不太清楚php压缩上传的图片,今天小编就来给大家整理php压缩上传的图片,希望对各位有所帮助,具体内容如下:

本文目录一览: 1、 PHP能压缩网络上的图片吗 2、 PHP网站上传图片自动压缩,怎么编程啊,求指 3、 如何利用php把上传的图片压缩 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网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

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

// The file

$filename = 'test.jpg';

$percent = 0.5;

// Content type

header('Content-Type: image/jpeg');

// Get new dimensions

list($width, $height) = getimagesize($filename);

$new_width = $width * $percent;

$new_height = $height * $percent;

// Resample

$image_p = imagecreatetruecolor($new_width, $new_height);

$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output

imagejpeg($image_p, null, 100);

?>

关于php压缩上传的图片的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。

查看更多关于php压缩上传的图片 php上传图片文件的详细内容...

声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did254940
更新时间:2023-09-26   阅读:53次

上一篇: php文章发布系统 php发布网站

下一篇:php如何制作游戏 php游戏源码

相关资讯

最新资料更新

  • 1.hbuilder写php hbuilder写PHP
  • 2.php生成静态代码 php如何实现静态化
  • 3.phpcurl解析失败 php在html中无法解析
  • 4.phpapache占有 php apc缓存
  • 5.phprsa密钥拼接 rsa加密解密
  • 6.php配置smtp php配置文件的文件名是什么
  • 7.php如何转换json php如何转换视频
  • 8.phpsessions phpsession使用
  • 9.php数据库对象 php针对数据库的查询函数是
  • 10.php识别条码 php代码扫描
  • 11.影视php解析api php解析vip视频
  • 12.php开启sslopen php开启gd库
  • 13.php内存监控视频 视频监控内存计算
  • 14.名称占位符php 占位符html
  • 15.字符编码+php 字符编码转换器
  • 16.php实现权限分配 php权限设计
  • 17.php保存用户信息 php登录成功保存session
  • 18.php包含eaplay吗 php是否包含
  • 19.基于php在线聊天 php 在线聊天
  • 20.包含ampquotphp的词条

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://www.haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网」
[ SiteMap ]