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?
Related
I am using a PHP function to add my logo as the watermark on images uploaded on my website. But I don't know why my watermark function is not working for png files. however, it works for jpeg files perfectly. this is my PHP function.
function watermark($img) {
global $wm_file, $wm_right, $wm_bottom;
// image values pulled from config.inc.php
$logo = './images/' . $wm_file; // path to the watermark.png
$sp = $wm_right; // spacing from right side
$sq = $wm_bottom; // spacing from bottom
$size = getImageSize($img);
$sizel = getImageSize($logo);
$imgA = imageCreateFromJpeg($img);
imageAlphaBlending($imgA, TRUE);
if($sizel[0] > $size[0] || $sizel[1] > $size[1])
{
// logo size > img size
$sizelo[0] = $sizel[0];
$sizelo[1] = $sizel[1];
$sizel[0] = ($sizel[0]/2);
$sizel[1] = ($sizel[1]/2);
}
else
{
$sizelo[0] = $sizel[0];
$sizelo[1] = $sizel[1];
}
$imgBa = imageCreateFromPng($logo);
$imgB = imageCreateTrueColor($sizel[0], $sizel[1]);
imageAlphaBlending($imgB, TRUE);
imageCopyResampled($imgB, $imgBa, 0, 0, 0, 0, $sizel[0], $sizel[1], $sizelo[0], $sizelo[1]);
imageColorTransparent($imgB, ImageColorAllocate($imgB, 0, 0, 0));
$perc = 100;
imageCopymerge($imgA, $imgB, ($size[0]-$sizel[0]-$sp), ($size[1]-$sizel[1]-$sq), 0, 0, $sizel[0], $sizel[1], $perc);
unlink($img);
if(imageJpeg($imgA, $img, 100))
{
imageDestroy($imgB);
imageDestroy($imgA);
return true;
}
chmod($img, 0777);
}
The problem I see is that you are using imageCreateFromJpeg() as the way to generate the resource for your $img that you are passing to the function.
If you pass a jpeg through the function it will work. If you pass a png it will not.
I recommend using imagecreatefromstring() to create all your resources as it is not dependent on the file type. Like so:
$source = imagecreatefromstring(file_get_contents($filePath));
Another benefit of this is that it will return false if the function fails to create a resource from the file path that you supplied meaning that the file is not an image file.
Now that you have a resource to use for the rest of your code, imageJpeg() will save the resource as a jpeg back to the file path.
Hope that helps.
One other side note. If you intend on using bmp images, the GD library does not have a built in function for bmps. However on PHP.net, someone did write a createimagefromBMP() that works really well. Also I think that on the latest version of PHP the GD library does now actually have a createimagefromBMP() function.
I also see that you are using unlink() to delete the image from your directory. This is not necessary for two reasons. The imageJpeg() will just overwrite the original. Also, if for some reason your script fails it may delete the image prematurely and you will loose the image without the new one being written.
Please be careful when using chmod(), always make sure that you set permissions back to the original permissions when you are done.
chmod($img, 777); //Give broad permissions.
//Do something.
chmod($img, 600(or whatever they were)); //Reset permission back to where they were before you changed them.
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
I am using the following Imagick function to resize an image. The code runs and resizes the image, but with an error from the server saying "An error occurred while requesting the document from the testing server." And I cannot detect the problem. In the browser it, however, outputs non-human readable characters. In case I try to output the resized/modified image to the browser, there is no problem. I face this problem as I try to save the image to the disk.
Here is my code:
<?php
imagick_resize('running.jpg');
function imagick_resize($image, $width = 460, $height = 300)
{
// define widescreen dimensions
// $width = 460;
// $height = 300;
// load an image
$i = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/' . $image);
// get the current image dimensions
$geo = $i->getImageGeometry();
// crop the image
if(($geo['width']/$width) < ($geo['height']/$height))
{
$i->cropImage($geo['width'], floor($height*$geo['width']/$width), 0, (($geo['height']-($height*$geo['width']/$width))/2));
}
else
{
$i->cropImage(ceil($width*$geo['height']/$height), $geo['height'], (($geo['width']-($width*$geo['height']/$height))/2), 0);
}
// thumbnail the image
$i->ThumbnailImage($width,$height,true);
// save or show or whatever the image
# $i->setImageFormat('png');
# header("Content-Type: image/png");
# unlink('small_square_img.png');
$i->writeImage($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/tmp/small_square_img.png');
# file_put_contents('small_square_img.png', $i);
exit($i);
}
?>
The data you are sending to the browser is not a valid image. It is quite likely that you have a Byte-Order-Marker in some of your files before the
You should check for that and fix it if it is occuring, or provide an example of the "non-human readable characters" for people to have a clue of what the problem is.
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.
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!