Insert image from Url

Topics: Developer Forum
Jul 23, 2013 at 11:16 AM
Hello,

I am trying to create an excel file containing some reports and i have a column called "Image Thumbs" for our picture check-ins. I need to add those images in each cell in order to be anchored and be fixed on cell when applying some filters or sorting etc.

The main problem is that for PHPExcel_Worksheet_Drawing i got an error when trying to setPath for my images.

Is there a way to add images on excel from url ? Our images are on amazon s3 bucket.
Coordinator
Jul 23, 2013 at 11:50 AM
You need a filesystem file, not a URL.
Jul 23, 2013 at 1:34 PM
This cannot be done via URL ? Only from filesystem images ?

Eitherway, is there a way in PHPExcel to anchor a photo to cell ? If i apply some filters and sortings on table, also images to move with rows.
Coordinator
Jul 23, 2013 at 3:37 PM
Images in Excel are overlaid on the worksheet: either linked over a single (top-left) cell, with a height and width; or to two cells (top-left and bottom-right). In most cases with PHPExcel, we use the single anchor method
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');       // filesystem reference for the image file
$objDrawing->setHeight(36);                 // sets the image height to 36px (overriding the actual image height); 
$objDrawing->setCoordinates('D24');    // pins the top-left corner of the image to cell D24
$objDrawing->setOffsetX(10);                // pins the top left corner of the image at an offset of 10 points horizontally to the right of the top-left corner of the cell
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
Jul 24, 2013 at 8:25 AM
MarkBaker,

Thanks a lot for your response. This work well and it fits to cell after i set rowHeight and ColumnWidth dimensions.

One small problem i have with images that are too long, let's say 50w and 500h or 50h and 500w. It works if i set height at 50 and on images that have a long width it still get out of cell borders. It won't work if i add setHeight and setWidth both togheter.

Kind Regards,

Andrei Kun
Oct 2, 2013 at 9:30 PM
Hi,

Could you help me?
I want to use a path which is in a variable $image, the problem is that it is not working, it works just when I type the path manually.
this is my script, I hope one of you can help me.
$i=1
while($fields = mysql_fetch_array($query)){
    $image = $fields['bar_code'];
    $objPHPExcel->getActiveSheet()->getRowDimension($i+1)->setRowHeight(52);
    $objDrawing = new PHPExcel_Worksheet_Drawing();
    $objDrawing->setName('Name');
    $objDrawing->setDescription('Summary');
    $objDrawing->setPath("../../img/".$img."_ch.png");
    $objDrawing->setHeight(70);
    $objDrawing->setCoordinates('B'.strval($i+1));
    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
    $i++;
}
Oct 3, 2013 at 11:23 AM
Do you have a 'File xxxx not found!' exception when you use a variable?
If Yes, check the path, use realpath() to solve the '..' and recheck.
Oct 4, 2013 at 11:35 PM
Hi, thanks,
The issue was that there were some files wich didn´t exists and what I did was to use if(file_exist()) as follows:
    $i=1;
    if(file_exists($imagen[$i])){
        $objPHPExcel->getActiveSheet()->getRowDimension($i+1)->setRowHeight(52);
        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setName('Nombre');
        $objDrawing->setDescription('Reporte de Pedidos');
        $objDrawing->setPath($imagen[$i]);
        $objDrawing->setHeight(70);
        $objDrawing->setCoordinates('B'.strval($i+1));
        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
        $i++;
    }