博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fuelphp学习和知识积累
阅读量:6913 次
发布时间:2019-06-27

本文共 7077 字,大约阅读时间需要 23 分钟。

hot3.png

一、图片压缩

/**     * 图片压缩  只针对文件后缀名为: jpg,png,jpeg     * @param $file 文件路径     * @param $tmp 为文件压缩条件,即当图片的长宽的长度最大的那个,大于$tmp值时进行压缩     * 压缩规则:取图片的长宽最大的那个,再设置为 $tmp 再等比压缩其他     *     */    public static function requestImageResize($file,$tmp = 1536){        \Fuel\Core\Log::debug("======================================== Image Resize Begin =====================================");        \Fuel\Core\Log::debug("filePath = $file ");//        $tmp = 1536;        $strs = explode('.',$file);        $index = count($strs) - 1;        $str = strtolower($strs[$index]);        if( $str == "jpg" || $str == "png"|| $str == "jpeg" ){            list($width, $height) = getimagesize($file);            \Fuel\Core\Log::debug("width=$width   height=$height");            $newwidth = $width;            $newheight = $height;            $max = $newheight >= $newwidth ? $newheight:$newwidth;            $flag = $newheight >= $newwidth ? 1:2;            if($max>$tmp){                \Fuel\Core\Log::debug("resize");                if( $flag == 2 ){                    $offset=$width/$tmp;                    $newwidth=$tmp;                    $newheight=$height/$offset;                }else{                    $offset=$height/$tmp;                    $newheight=$tmp;                    $newwidth=$width/$offset;                }                $src_im = imagecreatefromjpeg($file);                $dst_im = imagecreatetruecolor($newwidth, $newheight);                imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);                imagejpeg($dst_im,$file); //输出压缩后的图片                imagedestroy($dst_im);                imagedestroy($src_im);            }else{                \Fuel\Core\Log::debug("no resize");            }        }        \Fuel\Core\Log::debug("======================================== Image Resize End  =====================================");    }

二、视频压缩

这里需要安装 Mplayer

/** ** @param $in  源文件路径 * @param $out_vid  目的文件路径 * @return mixed  目的文件路径 * MPlayer-mingw32-1.0rc1     ffmpeg */public static  function flv_convert($in, $out_vid){    \Fuel\Core\Log::debug(date("Y-m-j H:i:s")."  ===================  flv_convert  $in  to $out_vid  begin");    $cmd = 'mencoder '.$in.' -o '.$out_vid.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame';    $res = shell_exec($cmd);    \Fuel\Core\Log::debug(date("Y-m-j H:i:s")."  ===================  flv_convert  $in  to $out_vid  finish");    return $out_vid;}

三、生成 excel 文件

其中文件保存路径$fileName,我这里使用的是相对路径

/** * @param array $head           头部(如果要输出序号就要在这里的第一个添加需要标识) * @param array $data           数据 * @param bool $data_id_flag    标识是否需要输出序号,如果是则在每一行前面输出序号 * @return string               输出文件保存位置 * @throws PHPExcel_Exception * @throws PHPExcel_Writer_Exception */public static function doExportExcel($head = array(),$data = array(),$data_id_flag = true ){     $objPHPExcel = new PHPExcel();     //表头     $start = 'A';//初始格代表A     $col = 1;//初始格     foreach($head as $key=>$row){         $cell = $start.$col;         $objPHPExcel->setActiveSheetIndex(0)             ->setCellValue($cell, $row);         $objPHPExcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);         $objPHPExcel->getActiveSheet()->getStyle($cell)->getAlignment()->setWrapText(true);         $objPHPExcel->getActiveSheet()->getColumnDimension($start)->setAutoSize(true);         #设置字体         $objFontA5 = $objPHPExcel->getActiveSheet()->getStyle($cell)->getFont();         $objFontA5->setName('Microsoft Yahei');         $objFontA5->setSize(11);         $objFontA5->setBold(true);         $objFontA5->getColor()->setARGB('FF333333');         $start = ++$start;     }     //数据     $start = 'A';//初始格代表A     $col = 2;//初始格     $j = 1;     foreach($data as $rows){         if($data_id_flag){             $cell = $start.$col;             $objPHPExcel->setActiveSheetIndex(0)                 ->setCellValueExplicit($cell,$j++ , PHPExcel_Cell_DataType::TYPE_STRING);             $start = ++$start;         }         foreach($rows as $key=>$val){             $cell = $start.$col;             $objPHPExcel->setActiveSheetIndex(0)                 ->setCellValueExplicit($cell, $val, PHPExcel_Cell_DataType::TYPE_STRING);             $start = ++$start;         }         $col++;         $start = 'A';     }     // Rename sheet     $objPHPExcel->getActiveSheet()->setTitle('DataSheet');     // Set active sheet index to the first sheet, so Excel opens this as the first sheet     $objPHPExcel->setActiveSheetIndex(0);     $savefilename = "data ".date('Ymd His').".xls";     //文件保存路径,这里可做修改     $fileName = 'file/'.$savefilename;     header('Content-Type: application/vnd.ms-excel');     header('Content-Disposition: attachment;filename="'.$savefilename.'"');     header('Cache-Control: max-age=0');     $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);     $objWriter->save($fileName);     return $fileName; }

四、pdf转图片

这里需要在安装 ImageMagick 和得到相应的 dll  然后再修改配置 php.ini 添加 extension=php_imagick.dll;

注意:要安装对版本

/** * @param $PDF 绝对路径 * @param $Path 绝对路径 * @return array * 安装 ImageMagick-6.8.6-Q16 和 相应的 dll */public static function pdf2png($PDF,$Path){    if(!extension_loaded('imagick')){        echo "imagick extension_loaded fail";        return false;    }    if(!file_exists($PDF)){        echo $PDF;        return false;    }    $IM = new Imagick();    $IM->setResolution(120,120);    $IM->setCompressionQuality(100);    $IM->readImage($PDF);    foreach($IM as $Key => $Var){        $Var->setImageFormat('png');        $Filename = $Path.'/'.md5($Key.time()).'.png';        if($Var->writeImage($Filename)==true){            $Return[]= $Filename;        }    }    return $Return;}

五、word转pdf

需要安装  openoffice 和 

/** * word 文档转pdf * @param $doc_url           源文档的全路径 * @param string $output_url 索要保存的路径包括文件名 * 需要安装  openoffice 和 配置其访问权限 * 博客 http://www.firerise.com.cn/article_ar71.html */public static function word2pdf($doc_url,$output_url="file:///Q:/bsl/wujixian/shengchang/qiye/8/infiniteus-sharefun/web/public/file"){    $output_url = $output_url."/1.pdf";    echo $output_url;    $osm = new COM("com.sun.star.ServiceManager")or die ("Please be sure that OpenOffice.org is installed.n");    echo "sss";    $args = array(Service_Util::MakePropertyValue("Hidden",true,$osm));    $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");    $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);    $export_args = array(Service_Util::MakePropertyValue("FilterName","writer_pdf_Export",$osm));    $oWriterDoc->storeToURL($output_url,$export_args);    $oWriterDoc->close(true);}/** * 辅助 word2pdf 方法完成功能 * @param $name * @param $value * @param $osm * @return mixed */static function MakePropertyValue($name,$value,$osm){    $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");    $oStruct->Name = $name;    $oStruct->Value = $value;    return $oStruct;}

转载于:https://my.oschina.net/swchenyuzhe/blog/516203

你可能感兴趣的文章
Oracle 11g数据库随系统自动启动与关闭的设置方法
查看>>
git pull force
查看>>
使用new操作符来调用一个构造函数的时候发生了什么
查看>>
交换机配置vlan 访问控制列表
查看>>
Python面向对象之类的成员
查看>>
Win8上iis配置
查看>>
Confluence 6 配置 Office 转换器
查看>>
Grin交易原理详解
查看>>
大数据体系【概念认知】系列-2:存储以及副本策略
查看>>
Apache与Tomcat区别联系
查看>>
用shell编写批量打包日志脚本
查看>>
传统的MapReduce框架慢在那里
查看>>
20个java异常处理最佳实践
查看>>
001作业题
查看>>
关于实习
查看>>
叠加等边三角形
查看>>
网页换肤
查看>>
[BZOJ3751/NOIP2014]解方程
查看>>
silverlight多国语言研究
查看>>
赋值法
查看>>