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

php压缩png图片

发布时间:2022-04-24 03:30:39

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中对应的信息。

望采纳,谢谢。

❷ php如何实时缩小图片大小

PHP中缩放图像:

有两种改变图像大小的方法.

(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.

(2):ImageCopyResampled(),其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比
ImageCopyResized() 慢).

两个函数的参数是一样的.如下:

ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);

它们两个都是从原图像(source)中抓取特定位置(sx,sy)复制图像qu区域到目标t
图像(destination)的特定位置(dx,dy)。另外dw,dh指定复制的图像区域在目标图像上的大小,sw,sh指定从原图像复制的图像区域
的大小。如果有ps经验的话,就相当于在原图像选择一块区域,剪切移动到目的图像上,同时有拉伸或缩小的操作。

例一:

(本例子是将图片按原大小的4/1的大小显示)

<?php
// 指定文件路径和缩放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定头文件Content type值
header('Content-type: image/jpeg');
// 获取图片的宽高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//获取源文件资源句柄。接收参数为图片路径,返回句柄
$source = imagecreatefromjpeg($filename);
// 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
imageresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 输出给浏览器
imagejpeg($thumb);
?>

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

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

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

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

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

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

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

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

❹ PHP 怎么样把一张图片缩小到指定大小

如果是改变显示的大小,直接img标签属性里,width和height设置啊。
如果想真正改变,你看看这个代码(没试验过):
function makeThumb($srcFile,$dstFile,$dstW,$dstH) {
$data=GetImageSize($srcFile,&$info);
switch (CoreUtil::getFileExtension($dstFile)){
case'gif':
$im= @ImageCreateFromGIF($srcFile); break;
case'jpg':
case'jpeg':
$im= @imagecreatefromjpeg($srcFile); break;
case'png':
$im= @ImageCreateFromPNG($srcFile); break;
default:returnFalse;
}
if(!$im) returnFalse;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW){
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2); $fdstW=$dstW;
} else {
$fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$ni=ImageCreate($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$black= ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$black);
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
ImageJpeg($ni,$dstFile);
imagedestroy($im);
imagedestroy($ni);
returnTrue;
}
大概就是用到imagecreatefromjpeg、imagecreatetruecolor、imageresampled 、 imagepng这几个函数

❺ php图片可以等比例的缩放吗

可以。

等比例缩放的方法是:

1、载入选区--自由变换。如下图:

2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。

❻ PHP不改变图片尺寸,压缩png图片体积变大,怎么办

压缩文件要加头部文件,头部文件比压缩的部分大

❼ 求php图片缩放处理函数

<?php
/**
*图片缩放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//获取图片宽度
$height=$imgInfo[1];//获取图片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用缩放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//缩放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//创建画布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);

ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}

$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;

以上代码存为thumb.php,调用效果:

❽ 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

❾ 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模块实现图片缩放、裁剪、压缩示例

❿ PHP 如何缩小上传的PNG和GIF图片

PNG图片用的是imagecreatefrompng。可以处理前先判断图片的格式。部分代码如下:
$ground_info=getimagesize($groundImage);
switch($ground_info[2]){ //取得背景图片的格式
case 1:$ground_im=imagecreatefromgif($groundImage);break;
case 2:$ground_im=imagecreatefromjpeg($groundImage);break;
case 3:$ground_im=imagecreatefrompng($groundImage);break;
default:return false; //未知的文件格式
}

阅读全文

与php压缩png图片相关的资料

热点内容
小米易语言登录源码 浏览:28
砖墙内加密钢筋 浏览:992
乡关何处pdf 浏览:84
小猪领赞小程序源码 浏览:336
python曲线如何原路返回 浏览:430
pdf快速看图破解版 浏览:294
怎么找一个软件里面的源码 浏览:774
python设定安装源 浏览:833
boss直聘程序员面试方式 浏览:486
cc服务器怎么处理 浏览:455
福万通app哪里查到期 浏览:346
苹果换手机如何还原app 浏览:560
云服务器测试技巧 浏览:546
网盘里面的文件如何解压 浏览:465
linux查看应用的端口 浏览:99
拉伸训练pdf 浏览:94
如何拨号到中央服务器 浏览:650
中国天才少年程序员 浏览:352
编程思想pdf 浏览:284
加密欧美航线 浏览:50