Place an image in PHP excel With an external path

1

I have the following code to insert an excel image, which works correctly:

$objDrawing = new PHPExcel_Worksheet_Drawing();
                    $objDrawing->setName('imgNotice');
                    $objDrawing->setDescription('Noticia');
                    $img = '../images/new.jpg'; // Provide path to your logo file
                    $objDrawing->setPath($img);
                    $objDrawing->setOffsetX(28);    // setOffsetX works properly
                    $objDrawing->setOffsetY(300);  //setOffsetY has no effect
                    $objDrawing->setCoordinates('A1');
                    $objDrawing->setHeight(150); // logo height
                    $objDrawing->setWorksheet($objPHPExcel->setActiveSheetIndex($pos+1)); 

However I have a database that has a table with some images whose routes are from the internet, for example http://cdn.sstatic.net/Sites/beta/img/404.png but for phpExcel it does not work, does anyone know if there is any way? or do I necessarily need the image in the project?

And yes, I already tried:

$img = "http://cdn.sstatic.net/Sites/beta/img/404.png";
    
asked by Andress Blend 01.03.2017 в 17:46
source

2 answers

1

It is not possible. The way you can do it is to get the file with get_file_contents (or similar), save it in your local filesystem, take the image from there for your excel and since it is in the excel and you do not use it delete the image of your local filesystem .

    
answered by 01.03.2017 / 18:12
source
0

This is the code that I use. make sure that mdl does not have php closure

<?php 
session_start();
require_once '../librerias/Classes/PHPExcel.php';
include '../librerias/Classes/PHPExcel/IOFactory.php';
include '../model/mdl_usuario.php';
set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize'  => '15MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

$objPHPExcel = new PHPExcel();
  $mdl_usuario = new Usuarios(); 
 $mdl_usuario->set("usu_id", $_SESSION['usu_id']);
 $datos=$mdl_usuario->listarById();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("../documentos/fase1/chequeotsa.xlsx");

$objPHPExcel->setActiveSheetIndex(0);

$gdImage = imagecreatefromjpeg($datos[13]);
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(0.44);
$objDrawing->setwidth(186);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());


header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Lista de Chequeo para TSA.xlsx"');
header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit; 
    
answered by 12.12.2017 в 17:35