Skip to content

Commit

Permalink
addImage from base64 string
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Dec 21, 2024
1 parent 83fcd70 commit 96078b4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,18 @@ For more information on using rich text, see here: [Using Rich Text](/docs/03-wr

### Adding Images

You can insert image to sheet from local file, URL or image string in base64

```php
// Insert an image to the cell A1
// Insert an image to the cell A1 from local path
$sheet1->addImage('A1', 'path/to/file');

// Insert an image to the cell A1 from URL
$sheet1->addImage('A1', 'https://site.com/image.jpg');

// Insert an image to the cell A1 from base64 string
$sheet1->addImage('A1', 'data:image/jpeg;base64,/9j/4AAQ...');

// Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally)
$sheet1->addImage('B2', 'path/to/file', ['width' => 150]);

Expand Down
2 changes: 1 addition & 1 deletion docs/91-api-class-excel.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ public function loadImageFile(string $imageFile): ?array

### Parameters

* `string $imageFile`
* `string $imageFile` -- URL, local path or image string in base64

---

Expand Down
10 changes: 5 additions & 5 deletions docs/92-api-class-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* [addCellStyle()](#addcellstyle)
* [addChart()](#addchart) -- Add chart object to the specified range of cells
* [addDataValidation()](#adddatavalidation) -- Add data validation object to the specified range of cells
* [addImage()](#addimage) -- Add image to the sheet
* [addImage()](#addimage) -- Add image to the sheet from local file, URL or image string in base64
* [addNamedRange()](#addnamedrange) -- Define named range
* [addNote()](#addnote) -- Add note to the sheet
* [addStyle()](#addstyle) -- Alias for 'addCellStyle()'
Expand Down Expand Up @@ -278,13 +278,13 @@ _Add data validation object to the specified range of cells_
public function addImage(string $cell, string $imageFile,
?array $imageStyle = []): Sheet
```
_Add image to the sheet_
_Add image to the sheet from local file, URL or image string in base64_

### Parameters

* `string $cell`
* `string $imageFile`
* `array|null $imageStyle`
* `string $cell` -- Cell address
* `string $imageFile` -- URL, local path or image string in base64
* `array|null $imageStyle` -- \['width' => ..., 'height' => ..., 'hyperlink' => ...]

---

Expand Down
12 changes: 9 additions & 3 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1581,14 +1581,20 @@ protected function getImageInfo(string $imageBlob): array
}

/**
* @param string $imageFile
* @param string $imageFile URL, local path or image string in base64
*
* @return array|null
*/
public function loadImageFile(string $imageFile): ?array
{
if (preg_match('#^https?://.+#i', $imageFile)) {
$imageBlob = false;
$imageBlob = false;
if (preg_match('/^data:image\/(\w+);base64,/', $imageFile, $matches)) {
// $imageType = $matches[1];
$base64Image = substr($imageFile, strpos($imageFile, ',') + 1);
$imageBlob = base64_decode($base64Image);

}
elseif (preg_match('#^https?://.+#i', $imageFile)) {
$response = file_get_contents(
$imageFile,
false,
Expand Down
8 changes: 4 additions & 4 deletions src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3439,15 +3439,15 @@ public function getNotes(): array
// === IMAGES === //

/**
* Add image to the sheet
* Add image to the sheet from local file, URL or image string in base64
*
* @example
* $sheet->addImage('A1', 'path/to/file');
* $sheet->addImage('A1', 'path/to/file', ['width => 100]);
*
* @param string $cell
* @param string $imageFile
* @param array|null $imageStyle
* @param string $cell Cell address
* @param string $imageFile URL, local path or image string in base64
* @param array|null $imageStyle ['width' => ..., 'height' => ..., 'hyperlink' => ...]
*
* @return $this
*/
Expand Down
12 changes: 10 additions & 2 deletions tests/FastExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,22 @@ public function testExcelWriter5()
unlink($testFileName);
}

$excel = Excel::create(['Demo']);
$excel = Excel::create(['Demo1', 'Demo2']);
$excel->setDefaultFont([Style::FONT_NAME => 'Century']);

$excel->setLocale('en');
$sheet = $excel->sheet();
$sheet->setTopLeftCell('c3');

$sheet->writeRow([1, 11, 111]);
$sheet->writeRow([1, 11, 111])->applyFontStyleBold();
$sheet->writeRow([2, 22, 222]);
$sheet->writeCell(3);
$sheet->writeCell(33);
$sheet->writeCell(333);

$sheet = $excel->sheet('Demo2');
$sheet->writeHeader(['AAA', 'BBB', 'CCC'])->applyFontStyleBold();

$this->excelReader = $this->saveCheckRead($excel, $testFileName);

$this->cells = $this->excelReader->readRows(false, null, true);
Expand All @@ -602,6 +607,9 @@ public function testExcelWriter5()
$this->assertEquals([2, 22, 222], $this->getValues(['c4', 'd4', 'e4']));
$this->assertEquals([3, 33, 333], $this->getValues(['c5', 'd5', 'e5']));

$cells = $this->excelReader->sheet('Demo2')->readCellsWithStyles();
$this->assertEquals('Century', $cells['A1']['s']['font']['font-name']);

unlink($testFileName);
}

Expand Down

0 comments on commit 96078b4

Please sign in to comment.