pdf_fit_image, relation to DPI pdflib - php

PDF LIB
i want to know if pdf_fit_image internally does some resizing based on DPI of the image
// image dimenstions of the 150_dpi and 300_dpi is same 900px , 558 px
$imgFile = "image/150_dpi.jpg";
$imgFile2 ="image/300_dpi.jpg";
$imgFile3 ="image/96_dpi.jpg";
$image = pdf_load_image($pdfdoc, "auto", $imgFile, "");
$image2 = pdf_load_image($pdfdoc, "auto", $imgFile2, "");
$image3 = pdf_load_image($pdfdoc, "auto", $imgFile3, "");
pdf_begin_page($pdfdoc, 595, 842);
pdf_fit_image($pdfdoc, $image2, 10, 300, "");
pdf_end_page($pdfdoc);
pdf_begin_page($pdfdoc, 595, 842);
pdf_fit_image($pdfdoc, $image2, 10, 300, "");
pdf_end_page($pdfdoc);
pdf_begin_page($pdfdoc, 595, 842);
pdf_fit_image($pdfdoc, $image3, 10, 300, "");
pdf_end_page($pdfdoc);
Even though the 150 DPI image and 300 DPI image are of same dimension , they are of varied size in the pdf.
Is function pdf_fit_image DPI specific?
Shouldn't two images of different DPI but same dimension, print similarly in PDF?
Basically i want to use a higher DPI image in the PDF , but with width and height less than 1000px and 600px respectively.

Even though the 150 DPI image and 300 DPI image are of same dimension
, they are of varied size in the pdf.
this is expected.
Is function pdf_fit_image DPI specific? Shouldn't two images of
different DPI but same dimension, print similarly in PDF?
yest to the first question, no to the second. PDF_fit_image() honor the DPI of an loaded image. From the PDFlib 9 API Reference, chapter 9.1 within the details of fit_image():
By default, an image will be scaled according to its resolution value(s).
This behavior can be modified with the dpi, scale, and fitmethod options

Related

Laravel Image Intervention avoid rotation

I'm uploading an iPhone image - taken by iPhone camera in vertical - with the dimensions of 2448x3264 and because this dimensions are so high (?) when I create a thumb of 600x360 it automatically rotates to horizontal.
What have I tried without any success:
Change the thumb dimensions
Use the fit function
Use the resize function
Use the crop function
Use the upsize and aspectRatio methods
Set only the height and use null on width
Set only the width and use null on height
The thumb must have a maximum of height of 360 and I'm ok if the width is not 600.
$imageResize = Image::make($originalFile);
$imageResize->fit(600, 360, function ($constraint)
{
$constraint->upsize();
});
$imageResize->save($thumbPath);
My goal is to have:
Thumbnails in vertical if the original photo is vertical
Thumbnails in horizontal if the original photo is horizontal
How can I achieve this?
As spoke before, the image is being saved in its correct orientation and at the point of resizing you are running the fit() function on which I was able to find some information on this issue running along side that which suggests you need to use orientate() with fit.
An example here:
$imageResize = Image::make($originalFile);
$imageResize->orientate()
->fit(600, 360, function ($constraint) {
$constraint->upsize();
})
->save($thumbPath);
I'm glad this helped.
According to this github issue you may need to run orientate() before fit():
$imageResize = Image::make($originalFile)
->orientate()
->fit(600, 360, function ($constraint) {
$constraint->upsize();
})
->save($thumbPath);
$img = Image::make($originalFile);
$img->orientate();
$img->resize(1024, null, function($constraint){
$constraint->upsize();
$constraint->aspectRatio();
});
$img->save();

Intervention - Resize - Quality

