PHPWord: Replaced image doesn't gets actually replaced - php

I have a word document (template) which contains some images that I have to replace with others that are present on the drive. The images that exist in the documents are currently blank. And the ones that will replace them have some text written on them. One way to do was to edit each document and then by double-clicking the image I use the Pbrush to edit the image. Other way is below.
I used PHPWord to do the job. Open the document and replace the image in the word/media.
The problem: At first look it looks like the image has been replaced. Also, if I check through zip/winrar the images are replaced. But when I double click the image in the doc I see the old(blank) image in PBrush. I want the new edited image to show up. Or an alternate way to do the job.
This function does not exist in the original PHPWord but found it as the quick fix to do the job. (PhpWord/Template.php)
public function replaceImage($path,$imageName) {
$this->_objZip->deleteName('word/media/'.$imageName);
$this->_objZip->addFile($path,'word/media/'.$imageName);
}
My Code:
$PHPWord = new PHPWord();
$document = $PHPWord[$i]->loadTemplate('ALL_FILES/Template.docx');
$document->replaceImage($oldImage, $newImage);
$document->save('ALL_FILES/genForms/updatedDoc.docx');
P.S. Hope it is clear what I am trying to say. If need be, I can upload a test document somewhere, in case the problem is not clear.

Related

Add remote images to PhpSpreadseet cell

There is a simple two-column table on a website: product name and product image. It's very easy to render in HTML.
The task would be creating an Xlsx file with these columns.
The images are not stored locally but all of them are remote images with full URL.
The export contains ~100-200 rows.
I tried to create a resource with imagecreatefromjpeg and adding it with MemoryDrawing but it took a huge amount of resources.
I tried with Html helper's toRichTextObject and a simple tag but got empty result.
How is it possible to add a remote image to a PhpSpreadsheet cell? It doesn't need working offline, it's fine to load the remote images when the file is opened.
As #FabriceFabiyi mentionned in his comment, and after reading:
PhpSpreadseet documentation on drawing.
PHP documentation on file_get_contents.
PHP documentation on file_set_contents.
This worked for me:
$image = file_get_contents('https://website.com/path/to/image');
$imageName = 'a_nice_image_name';
//You can save the image wherever you want
//I would highly recommand using a temporary directory
$temp_image=tempnam(sys_get_temp_dir(), $imageName);
file_put_contents($temp_image, $image);
// And then PhpSpreadsheet acts just like it would do with a local image
$drawing->setPath($temp_image);
$drawing->setHeight(36);
$drawing->setWorksheet($sheet);
$drawing->setCoordinates('B15');
More on temporary directories in the PHP docs:
sys_get_temp_dir()
tempnam()
There is an example how add image to your excel export:
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('/images/image-1.png'); // put your path and image here
$drawing->setHeight(30);
$drawing->setCoordinates('A5');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
Specifying coordinates for the image might help, as per the examples and the documentation
$objDrawing->setCoordinates('A3');
Note that an image isn't in a cell/column/row, but overlaid over the main sheet at the same position as that cell/column/row

Change picture using OpenTBS

I saw many post about changing picture in OpenTBS but I don't understood how to do that.
I have a .odt file wich has an image and i need to replace it from php with another one.
From documentation i see the Exemple: [ onshow.x;ope=changepic] but I have no idea where I have to put this line.
I've also tried to use this code below but nothing happens.
"ticket.odt" has an image (it has $PicRef descripton) which I would like to change.
$TBS= new clsTinyButStrong;
$TBS->PlugIn(TBS_INSTALL, OPENTBS_PLUGIN);
$TBS->LoadTemplate("Ticket/ticket.odt");
$TBS->PlugIn(OPENTBS_CHANGE_PICTURE, $PicRef, $logo );
As documentation $PicRef is a string that is saved in the Title or the Description of the picture and $logo is the path for picture file that will be copied inside the document
Could someone explain me how it'works and what i have to do?
Example using command OPENTBS_CHANGE_PICTURE:
PHP side:
$TBS->PlugIn(OPENTBS_CHANGE_PICTURE, 'my_picture', 'logo.png');
$TBS->Show(OPENTBS_FILE, $file_name);
Template side:
In the ODT template do a right-click on the picture you want to be replaced then you have the contextual menu. In the contextual menu, select Properties. Then in the tab Options, change the property Name to 'my_picture'.
When running the script, the picture will be replaced.
Example using parameter changepic:
PHP side:
$TBS->VarRef['x'] = 'logo.png';
$TBS->Show(OPENTBS_FILE, $file_name);
Template side:
Put the TBS field [onshow.x;ope=changepic] somewhere in the normal text after the picture you whant to be changed.
Or
Put the TBS field [onshow.x;ope=changepic;tagpos=inside] in the property Name of the picture (see previous example).

How to edit word documents with php?

I have an existing word document in my computer and like to edit this file from my website (using PHP). I was able to find PHPWORD but this deals with new documents only. I don't want to code PHP for the whole document, instead wish to use it for the stuff that varies.
Does anybody know any way out?
https://github.com/PHPOffice/PHPWord
PHPWord also features a Reader which can be used to edit existing documents.
#nssmart,
Yes,mark a section on your word document with a variable such as {value1} and then, if you have PHPWORD downloaded, you can replace that section using code
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(Doc location);
$document->setValue('value1', 'Description');
This can be used to fill data in tables also.
https://github.com/PHPOffice/PHPWord
I also did this problem in phpword using the following code
if(!file_exists('file/word.docx')){
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
$templateProcessor->setValue('name', 'Akbarali');
$templateProcessor->setValue('time','13.02.2021');
$templateProcessor->setValue('month', 'January');
$templateProcessor->setValue('state','Uzbekistan');
$templateProcessor->saveAs('file/word.docx');
}
This will change the words in the demo.docx file. The new file is then saved in the word.docx file folder.
you can define variables in the form of $ {name} in the demo word file.
that is, ${name}, ${time}, ${month} and ${state}

How to treat a PHP image generation script as an image

This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}

Gmagick thumbnail for multi-page PDF

I'm attempting to create a thumbnail of a multi-page PDF document using Gmagick, however I only want the first page of the PDF, not all of them.
$thumb = new Gmagick();
$thumb->readImage("/path/to/file/document.pdf");
$thumb->setImageFormat('JPG');
$thumb->thumbnailimage(198, 255);
$thumb->writeImage("/path/to/file/document.jpg");
$thumb->destroy();
This code works, however instead of creating just 1 image 'document.jpg' it creates 'document.jpg.0', 'document.jpg.1', 'document.jpg.2', etc. for all of the PDF pages. I could go and delete all the additional pages and rename the first image just 'document.jpg' but that seems a bit hacky to me.
Is there a way to designate just the first page of the PDF? I can't seem to find anything, and Gmagick's documentation seems to be lacking.
Pekka is right. GraphicsMagick - just like ImageMagick - accepts the special pdf file name notation filename.pdf[0] which would render only the first page, filename.pdf[1] for the second page, and so on.

Categories