PHP dataURL to png image - php

I have captured an image from the canvas and sent it via a ajax post method to my php file.
Here I need to put it back into a png with a filetype and extension. As I will be adding this new image to a pdf using fpdf.
Here is my code. I am just left with a broken image link icon.
<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
$imgEmbed = $_POST['embed'];
//convert base64 string into an image file by wrapping
//it in the png container
$parts = explode(',',$imgEmbed);
$data = $parts[1];
$data = base64_decode($data);
header('Content-Type:image/png');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pdf->setSourceFile("test2.pdf");
//Import the first page of the file
$tppl = $pdf->importPage(1);
//Use this page as template
// use the imported page and place it at point 20,30 with a width of 170 mm
$pdf->useTemplate($tppl, null, null, 215.9, 279.4, TRUE);
//Select Arial italic 8
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(100, 100);
$pdf->Write(0, 'This is just a simple text');
$pdf->Image($newIm,null,null,150,100);// x y h w 'png'
$pdf->Output("custom.pdf", "I"); // d send to browser, F send to local
// I auto open in browser window.
?>

$newIm='test.png';<br>
if(exif_imagetype($newIm)==IMAGETYPE_PNG) {<br>
$file_type="PNG";<br>
}elseif (exif_imagetype($newIm) ==IMAGETYPE_JPEG) {<br>
$file_type="JPG";<br>
}<br>
$pdf->Image($newIm,null,null,150,100,$file_type);<br>
In Fpdf,The Image synatax reads the whole image pixel by pixel and if the image base is in png and we give the extension as test.jpg then it will give an error so here i have used exif_imagetype() which will give me the image type of the image.And then i will pass this $file_type in the syntax to get the result.And in my case it has worked.

Related

Creating a QR Code with a centered Logo in PHP with PHP QR Code Generator

I'm creating QR codes with PHP QR Code (http://phpqrcode.sourceforge.net/). It works well but now I need a free space for a custom graphic or logo in the center of it. And I want to do this without saving the image on the server. Has anyone a suggestion? What I've got so far is this:
<?php
$param = $_GET['projectid'];
$divider = ",";
$codeText = 'Projectname'.$divider.$param;
// outputs image directly into browser, as PNG stream
//QRcode::png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
QRcode::png($codeText, false, QR_ECLEVEL_H, 9, 2, true );
?>
Ok, I've found a solution. Where a image file is created temporaray to insert the the logo or whatever you want. I't just a very small change form the code found here http://ourcodeworld.com/articles/read/225/how-to-generate-qr-code-with-logo-easily-in-php-automatically
I'm using readfile() at the end to push everything directly to the output buffer.
<?php
// user input
$param = $_GET['projectid'];
$divider = ",";
// Path where the images will be saved
$filepath = 'content/images/qr/qr-temp-image.png';
// Image (logo) to be drawn
$logopath = 'content/images/qr/qr-freespace.png';
// we need to be sure ours script does not output anything!!!
// otherwise it will break up PNG binary!
ob_start("callback");
// text for the qr code
$codeText = 'Projectname'.$divider.$param;
// end of processing here
$debugLog = ob_get_contents();
ob_end_clean();
// create a QR code and save it in the filepath
QRcode::png($codeText, $filepath, QR_ECLEVEL_H, 9, 2, true );
// Start DRAWING LOGO IN QRCODE
$QR = imagecreatefrompng($filepath);
// START TO DRAW THE IMAGE ON THE QR CODE
$logo = imagecreatefromstring(file_get_contents($logopath));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Save QR code again, but with logo on it
imagepng($QR,$filepath);
// outputs image directly into browser, as PNG stream
readfile($filepath);
?>
There's a much better/modern library now available: https://github.com/chillerlan/php-qrcode
There is also an example for including space for a logo:
https://github.com/chillerlan/php-qrcode/blob/d650fe73067c2559519f09949e9b71b466ab2bee/examples/imageWithLogo.php

Save images in 300 dpi using phpimageworkshop

Hello I am working on a screen to print application, with a front end javascript customizer that will send then send xml data via ajax to my server.
I am using phpimageworkshop
to generate the final customized images, everything is working well except for the final output file which is being outputted in 72 DPI even though the initially uploaded image is of 300 DPI. For my purpose I need the final output in print ready 300 DPI, Here is the simple code for generation.
<?php
use PHPImageWorkshop\ImageWorkshop; // Use the namespace of ImageWorkshop
require_once('PHPImageWorkshop/ImageWorkshop.php'); // Be sure of the path to the class
$pinguLayer = ImageWorkshop::initFromPath(__DIR__.'/images/cards-01.jpg');
echo $pinguLayer->getWidth();
echo $pinguLayer->getHeight();
//$pinguLayer->resizeInPixel(400, null, true);
$wwfLogoLayer = ImageWorkshop::initFromPath(__DIR__.'/images/wwf.png');
$tuxLayer = ImageWorkshop::initFromPath(__DIR__.'/images/tux.png');
/*$wwfLogoLayer->resizeInPixel(64, 64, true);
$tuxLayer->resizeInPixel(64, 64, true);
*/
$pinguLayer->addLayerOnTop($wwfLogoLayer, 20, 10, 'LB');
$pinguLayer->addLayerOnTop($tuxLayer, 20, 10, 'RT');
$dirPath = __DIR__."/exports/";
$filename = "pingu_edited.jpg";
$createFolders = true;
$backgroundColor = null; // transparent, only for PNG (otherwise it will be white if set null)
$imageQuality = 100; // useless for GIF, usefull for PNG and JPEG (0 to 100%)
$pinguLayer->save($dirPath, $filename, $createFolders, $backgroundColor, $imageQuality);
?>
Is there anyway for specifying the output in 300 DPI?