I am trying to resize images from 3000 * 4000 px with a size of 7 MB to images of height 650px. With intervention I am using the following steps.
Image::configure(array('driver' => 'gd'));
$img = Image::make($imgName);
$img->resize(null,650, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$img->encode('jpg', 100);
$img->save($img_path, 100);
But the images that gets saved looks a bit blurred. Would be great any one could direct me with the correct steps.
Thanks
Lynn

phpImageWorkshop: Removing a layer after saving

In the phpImageWorkshop documentation (http://phpimageworkshop.com/doc/13/saving.html) it says:
...after saving, you'll be able to continue to use your document and
to perform some actions on its sublayers, really convenient !
However, after calling save() I'm unable to remove the watermark layer.
I start by loading the photo and watermark and resize the photo:
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath($tmp_name);
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath($watermark);
$photo->resizeInPixel(960, null, true);
And then I add the watermark, save the photo, then remove the watermark (so I can make other sizes without a watermark without creating a new object):
$photo->addLayer(1, $mark, 0, 0, 'LB');
$photo->save($path, $filename, false, null, 80); // file correctly has watermark
$photo->remove(1);
$photo->resizeInPixel(550, null, true);
$photo->save($path, $filename, false, null, 80); // file has watermark, not correct
This does not delete the watermark layer. However, if I call remove() before save() it will remove the watermark:
$photo->addLayer(1, $mark, 0, 0, 'LB');
$photo->remove(1); // calling remove() before save removes watermark
$photo->save($path, $filename, false, null, 80); // file does not have watermark
I cannot understand why this is happening, since the documentation clearly says calling save() does not change the layers.
I've confirmed that the watermark layer is being put on layer level 1, and it works OK if I do not call save().
Despite the documentation saying you'll be able to continue to use your document, the fact is that the save() function calls getResult() which returns a merged resource image (this is in ImageWorkshopLayer.php)
However, if you create a 'base layer' and add the photo and watermark on top of it, the save() function appears to merge to the base layer - leaving the photo and mark untouched, so you can remove the mark and re-save (which causes the photo to be re-merged onto the base layer) i.e.
$baseimg = PHPImageWorkshop\ImageWorkshop::initVirginLayer(1024,800);
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath("test.png");
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath("test2.png");
$iphoto= $baseimg->addLayerOnTop($photo, 0, 0, 'LB');
$imark= $baseimg->addLayerOnTop($mark, 0, 0, 'LB');
$baseimg->resizeInPixel(960, null, true);
// file correctly has watermark
$baseimg->save(__DIR__, "test_out.png", false, null, 80);
$baseimg->remove($imark['id']);
// file correctly has no watermark
$baseimg->save(__DIR__, "test_out2.png", false, null, 80);
Note that I use the return value from addLayerOnTop() to determine the 'id' pf the layer to remove.
EDIT: although the above works, it is not ideal because you really need the resulting image to be the size of the re-sized photo. Also I found that once the photo is made a layer, you have to resize that layer (not the original photo) so...
// load photo and mark
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath("test.png");
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath("test2.png");
// resize the photo 1st time
$photo->resizeInPixel(960,null, true);
$width= $photo->getWidth();
$height= $photo->getHeight();
// make empty base image same size as photo
$baseimg = PHPImageWorkshop\ImageWorkshop::initVirginLayer($width,$height);
// add photo and mark onto base image
$iphoto= $baseimg->addLayerOnTop($photo, 0, 0, 'LT');
$imark= $baseimg->addLayerOnTop($mark, 0, 0, 'LT');
$photoid= $iphoto['id']; // get layer id's
$markid= $imark['id'];
// file correctly has watermark
$baseimg->save(__DIR__, "test_out.png", false, null, 80);
// remove mark layer
$baseimg->remove($imark['id']);
// resize photo again
// - photo is now a layer in baseimg
$baseimg->layers[$photoid]->resizeInPixel(550,null, true);
$width= $baseimg->layers[$photoid]->getWidth();
$height= $baseimg->layers[$photoid]->getHeight();
// crop baseimg to match photo size
$baseimg->cropInPixel($width,$height);
// file correctly has no watermark
$baseimg->save(__DIR__, "test_out2.png", false, null, 80);
That seems to work fine now.

How to implement imagecopy (GD) function by Imagick?

I'm working with imagick and face some problem. I want to composite two images: image01 and image02,image01 is background image,a part of image02 composite on image01. the function just like GD's imagecopy function.
bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y,
int src_x, int src_y,int src_w, int src_h )
Copy a part of src_im onto dst_im starting at the x,y coordinates
src_x, src_y with a width of src_w and a height of src_h. The portion
defined will be copied onto the x,y coordinates, dst_x and dst_y.
the question is: how to implement imagecopy function by Imagick?
thanks for your help.
This should do it:
//load files from source
$background = new Imagick(image01_src);
$overlay = new Imagick(image02_src);
//Crop the overlay to the required size
$overlay->cropImage ($new_width,$new_height,$x_offset,$y_offset);
//composite overlay on background
$background->compositeImage($overlay, Imagick::COMPOSITE_OVER, $margin_x, $margin_y);
//save result
$background->setImageFormat("png");
$background->writeImage(new_src);
//clean up
$background->clear();
$background->destroy();
$overlay->clear();
$overlay->destroy();
Use composite, for example:
$large_image->compositeImage($small_image, Imagick::COMPOSITE_OVER, $margin_x, $margin_y);
If you show me the source and final pictures, I can give you the exact code.

tcpdf imagebox and cropping

I would like to add a 'imagebox' a box which contains the image and crops exceeding image value that is outside of this box. something like this:
I am not sure on how to do this if it's even possible.
Actually you can do this with Clipping.
The below line would show a photo of 200X300:
$pdf->Image('photo.JPG', 100, 100, 200, 300, '', true, '', false, 300);
To clip it you need:
$pdf->StartTransform();
$pdf->Rect(100, 100, 200, 300, 'CNZ'); //Clipping mask (CNZ style makes your day)
$pdf->Image('photo.JPG', 50, 50, 300, 400, '', true, '', false, 300);
//this would actually cut off a 50 units a in each direction.
$pdf->StopTransform();
You could crop an image with php , store it as a temp_file pass it to tcpfd and then delete it after the rendering of the pdf was done . Another option would be to use html/css to position a html element over the image but as we all know tcpdf doesn't know too much about css so i don't know if it will work .

Categories