导航:首页 > 文件处理 > php图片质量压缩

php图片质量压缩

发布时间:2022-05-01 06:56:02

A. 怎么用php压缩GIF图

缩图小软件ImageResizerPowertoys
微软出品的免费缩图小软件,它不但能够快速、批量地缩小图片,而且操作超级简单。安装结束后,它会在你的鼠标右键菜单中嵌入一个"ResizePicture"项。在图片文件上点右键选这个"ResizePicture"即可设置你希望缩小的图片尺寸。

JPEG Imager 2.1.2.25 汉化版
JPEG Imager 能将 BMP、JPG、PNG、GIF 等格式的图形文件进行压缩,使文件变得更小,可自设压缩比例、大小、明暗度等等,它采用了一种新压缩算法:“智能过滤(smart filtration)”不仅可以改善图像的观感质量,而且还可为输出的图片“减肥”,允许压缩后的图形文件不失真。还可以建立类似于渐变 GIF 效果的渐变式 JPEG 图像,这种形式的 JPEG 图像应用于网页制作可使网页读取的速度加快。它的特点还包括对图像进行批量处理高效建立缩略图以及利用自带的滤镜、图像编辑器对图形进行简单的处理等。

B. php图片上传后被压缩了,变得不清晰了,怎么解决

如果清晰的上传后,不清晰,,那就是设置的压缩质量问题,,,如果是不清晰上传了,这肯定不会清晰了。。。

找到上传的设置里可以找到图片压缩比例。。。。

C. PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

三个文件代码如下:
连接数据库:connect.php

<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",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="insertintoimg(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");

//获取文件后缀名函数
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}

//生成随机文件名函数
functionrandom($length)
{
$hash='CR-';
$chars='';
$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'];

functionResizeImage($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("imageresampled"))
{
$uploaddir_resize=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($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中对应的信息。

望采纳,谢谢。

D. php 图片压缩显示

(1)网页结构里用:<img src="image.php?name=p01.png">,来调用处理后的图片信息。
(2)在后台脚本 image.php 里对传过来的图片名进行处理返回:
<?php
$pic = $_REQUEST['name'];
// 1.打开图片源文件资源
$im = @imagecreatefrompng($pic);
if ($im) {
// 2.源文件的宽高,也可写为定值
$fx = imagesx($im); // 取宽
$fy = imagesy($im); // 取高
// 3.使用新的宽高
$sx = 150;
$sy = 100;
// 4.生成小图资源
$sm = imagecreatetruecolor($sx,$sy);
// 5.进行缩放
imageresampled($sm,$im,0,0,0,0,$sx,$sy,$fx,$fy);
// 6.输出图像
header('Content-Type: image/png');
imagepng($sm);
// 7.释放资源
imagedestroy($sm);
imagedestroy($im);
}

(3)代码里假设是对 png 图片处理,相关字都是 png,如果想对 jpg 类型处理的可都换成 jpeg

E. php图片上传能用代码压缩图片文件的大小吗

图片的格式是多变的,但是压缩图片的方式不变,压缩软件压缩图片一致都是那样,我将操作步骤写下来了,楼主可以看看
1、安装相对应的辅助工具(迅捷图片压缩软件)运行工具;

2、打开工具,看到页面上的图片压缩选项,点击这个蓝色的按钮进入将要实行操作的页面。

3、在页面上点击选择文件按钮,或是选择文件夹按钮,都可以将存放图片文件的文件夹打开,然后对图片进行选择。

4、选择文件时我们按住多选键Ctrl,选择我们需要压缩的图片添加到页面中间的位置。

5、做到这一步了,下面我们可以对压缩图片的压缩选项做一个选择,可以转换图片的格式,转化为png或者是jpg,将图片压缩可以选择的压缩选项如下。

6、将所有的参数设置完成之后我们点击页面上的“开始压缩按钮就可以进行压缩了。

日常使用的压缩图片的办法是将图片压缩为压缩包,在使用是还要对其解压才能使用,这种压缩方法压缩图片不同点在于不会将图片文件压缩为压缩包,能将图片最大限度的缩小,图片的状态不会改变。

F. 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 = '';$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("imageresampled")){
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);imageresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);imageresized($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中对应的信息

G. PHP等比例压缩图片的实例代码

具体代码如下所示:
/**
*
desription
压缩图片
*
@param
sting
$imgsrc
图片路径
*
@param
string
$imgdst
压缩后保存路径
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//压缩后的图片宽
$new_height
=
$height;//压缩后的图片高
if($width
>=
600){
$per
=
600
/
$width;//计算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是质量、压缩图片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
总结
以上所述是小编给大家介绍的PHP等比例压缩图片的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:php中10个不同等级压缩优化图片操作示例PHP
实现等比压缩图片尺寸和大小实例代码php
gd等比例缩放压缩图片函数基于PHP实现等比压缩图片大小php上传图片并压缩的实现方法PHP实现图片上传并压缩PHP实现图片压缩的两则实例php使用imagick模块实现图片缩放、裁剪、压缩示例

H. php上传图片压缩

html里加一个表单,然后提交到处理上传文件的upload.php

类似下面这样

<fromaction="upload.php"enctype="multipart/from-data"method="post">
<inputtype="file"name="file3"/>
</from>

然后upload.php里先处理图片上传,最终保存一份原始图片文件在服务器上,接着就把文件路径什么的传到你的这个函数里处理图片就OK了

I. 如何利用php把上传的图片压缩

<?php
//Thefile
$filename='test.jpg';
$percent=0.5;

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

//Getnewdimensions
list($width,$height)=getimagesize($filename);
$new_width=$width*$percent;
$new_height=$height*$percent;

//Resample
$image_p=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromjpeg($filename);
imageresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);

//Output
imagejpeg($image_p,null,100);
?>

http://php.net/manual/en/function.imageresampled.php

J. php 怎么压缩图片的大小

好办的,你把网站下载到本地,然后 用这个批量压缩图片的软件就可以了

阅读全文

与php图片质量压缩相关的资料

热点内容
卡尔曼滤波算法书籍 浏览:769
安卓手机怎么用爱思助手传文件进苹果手机上 浏览:844
安卓怎么下载60秒生存 浏览:803
外向式文件夹 浏览:240
dospdf 浏览:431
怎么修改腾讯云服务器ip 浏览:392
pdftoeps 浏览:496
为什么鸿蒙那么像安卓 浏览:736
安卓手机怎么拍自媒体视频 浏览:186
单片机各个中断的初始化 浏览:724
python怎么集合元素 浏览:481
python逐条解读 浏览:833
基于单片机的湿度控制 浏览:499
ios如何使用安卓的帐号 浏览:883
程序员公园采访 浏览:812
程序员实战教程要多长时间 浏览:979
企业数据加密技巧 浏览:135
租云服务器开发 浏览:814
程序员告白妈妈不同意 浏览:337
攻城掠地怎么查看服务器 浏览:601