I am doing a project in which I use the PHP Spreadsheet library, more info here.
I need to add an image, a jpeg file, in the header of the .xlsx document. For this I am using part of the example code from the official documentation. In this way:
// Create a New Excel
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setPath('app/files/images/example.jpeg');
$drawing->setHeight(36);
$spreadsheet->getActiveSheet()->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_LEFT);
At the moment the image does not appear and I have not found any error in the code that I use or in the server log, it is a copy of the original, so the doubts that arise are if there is an incompatibility issue or something.
UPDATE:
I have done a test. With this code I have added an image in a cell:
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('app/files/images/example.jpeg'); /* put your path and image here */
$drawing->setCoordinates('A1');
$drawing->setWorksheet($spreadsheet->getActiveSheet());
It's works fine to use in cell because it's not works in header :(
Please Could you help me?
SOLUTION:
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setPath('app/files/images/example.jpeg');
$drawing->setHeight(25);
$sheet->getHeaderFooter()->addImage($drawing, \PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter::IMAGE_HEADER_RIGHT);
// Set header
$sheet->getHeaderFooter()->setOddHeader('&L&"Verdana,Negrita"&8HEADER TEXT&R&G');
After it sets the image object they is to set in OddHeader using codes:
$R -> To right align
$G -> Insert image
More info here.
I Have A Background Image Now I want PHP to Put Another Image On Top Of It (The Other Image Is Stored in A Variable)
Example variable $x
& My Background Image Is Also A Variable $back
Example ../img/back.jpg
Now i wish to Add The $x On The Left side of The Background
How May I Achieve this?
Like In This Pic There is The Green Part with a Shadow Image
How Can I replace that PART with another Picture using PHP?
(https://i.stack.imgur.com/IjK0o.jpg)
What i Have so Far
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=99&height=99", "picture.jpg");
$x = "picture.jpg";
copy("https://i.stack.imgur.com/IjK0o.jpg","bg.jpg");
$back = "bg.jpg";
?>
Combining images is part of image processing. You can use the gd library directly. However I recommend using an OO image processing library like intervention image, which is easier to write and understand.
// open the background image file
$img = Image::make('bg.jpg');
// Add the facebook image
$img->insert('picture.jpg');
// Save the image as a new file
$img->save('newpicture.jpg');
Read the documentation about the insert method to understand how to position the facebook image.
Sorry to ask this issue as it puzzled me for a long time using Codeigniter.
Here is what I am doing.
As seen below, I am displaying a form where there is a button "Download".
This download button should download that image into PDF file where the details
are written on it (those in RED box).
So this is the process I am doing.
Create an Image on the fly (dynamic) using PHP function imagecreatefromjpeg.
Download/Copy that image generated in the URL and save it in a folder in the server
Generate a PDF using mpdf referencing to that image created dynamically.
I can now create an image dynamically like this one below. Where I can write a text into the image (see 2015 text written in the image)
My problem is, when I click the "Download" button, it should not display
the image in the browser but automatically download it in the folder on the server then it calls the function MPDF to convert it into PDF file. Is this possible?
Code:
This is the Method called when user clicked the "Download" button
public function download_pdf() {
$data['image'] = $this -> get_photo();
$this -> mpdf_convert_pdf();
}
This is the function that creates the image dynamically. Once created, it should download it in the specified folder location on the server.
function get_photo(){
//Create an Image on the Fly
header('Content-Type: image/jpg');
$imgpath = site_url() . 'assets/images/2316.jpg';
$img = #imagecreatefromjpeg($imgpath);
$white = imagecolorallocate($img, 0, 0, 0);
$font_path = 'assets/fonts/arial.ttf';
$text = "2015!";
imagettftext($img, 100, 0, 300, 300, $white, $font_path, $text);
imagejpeg($img);
imagedestroy($img);
//Copy an Image from URL into a server folder
$content = file_get_contents('http://localhost/run_ms/ccertificates/get_photo');
file_put_contents('C:\xampp\htdocs\run_ms\assets\images\thumb\Test.png', $content);
}
And here's is the part where it converts the image into PDF file.
public function mpdf_convert_pdf() {
include ('application/third_party/mpdf/mpdf.php');
$mpdf = new mPDF('c', 'Legal', '', '', 5, 5, 5, '5', '5', '0');
//Display output
$html = '<img src="C:\xampp\htdocs\run_ms\assets\images\thumb\Test.png">';
//display PDF file
$mpdf -> WriteHTML($html);
$mpdf -> Output('Converted_pdf', 'D');
exit ;
}
Thank you so much experts any inputs you may share into this query of mine.
so the point is, in every "Download" button you clicked, the pdf should open to new tab and showing the data first?
In order not to display the created image in the browser, you need to specify the path where it will be saved into your server. Since my issue before is how to download the created image and not to be displayed in the browser.
To do so, you need to specify the path on the fm imagejpeg
So instead of doing like this:
imagejpeg($img);
It should be done this way:
imagejpeg($img,'C:\xampp\htdocs\rn_ms\assets\images\thumb\test.png');
The second parameter is the path and filename where it will be saved in the server as stated in the PHP manual (http://php.net/manual/en/function.imagejpeg.php ). So, if the path was not specified, it will be shown in the browser but if specified, then it will be downloaded as file in the specified server path.
Hopes this will help to other developers. :)
So I am trying to get a bunch of photos to appear that are saved in a mysql database. With the code I am using, the page displays just the very first photo in the database with nothing else. There are 5 different photos and I don't know where they are going. If someone could help that would be great. My code is here:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
}
The headers will only display one image per page.
You could try:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
print '<img src="data:image/png;base64,'.$data.'" />';
}
That's untested, but something like that should work.
A better way would be to store the image file on the disk and store the location in the database.
You could either insert them into mysql as in blob type or simply upload the file to a folder and insert the file name into the database (I assume you just dont create random folders for each instance therefore you would know the destination path already)
I am trying to generate a PDF with an image that is also generated by php.
Sounds simple enough and I am sure I'm just screwing up the header but I can't seem to find a solution here.
first I generate a PDF:
define('FPDF_FONTPATH','fonts/');
require('scpt/fpdf.php');
class PDF extends FPDF {}
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->SetAutoPageBreak(false,0);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);
$pdf->SetFont('Helvetica','',12);
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
then I generate the PNG in label.php:
if(isset($_GET["imgid"])) {
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
}
This will output: FPDF error: Not a PNG file:....
calling label.php?imgid=17 in the browser however will show me the image just fine...
What am I missing?
EDIT
In document:
Example
// Insert a logo in the top-left corner at 300 dpi
$pdf->Image('logo.png',10,10,-300);
// Insert a dynamic image from a URL
$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG');
so it SHOULD be possible to include a dynamically generated image without saving it first?!
EDIT #2
I made it work with the library mem_image but the problem remains that this should not throw an error IMO?! So I leave this question open to see if there is indeed something wrong with my script of this turning out to be a bug.
You almost had it, you're missing only a couple things. First you need the full server address in the URL for your Image() call, so instead of
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
You need:
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
That will eliminate the FPDF error you were encountering. Then to get FPDF to render your output correctly, you need to add a call to AddPage(), so your PDF generation script would become:
require('scpt/fpdf.php');
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->AddPage();
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
Of course you don't need the SetTextColor(), SetFont(), etc if you're only including the single image. You didn't need the custom class definition either (I removed the unneeded lines).
Don't forget to substitute www.yourserver.com for the appropriate domain and path for your script.
The argument to the Image function needs to be a file, not a URL or another script. Since you already have your label script, the easiest work around would be to download the image to a temp location and then call $pdf->Image() with that temporary file.
This problem occurred With your file name,Make sure your passing file name is valid
function _parsepngstream($f, $file)
{
// Check signature
if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
$this->Error('Not a PNG file: '.$file);
...
}
View allow_url_fopen directive
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
You need to do only changes that
require('fpdf.php');
and do the
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
and run your code...
First, try to check your url with this code
$handle = fopen("http://www.yourserver.com/label.php?imgid=17", "rb");
print_r(fread($handle, 8192));
you with see what the contain in your url
**) make sure your url is not redirect to the login page
much more easy, only put .png in the final of dinamic chain and its all
example:
$pdf->Image('http://www.yourserver.com/label.php? imgid=17.png',0,0,0,0,'PNG');
or
$pdf->Image('../label.php?imgid=17.png',0,0,0,0,'PNG');
the mime type is not recognized in the url why does not appear png, if it's a stupid mistake but it is proven, google does the same by adding the extension with the url & PNG