php take image, rotate and save rotated image on server

Want to take image from own server rotate certain angle and save the image.
Image file $filename = 'kitten_rotated.jpg'; With echo '<img src='.$filename.'>'; i see the image.
Then
$original = imagecreatefromjpeg($filename);
$angle = 90.0;
$rotated = imagerotate($original, $angle, 0);
Based on this https://stackoverflow.com/a/3693075/2118559 answer trying create image file
$output = 'google.com.jpg';
If i save the same image with new file name, all works
file_put_contents( $output, file_get_contents($filename) );
But if i try to save rotated image, then file_put_contents(): supplied resource is not a valid stream resource.
file_put_contents( $output, $rotated );
Here https://stackoverflow.com/a/12185462/2118559 read $export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image.. but can not understand how to use the code in that answer.
How to create image file from $rotated?
Tried to experiment, based on this http://php.net/manual/en/function.imagecreatefromstring.php
$fh = fopen( 'some_name.png' , 'w') or die("can't open file");
fwrite($fh, $data );
fclose($fh);
Does it means that need something like
$data = base64_encode($rotated);
And then write in new file?
I have not tested this, but I think you need to encode the image as base 64 first.
If you check the string from any Image URL, you'd see data:image/png;base64, preceding the hash. Prepending this to your image string and saving.
Here is a function that may help, based on what you already have:
// Function settings:
// 1) Original file
// 2) Angle to rotate
// 3) Output destination (false will output to browser)
function RotateJpg($filename = '',$angle = 0,$savename = false)
{
// Your original file
$original = imagecreatefromjpeg($filename);
// Rotate
$rotated = imagerotate($original, $angle, 0);
// If you have no destination, save to browser
if($savename == false) {
header('Content-Type: image/jpeg');
imagejpeg($rotated);
}
else
// Save to a directory with a new filename
imagejpeg($rotated,$savename);
// Standard destroy command
imagedestroy($rotated);
}
// Base image
$filename = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';
// Destination, including document root (you may have a defined root to use)
$saveto = $_SERVER['DOCUMENT_ROOT']."/images/test.jpg";
// Apply function
RotateJpg($filename,90,$saveto);
If you want to save image just use one of GD library functions: imagepng() or imagepng().
imagerotate() returns image resource so this is not something like string.
In your case just save rotate image:
imagejpg($rotated, $output);
And now You can use $output variable as your new filename to include in view like before:
echo '<img src='.$output.'>';
Don't forget to include appropriate permissions in directory where You're saveing image.

cut video thumbnails in php

I have found a code that pull out a thumbnail from a video and its work!
but when I trying to make the php file to print the image with the Content-Type its return an error, when i tried to save it to the server its work fine!
the code:
<?php
function captureVideoPosterImg($movie_file = '')
{
extension_loaded('ffmpeg');
// Instantiates the class ffmpeg_movie so we can get the information you want the video
$movie = new ffmpeg_movie($movie_file);
// Get The duration of the video in seconds
echo $Duration = round($movie->getDuration(), 0);
// Get the number of frames of the video
$TotalFrames = $movie->getFrameCount();
// Get the height in pixels Video
$height = $movie->getFrameHeight();
// Get the width of the video in pixels
$width = $movie->getFrameWidth();
//Receiving the frame from the video and saving
// Need to create a GD image ffmpeg-php to work on it
$image = imagecreatetruecolor($width, $height);
// Create an instance of the frame with the class ffmpeg_frame
$Frame = new ffmpeg_frame($image);
// Choose the frame you want to save as jpeg
$thumbnailOf = (int) round($movie->getFrameCount() / 2.5);
// Receives the frame
$frame = $movie->GetFrame($thumbnailOf);
// Convert to a GD image
$image = $frame->toGDImage();
// Save to disk.
//echo $movie_file.'.jpg';
header('Content-Type: image/jpeg');
imagejpeg($image,null,100);
}
echo captureVideoPosterImg("smovie.mp4");
?>
the file in convert to UTF8 without bom.
thx!

Html to PDF to JPEG

I am basically all the way there. I am unsure how to push the temporary pdf object to imagick to generate and return the jpeg image. Using mPDF's standard output(), I get the pdf rendered to the screen:
$mpdf=new mPDF();
$mpdf->WriteHTML($output);
$img = new Imagick($mpdf->output());
$img->setResolution(300,300);
$img->resampleImage(150,150,imagick::FILTER_UNDEFINED,1);
$img->resizeImage(512,700,Imagick::FILTER_LANCZOS,0);
$img->setImageFormat('jpeg');
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->writeImage ("test.jpeg");
Under this scenario I get the pdf outputed to the screen instead of a jpg image returned/created. What is the proper way to pass the pdf into imagick?
I think you need: $mpdf->Output('temp/'.$filename.'.pdf','F');
$mpdf->Output('temp/'.$filename.'.pdf','F');
$pdf_file = 'temp/'.$filename.'.pdf';
$savepath = 'temp/'.$filename.'.jpg';
$img = new imagick($pdf_file);
$img->setImageFormat('jpg');
$img->writeImage($savepath);
echo "<img src='temp/$filename.jpg' />";
http://phpform.net/pdf-to-image.php

Categories