1

I need to create an excel document with only 2 col. The first one will contain multiple images 150px large and the seconde one will contain a web code. For some reason only one image is added and the file appears to be currupted. Not sure what i am doing wrong ...

<?php
include("../../init.php");

if (is_numeric($_GET[groupe])){

  define( 'PCLZIP_TEMPORARY_DIR', $_CONFIG['upload']['root'].'/cache' );

  // filename for download
  $groupe = mysql_query("SELECT * FROM photographe_groupe WHERE id='$_GET[groupe]'");  
  $records = "Groupe - $groupe[nom].xlsx";

  header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition:inline;filename='.$records);

  $workbook = new PHPExcel;
  $sheet = $workbook->getActiveSheet();

  $i=0;
  $select = mysql_query("SELECT * FROM photographe_images WHERE sid='$_GET[groupe]' group by `paire`");
  while($photo = mysql_fetch_array($select)){
    // Table header
    if ($i=="1"){
        $sheet->setCellValue("A1",'Photo(s)');
        $sheet->setCellValue('B1','Code Web');
        $i++;
    }

    // Set images in col 1
    $select1 = mysql_query("SELECT * FROM photographe_images WHERE paire='$photo[paire]'");
    while($photo1 = mysql_fetch_array($select1)){

        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setName($photo1[img]);
        $objDrawing->setDescription($photo1[img]);
        $objDrawing->setWorksheet($workbook->getActiveSheet());
        $objDrawing->setPath($_CONFIG['upload']['root'].'/userfiles/photos/'.$photo1[img]);
        $objDrawing->setWidth(150);
        $objDrawing->setCoordinates('A'.$i);

    }

    // Set web code in col 2
    $sheet->setCellValue("B$i",$photo[code_web]);
    $i++;
  }

}
$workbook->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$writer = new PHPExcel_Writer_Excel2007($workbook);
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$writer->save('php://output');
?>

You can download and output demo here

pnuts
  • 58,317
  • 11
  • 87
  • 139
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38

1 Answers1

0

Just a few points to note before I try taking a look at your file:

The first point to note here is that Excel doesn't store images in cells, it "overlays" images, and doesn't particularly care where cell boundaries occur, so an image can be a fixed size irrespective of how many cells it overlays

When you specify a coordinate for a image, you're relating the top-left corner of that image to be placed at the top-left corner of that cell.... where the bottom and/or right corner of the image lies isn't determined by the cells at all. Setting the row height or column width to autosize will not be affected by the image in any way, because the image is overlaid, not a content of the cell

You can, offset the image from the top-left corner of the related cell by using the Drawing object's setOffsetX() and setOffsetY() methods.

You can link more than one image to be placed relative to the same cell's top-left corner using different offset values so they don't overlap, but you need to work out the offsets from the size of the images

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I see ... sounds very complicated ... So what i need to do is get the image height and width, then apply the size to the cell so the image is inside then calculate the setOffsetX and setOffsetY for the next one ... There has to be a better way to add images no? ... I am not sure how to implement such a strategy in my code ... – Patrick Simard Jun 04 '15 at 15:52
  • well you're already forcing the image width with a call to `setWidth(150);` You can do similar with the image height – Mark Baker Jun 04 '15 at 15:55
  • The height is not specifyed or it will distort the image. But i can still probably retreive the new height and apply that to the cell some how ... – Patrick Simard Jun 04 '15 at 16:13
  • You should be able to use the image dimensions to calculate the height proportionately – Mark Baker Jun 04 '15 at 16:14
  • In any case, thats not solving my currupted file problem ... why does it load only one image instead of overlaping them? – Patrick Simard Jun 04 '15 at 16:14
  • /Ok so i got past the currupted data and found a way to align the images now i need to retreive the new height but i am not sure how to get that out of the setWidth(150)... – Patrick Simard Jun 04 '15 at 16:41