㈠ php 如何将多张图片压缩下载到本地 ,详细一点,有案例更好!!谢谢各位了
php的压缩方式
<?php
$zip = zip_open("/tmp/test2.zip");
if ($zip) {
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "File Contents:\n";
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$buf\n";
zip_entry_close($zip_entry);
}
echo "\n";
}
zip_close($zip);
}
?>
用php下载多张图片
<?php
set_time_limit(0);//设置PHP超时时间
$aImgList = array_unique($aImgList );
foreach($aImgList as $lists) {
file_put_contents(basename($lists), file_get_contents($lists));
}
?>
㈡ php压缩图片大小到500k一下应该怎么做啊
可以用光影啊,还有就是ps都可以了
㈢ 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 图片压缩显示
(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、HTML5上传图片自动压缩问题
给你个图片处理的类吧,图片剪裁处理后,也就等于将图片压缩了。
/**
*图像处理类
*============================================================================
*Copyright2014大秦科技,并保留所有权利。
*网站地址:http://www.qintech.net;
*============================================================================
*/
classImage{
//生成缩略图的方式
public$thumbType;
//缩略图的宽度
public$thumbWidth;
//缩略图的高度
public$thumbHeight;
//生成缩略图文件名后缀
public$thumbEndFix;
//缩略图文件前缀
public$thumbPreFix;
/**
*构造函数
*/
publicfunction__construct(){
$this->thumbType=1;
$this->thumbWidth=120;
$this->thumbHeight=60;
$this->thumbPreFix='';
$this->thumbEndFix='_thumb';
}
/**
*检测是否为图像文件
*@param$img图像
*@returnbool
*/
privatefunctioncheck($img){
$type=array(".jpg",".jpeg",".png",".gif");
$imgType=strtolower(strrchr($img,'.'));
returnextension_loaded('gd')&&file_exists($img)&&in_array($imgType,$type);
}
/**
*获得缩略图的尺寸信息
*@param$imgWidth原图宽度
*@param$imgHeight原图高度
*@param$thumbWidth缩略图宽度
*@param$thumbHeight缩略图的高度
*@param$thumbType处理方式
*1固定宽度高度自增2固定高度宽度自增3固定宽度高度裁切
*4固定高度宽度裁切5缩放最大边原图不裁切
*@returnmixed
*/
privatefunctionthumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
//初始化缩略图尺寸
$w=$thumbWidth;
$h=$thumbHeight;
//初始化原图尺寸
$cuthumbWidth=$imgWidth;
$cuthumbHeight=$imgHeight;
switch($thumbType){
case1:
//固定宽度高度自增
$h=$thumbWidth/$imgWidth*$imgHeight;
break;
case2:
//固定高度宽度自增
$w=$thumbHeight/$imgHeight*$imgWidth;
break;
case3:
//固定宽度高度裁切
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
break;
case4:
//固定高度宽度裁切
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
break;
case5:
//缩放最大边原图不裁切
if(($imgWidth/$thumbWidth)>($imgHeight/$thumbHeight)){
$h=$thumbWidth/$imgWidth*$imgHeight;
}elseif(($imgWidth/$thumbWidth)<($imgHeight/$thumbHeight)){
$w=$thumbHeight/$imgHeight*$imgWidth;
}else{
$w=$thumbWidth;
$h=$thumbHeight;
}
break;
default:
//缩略图尺寸不变,自动裁切图片
if(($imgHeight/$thumbHeight)<($imgWidth/$thumbWidth)){
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
}elseif(($imgHeight/$thumbHeight)>($imgWidth/$thumbWidth)){
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
}
//}
}
$arr[0]=$w;
$arr[1]=$h;
$arr[2]=$cuthumbWidth;
$arr[3]=$cuthumbHeight;
return$arr;
}
/**
*图片裁切处理
*@param$img原图
*@paramstring$outFile另存文件名
*@paramstring$thumbWidth缩略图宽度
*@paramstring$thumbHeight缩略图高度
*@paramstring$thumbType裁切图片的方式
*1固定宽度高度自增2固定高度宽度自增3固定宽度高度裁切
*4固定高度宽度裁切5缩放最大边原图不裁切6缩略图尺寸不变,自动裁切最大边
*@returnbool|string
*/
publicfunctionthumb($img,$outFile='',$thumbWidth='',$thumbHeight='',$thumbType=''){
if(!$this->check($img)){
returnfalse;
}
//基础配置
$thumbType=$thumbType?$thumbType:$this->thumbType;
$thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
$thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
//获得图像信息
$imgInfo=getimagesize($img);
$imgWidth=$imgInfo[0];
$imgHeight=$imgInfo[1];
$imgType=image_type_to_extension($imgInfo[2]);
//获得相关尺寸
$thumb_size=$this->thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
//原始图像资源
$func="imagecreatefrom".substr($imgType,1);
$resImg=$func($img);
//缩略图的资源
if($imgType=='.gif'){
$res_thumb=imagecreate($thumb_size[0],$thumb_size[1]);
$color=imagecolorallocate($res_thumb,255,0,0);
}else{
$res_thumb=imagecreatetruecolor($thumb_size[0],$thumb_size[1]);
imagealphablending($res_thumb,false);//关闭混色
imagesavealpha($res_thumb,true);//储存透明通道
}
//绘制缩略图X
if(function_exists("imageresampled")){
imageresampled($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}else{
imageresized($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}
//处理透明色
if($imgType=='.gif'){
imagecolortransparent($res_thumb,$color);
}
//配置输出文件名
$imgInfo=pathinfo($img);
$outFile=$outFile?$outFile:dirname($img).'/'.$this->thumbPreFix.$imgInfo['filename'].$this->thumbEndFix.".".$imgInfo['extension'];
Files::create(dirname($outFile));
$func="image".substr($imgType,1);
$func($res_thumb,$outFile);
if(isset($resImg))
imagedestroy($resImg);
if(isset($res_thumb))
imagedestroy($res_thumb);
return$outFile;
}
}
㈥ 如何利用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
㈦ thinkphp如何做图片压缩呢
在上传图片的时候先看看图片有多大,一般来说导航幻灯片的图片单张大小尽量不超100k,产品图不超过20k,这样加载还慢的话就用ajax后加载方法,可以是滚动加载之类,但是对蜘蛛抓取页面并不是很友好。
至于你说的用tp把图片压缩,那只能是将图片的尺寸改成你想要的尺寸,大小的话是web所用格式大小,等页面加载完你又换原图,这样相当于又加载了一遍,还不如做ajax滚动加载。
㈧ PHP怎么对GIF动图进行压缩和上传
单帧 gif 可以转换成 jpg/png/webp 等其他图片格式来节约体积。
多帧 gif 可以使用 gifsicle 压缩,也可以转换成 apng/webp 来节约体积,国外比较流行的一种做法是把 gif 转换成 mp4视频。
用哪种方法取决于你的用户端,压缩的效果可以通过 compression ratio/ssim/psnr 等来衡量,寻找一个合适的压缩参数。
如果用到的库没有 PHP binding,那么在条件允许的情况下可以通过 popen() 和标准流来集成。