phpExcel圖片導出而且調用gd庫壓縮圖片

 public function water_log_export(){

    ini_set(memory_limit,'1024M');

      //  $data = $m->field('eq_id,img,create_time,loss,last_loss,ps_id')->select();

        $data=M()
            ->table(array('zgpt_instrument'=>'in','zgpt_equipment'=>'eq'))
            ->where('in.eq_id=eq.id and eq.type=1')
            ->field('eq_id,img,create_time,loss,last_loss,ps_id')
            ->select();


        // 導出Exl
        import("ORG.Util.PHPExcel");
        import("ORG.Util.PHPExcel.Reader.Excel2007");
        import("ORG.Util.PHPExcel.IOFactory");
        import("ORG.Util.PHPExcel.Worksheet.Drawing");

        $objPHPExcel = new \PHPExcel();

        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

        $objActSheet = $objPHPExcel->getActiveSheet();

        //合併單元格
        $objPHPExcel->getActiveSheet()->mergeCells('A1:U1');      //合併

        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        //加邊框
        $objPHPExcel->getActiveSheet()->getStyle('A2:U2')->applyFromArray(
            array(
                'borders' => array(
                    'top' => array(
                        'style' => \PHPExcel_Style_Border::BORDER_THIN
                    )
                )
            )
        );

        $objActSheet->setCellValue('A1', '園區總水錶抄表統計');

        //表頭數組
        $tableheader = array('水錶種類','照片','時間','抄表見數(噸)','用水量(噸)','抄表人','備註','水錶種類','照片','時間','抄表見數(噸)','用水量(噸)','抄表人','備註','水錶種類','照片','時間','抄表見數(噸)','用水量(噸)','抄表人','備註');
        //填充表頭信息
        $j = 0;
        for($i = A;$i <= U;++$i) {
            $objPHPExcel->getActiveSheet()->setCellValue($i.'2',$tableheader[$j]);
            $objPHPExcel->getActiveSheet()->getColumnDimension($i)->setWidth(20);

            $objPHPExcel->getActiveSheet(0)->getStyle($i.'2')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
            $objPHPExcel->getActiveSheet(0)->getStyle($i.'2')->getFill()->getStartColor()->setARGB('ff00b050');

            $j++;
        }


        $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getFont()->setSize(16);


        /*for($i = 0;$i <1000;$i++){
            $data[] = $data[0];
        }*/
        $cols = 7; //字段數量
        foreach ($data as $k => $v) {

            for($i = 0;$i < $cols;$i++){
                $col = strtoupper(chr(65+$i+$cols*($k%3)));
                $row = ceil(($k+1)/3)+2;
                $eq=M('equipment')->where(array('id'=>$v['eq_id']))->find();
                $last_loss=$v['loss']-$v['last_loss'];
                $person=M('person')->where(array('ps_id'=>$v['ps_id']))->find();
                $val=array(
                    $eq['name'],
                    //$v['img'],
                    '',
                    date('Y-m-d H:i',$v['create_time']),
                    $v['loss'],
                    $last_loss,
                    $person['ps_name'],
                    ''
                );
                if($col==B || $col==I || $col==P){
                    // 圖片生成
                  //  $objDrawing = new \PHPExcel_Worksheet_Drawing();

                  //  $objDrawing->setPath($_SERVER['DOCUMENT_ROOT'].$v['img']);

                    // 設置寬度高度
                //    $objDrawing->setWidth(140); //照片寬度

                    // 圖片偏移距離
                 //   $objDrawing->setOffsetX(0);

                 //   $objDrawing->setOffsetY(0);

                  //  $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

                 //   $objDrawing->setResizeProportional(false);

                    //設置圖片要插入的單元格
                   // $objDrawing->setCoordinates(strtoupper(chr(65+$i+$cols*($k%3))).$row);  //照片



                    $objDrawing = new \PHPExcel_Worksheet_MemoryDrawing ();
                    /* 設置圖片路徑 切記:只能是本地圖片 */
                    $img = $_SERVER['DOCUMENT_ROOT'].$v['img'];
                  //  $newim = imagecreate (200, 116);
                    $newim= imagecreatetruecolor(300,174);    //建立真彩圖像資源
                    $img = imagecreatefromjpeg($img);
                    $width = imagesx($img);
                    $height = imagesy($img);
                    imagecopyresized($newim,$img,0,0,0,0,300,174,$width,$height);

                    // imagejpeg($dst_im); //輸出壓縮後的圖片
                    $objDrawing->setName('erweima');

                    $objDrawing->setDescription('erweima');

                    $objDrawing->setImageResource($newim);
                    $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);        ////渲染方法
                    $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                    /* 設置圖片高度 */
                    $objDrawing->setWidth(140); //照片寬度
                  //  $objDrawing->setHeight ( 100 );
                    /* 設置圖片要插入的單元格位置 */
                    $objDrawing->setCoordinates(strtoupper(chr(65+$i+$cols*($k%3))).$row);
                    // 寫入圖片在指定格中的X座標值
                    $objDrawing->setOffsetX ( 0);
                    // 寫入圖片在指定格中的Y座標值
                    $objDrawing->setOffsetY ( 0);

                    $objDrawing->setWorksheet ( $objPHPExcel->getActiveSheet () );


                }


              //  unset($objDrawing);
                // 表格高度
                $objActSheet->getRowDimension($k)->setRowHeight(60);

                $objPHPExcel->getActiveSheet()->setCellValueExplicit($col.$row,"$val[$i]",\PHPExcel_Cell_DataType::TYPE_STRING);
            }
           // break;
        }


        // 設置第一行和第二行表格高度
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(30);


        $fileName = '園區水錶抄表統計.xls';

       // $date = date("Y-m-d",time());

       // $fileName .= "_{$date}.xls";

        $fileName = iconv("utf-8", "gb2312", $fileName);

        //設置活動單指數到第一個表,因此Excel打開這是第一個表

        $objPHPExcel->setActiveSheetIndex(0);

        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment;filename=\"$fileName\"");

        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output'); //文件經過瀏覽器下載

        // END


    }
相關文章
相關標籤/搜索