一、图片压缩
/**
* 图片压缩 只针对文件后缀名为: 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 和 配置com访问权限
/**
* 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;
}