Create/Download an Image using Codeigniter - php

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. :)

Related

PHP Multiple Image Scripts On One Image Source

I have 2 different scripts for handling images:
The first one is a watermark script for watermarking images on the fly:
$imgpath=$_REQUEST['filename'];
header('content-type: image/jpeg');
$watermarkfile="assets/img/logo_variations/logo_watermark_75.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = ($size[0] - $watermark_width)/2;
$dest_y = ($size[1] - $watermark_height)/2;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
So the image URL for a watermarked image is: http://example.com/watermark.php?filename=assets/img/temp/temp_share.jpg or since I'm using mod_rewrite to "pretty up" my URL: http://example.com/watermark/assets/img/temp/temp_share.jpg.
Works like a charm and my reason for doing so like this is because this is on a modeling website where I want to display the images without watermarks but I use a jquery script to change the image source of the image gets right clicked(assuming a user is trying to save the image).
The script only changes the source of any image with a class of img-protected.
I've written it to ignore any image with watermark in the URL so that it doesn't try to change the already watermarked image which would result in a url like: http://example.com/watermark/watermark/img.jpg which would result in a broken image. The other part is written to remove http://example.com from the original source so I don't end up with http://example.com/watermark/http://example.com/img.jpg.
$('.img-protected').on('mousedown', function (event) {
if (event.which == 3) {
if(this.src.indexOf("watermark") > -1) {
return false;
}
else {
src = this.src.replace('http://example.com/','');
this.src = 'http://example.com/watermark/' + src;
}
}
});
All of this works exceptionally well until I added another image handling script:
I'm using TimThumb.php which is an on the fly image resize script I use for creating gallery icons instead of uploading an icon and a full size image(this is how I wish to keep doing so as well).
The problem I am facing is this:
If I have an image that is being turned into a thumbnail using TimThumb.php which I renamed to thumb.php on my server the URL is http://example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which gives me an icon for 5361ae7de9404.jpg.
All of my icons have a class of img-protected which means on right click the above URL is going to be changed to the watermarked one.
This is where it fails.
The outputed URL when right clicked is http://example.com/watermark/http://www.example.com/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 which results in a broken image.
I manually tried making the URL into http://example.com/watermark/thumb.php?src=gallery/goth/industrial_brick/5361ae7de9404.jpg&w=350&h=500a=c&s=1&f=11 to see if that would change anything but it still results in a broken image.
What I need is to be able to also watermark the generated icons from thumb.php using watermark.php.
Is there a way to combine these two scripts or a workaround to make this work?
I'm at a complete loss here.
EDIT: I am fully aware that advanced users can still acquire the non watermarked image since it's already been downloaded to there device, but I don't expect a high volume of users to visit this particular website as this is simply a local models portfolio.

Image not saving properly [duplicate]

This question already has an answer here:
How to change the filename displayed in the "Save as..." dialog from .php to .png
(1 answer)
Closed 9 years ago.
This is the link to the project I'm making: http://1-to-n.com/ik/
It is simple you type in anything and press submit, then it goes to the next page and displays the text on the picture. Everything is working great, but when I right click the picture and try to save it the extension is weird like "picture.php.jpe".
The picture generating page code is as followed
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('vote.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 47, 45, 46);
// Set Path to Font File
$font_path = 'MISTRAL.TTF';
// Set Text to Be Printed On Image
$text = $_POST['name'];
// Print Text On Image
imagettftext($jpg_image, 75, 0, 500, 130, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
The solution here is Content-Disposition, but exactly what you want depends how how you want the UI to work.
You want to set:
header('Content-Disposition: %token%; filename="%name_of_file%"');
Where:
%name_of_file% is the name of the file that you want the user to see
%token% is one of attachment or inline. If you set it to attachment, the user will be immediately prompted to download the file, the browser will not display the image as it currently does. If you set inline, the user will be shown the image in the browser pane as they currently are, and they will need to right click -> save as in order to save the image.
You will need to decide on how you want the UI to work before you can determine which header to use.
For clarity, here are a couple of concrete examples:
// Prompt the user to save the file immediately
header('Content-Disposition: attachment; filename="file.jpg"');
// OR
// Display the image to the user and ask them to manually save it
header('Content-Disposition: inline; filename="file.jpg"');
Something worth noting is that IIRC, older versions of IE had issues with inline in that they didn't take any notice of the suggested file name. However this could be worked around by adding a name="" token to the Content-Type:. This is a standards violation but also fairly harmless.

PHP how to output dynamic image

I think this will be an easy answer but I just can't figure it out.
I have a script that generates a pdf using fpdf and fpdi.
If I use a normal image then the pdf generates perfectly but I am trying to use an image generated dynamically by php.
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// image
$src = $bc->getVoucher();
$pdf->Image($src, 22, 94, 100, 15);
This gives the error:
FPDF error: Image file has no extension and no type was specified: Resource id #16
If I do
$barcode = imagejpeg($src);
$pdf->Image($barcode, 22, 94, 100, 15);
presumably because the imagejpeg is actually outputting the image but the image header is set because if the jpeg header is set the image will display fine so I'm just trying to figure out the correct way of doing this.
If it's easier, I have a separate script which can generate the image, e.g. `printbarcode.php' but I don't know how to get the contents of that script into this function:
$pdf->Image($barcode, 22, 94, 100, 15);
From the error message I would guess that the $pdf->Image() function is expecting $src to be a path to a file. What you are passing seems like a object.
Try saving the image to disk and loading from there. You are almost there with imagejpeg() function - see docs on php.net. Supply a path to save the file
imagejpeg($img, 'myfile.jpg', 90); // 90 denotes high quality
$pdf->Image('myfile.jpg', 22, 94, 100, 15);
...
unlink('myfile.jpg'); // delete after
or alternatively look for a function on the fpdf library that takes image resource as a parameter.
You don't need the image handler PHP uses to manipulate the image. What you need is the actual result of the image manipulation, namely the resulting file. Use imagejpeg($img, '/path/to/some/tmp/dir/file.jpg'); and hand the path over to your $pdf->Image() call. Then after creating the PDF you can safely remove the temporarily stored image, I think.
you have to do like this with the help of php's GD library..
//Create a new image from file or URL
$img_src = imagecreatefromjpeg($src);
// Output the image
imagejpeg($img_src);
// Free up memory
imagedestroy($img_src);

php include generated png in pdf via fpdf

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

Need help with PHP GD imagesetpixel()

$imageurl = "kaptka.gif";
$im = imagecreatefromgif($imageurl);
$b = imagecolorallocate($im, 0, 0, 0);
imagesetpixel($im, 5, 5, $b);
header('Content-Type: image/gif');
imagegif($im, "asd.gif");
kaptka.gif is normal gif image. I want to draw some pixels anywhere on the image. Asd.gif looks normal, but when i open the file it should show me both like asd.gif, but it shows just "IMAGE" text.
You are calling the function imagegif() with a filename. This will write the image to disc. To display the contents, simply call it without the filename: this will pass the contents to the output stream, which means it will be sent to the client's browser. One way to do both is to call it twice, once to save the file, and once to display it.
When saving the file, you can assign the result to a variable $foo=imagegif($im, 'bar.gif') and then check the result to see if the save was successful. A FALSE means it failed.
You say the image saved to the server is OK, so the reason you are getting an "IMAGE text" in your browser is probably because you are sending a PNG header, but no data (because of the way you called imagefig()).

Categories