当前位置:首页 > 代码 > 正文

php在线压缩代码(php 解压缩)

admin 发布:2022-12-19 13:20 87


本篇文章给大家谈谈php在线压缩代码,以及php 解压缩对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

php图片上传到服务器指定路径,并且图片压缩成70*95大小代码!

?

$path='img/';//路径

$phtypes=array(

'img/gif',

'img/jpg',

'img/jpeg',

'img/bmp',

'img/pjpeg',

'img/x-png'

);

?

htmlbody

form method="post" enctype="multipart/form-data" name="form1"

table

trtd上传图片/td/tr

trtdinput type="file" name="photo" size="20" //td/tr

trtdinput type="submit" value="上传"//td/tr

/table

允许上传的文件类型为:?=implode(', ',$phtypes)?/form

?php

if($_SERVER['REQUEST_METHOD']=='POST'){

if (!is_uploaded_file($_FILES["photo"][tmp_name])){

echo "图片不存在";

exit();

}

if(!is_dir('img')){//路径若不存在则创建

mkdir('img');

}

$upfile=$_FILES["photo"];

$pinfo=pathinfo($upfile["name"]);

$name=$pinfo['basename'];//文件名

$tmp_name=$upfile["tmp_name"];

$file_type=$pinfo['extension'];//获得文件类型

$showphpath=$path.$name;

if(in_array($upfile["type"],$phtypes)){

echo "文件类型不符!";

exit();

}

if(move_uploaded_file($tmp_name,$path.$name)){

echo "成功!";

}

echo "img src=\"".$showphpath."\" hight=\"70\" width=\"95\" /";

}

?

/body

/html

如何用php压缩html代码并输出

function compressHtml($string) {

$matches = array();

preg_match_all('/((?:pre|code).+?\/(?:pre|code))+/is', $string, $matches);

foreach ((array)$matches[1] as $k = $v) {

$string = str_replace($v, "#{$k}#", $string);

}

$string = str_replace("\r\n", '', $string);

//清除换行符

$string = str_replace("\n", '', $string);

//清除换行符

$string = str_replace("\t", '', $string);

//清除制表符

$pattern = array ("/ *([^ ]*) */", "/[\s]+/", "/!--[^!]*--/", "/\" /", "/ \"/", "'/\*[^*]*\*/'");

$replace = array ("\\1", " ", "", "\"", "\"", "");

$string = preg_replace($pattern, $replace, $string);

foreach ((array)$matches[1] as $k = $v) {

$string = str_replace("#{$k}#", $v, $string);

}

return $string;

}

思路就是提前替换出 pre, code 的块内容.

在处理完之后, 再替换回来.

ps . 其实那个压缩函数没多大用途. nginx 直接开 gzip 压缩就好了. 效率和性能比用php做要高.

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);

imagecopyresampled($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);

imagecopyresampled($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);

imagecopyresampled($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 压缩文件夹

php将文件夹打包成zip文件,参考代码如下:

function addFileToZip($path,$zip){

    $handler=opendir($path); //打开当前文件夹由$path指定。

    while(($filename=readdir($handler))!==false){

        if($filename != "."  $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作

            if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归

                addFileToZip($path."/".$filename, $zip);

            }else{ //将文件加入zip对象

                $zip-addFile($path."/".$filename);

            }

        }

    }

    @closedir($path);

}

$zip=new ZipArchive();

if($zip-open('images.zip', ZipArchive::OVERWRITE)=== TRUE){

    addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法

    $zip-close(); //关闭处理的zip文件

}

php在线压缩代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php 解压缩、php在线压缩代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/8716.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载