CFDocument displays wrong images - image

I am using ColdFusion 9 to create a PDF containing multiple QR codes.
Images were successfully created before generating the PDF document. The images are different to each other but they have a similar file size and resolution.
Now I generate HTML for each of the previous generated images and put it into a PDF document. The path is correct – I checked it.
<cfset amount="6" />
<cfdocument
format="pdf"
unit="cm"
marginTop="0.5"
marginLeft="0.5"
marginRight="0.5"
marginBottom="0.5"
pageType="A4"
filename="#path##name#.pdf">
<cfoutput>
<cfloop from="1" to="#amount#" index="i">
<cfset filename = "#name#_#i#" />
<img src="file://#path#codes/#filename#.png" style="width: 3.58cm; margin: 0 0.2cm 0.5cm;">
#path#codes/#filename#
</cfloop>
</cfoutput>
</cfdocument>
But there is one problem: Only the first image is displayed properly. Every other image is an identical copy of the first one. So I got 6 identical images in this PDF document.
Let me point out that the paths are right. It is not 6 times the same path.
Some completely different images are displayed properly. I think Coldfusion has some problems with displaying nearly identical (file size, resolution) images.
Is there a way to fix this problem?

Solved the problem.
There is a problem in ColdFusion with CFDocument and PNG bar code images. I converted them to JPG and everything works as expected.
<cfset amount="6" />
<cfdocument
format="pdf"
unit="cm"
pageType="A4"
filename="#path##name#.pdf">
<cfoutput>
<cfloop from="1" to="#amount#" index="i">
<cfset filename = "#name#_#i#" />
<cfimage
action="convert"
destination="#path#codes/#filename#.jpg"
source="#path#codes/#filename#.png" />
<img src="file://#path#codes/#filename#.jpg" style="width: 3.58cm; margin: 0 0.2cm 0.5cm;">
</cfloop>
</cfoutput>
</cfdocument>
Thank you for your help!

Related

Laravel Dompdf- Change photography size

I have a serie of photos taken with a mobile. I am trying to print a complete document in PDF (done by DOMPDF in Laravel), with text and a photo inserted in it. It works perfect, except for the photos, it got printed very very small.
I had same problem with background, but I designed it with the exact A4 size (in print point).
My question is how to modify the photographies in order to get them printed in the PDF at the size I want.
PHP Code:
$cliente = Cliente::findOrFail($id);
$view = \View::make('cpanel.pdf.curriculum', compact('cliente'))->render();
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($view);
return $pdf->download('cv-'.$cliente->nombre." ".$cliente->apellidos."-".Date('d-m-Y').'.pdf');
I tried with css rules, but DOMPDF ignores it for images
<img style="width: 125pt;border: 1px solid red;" src="{{URL::asset('/images/candidatos/'.$cliente->foto)}}" />
instead of asset {{URL::asset('/images/candidatos/'.$cliente->foto)}} use relative url eg . "home/public_html/img/imagename.jpg"
there is a semicolon after style= replace it with double quotes

ColdFusion: ImageScaleToFit

I have an image Slider with different images. Some of the images has the size ratio of 600x400px, others 400x600px.
But the gallery has the width of 570px and height of 350px.
I want to resize the pictures. They should go in the 570x350px size, without distortions.
I tried this:
<cfimage
action = "read"
source = "#getPfad.Wert##getBild.Dateiname#"
name = "bild_gross"
>
<cfset ImageSetAntialiasing(bild_gross)>
<cfset ImageScaleToFit(bild_gross,570,350)>
<cfimage
action="writetobrowser"
source="#bild_gross#"
>
But the image size won't change! The images are cut off.
Edit: The code works, if I try it without the gallery.
<div id="bilder">
<div id="amazingslider-wrapper-1" style="display:block;position:relative;max-width:706px;padding-left:0px; padding-right:146px;margin:0px auto 0px;">
<div id="amazingslider-1" style="display:block;position:relative;margin:0 auto;">
<ul class="amazingslider-slides" style="display:none; ">
<cfloop query="getBild">
<li>
<a href="#getPfad.Wert##getBild.Dateiname#" class="html5lightbox">
<cfimage
action = "read"
source = "#getPfad.Wert##getBild.Dateiname#"
name = "bild_gross"
>
<cfset ImageSetAntialiasing(bild_gross)>
<cfset ImageScaleToFit(bild_gross,570,350)>
<cfimage
action="writetobrowser"
source="#bild_gross#"
>
</a>
</li>
</cfloop>
</ul> ...
Does anyone know, what I can do?
How about using CSS? object-fit enables you to crop an inline image by specifying how it squishes and stretches inside its box. Using cover will enable the image to fill the height and width of its box while maintaining the aspect ratio but auto-crop the image if needed. (I'm not sure whether IE fully supports this yet or not, but there's a hack in the link below.)
<img src="/myimage.jpg" style="object-fit:cover;" width="570" height="350">
More info at https://css-tricks.com/almanac/properties/o/object-fit/
I suggest don't do image processing in your display/view. ImageScaleToFit function will resize the image and will use server resources each time users visit.
Have a separate function to process and resize those images. Then use the <img> tag to display those images. Using the img html tag will give you more options in controlling your display.

How to show images with keeping their proportion in mvc3?

In mvc3 Products have more images and there is standard box size that all images seems in that in view.
Users enter images in different sizes: 200x800, 2200x500 ..etc. But I must show all images in standard box in 150x120 sizes. Most images stretchs horizontal or vertical in 150x120 size box and seems very bad.
<img src="#Url.Content( Path.Combine( "~/Uploads/Products/", #item.Date.Value.ToShortDateString(), #item.ImagePath1 ) )" width="150" height="120" alt="" />
How can I do all images get small, but to keep proportion ?
one way to keep propotions is to set the width value in css, but then the image will have the width and the height is unknown depending och the startimage. This will be the simplest way to fix the streched images. But you will not know the height as mentioned.
And ofc you must load the big image (big transfer size), so this may not be a propriate solution.
razor code
<img src="#Url.Content( Path.Combine( "~/Uploads/Products/", item.Date.Value.ToShortDateString(), #item.ImagePath1 ) )" alt="" />
css code
.parentDiv img{
width: 150px;
}
You got good question. However one way to do this is to ask user to upload in some proportion using javascript.
E.g your input control should be like
<input id="ImageFile" name="ImageFile" type="file" onchange="check_filesize(this.value,'upload');" />
And in javascript your can check file size and etc.
Another is to make use of ImageResizeClass and in your Upload Control you can resize the file to match your expectation size to shown in div.
So your upload controller would be something like following...
public ActionResult Upload(Image image, HttpPostedFileBase ImageFile)
{
ResizeImageHelper resizeImageHelper = new ResizeImageHelper();
resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + ImageFile.FileName, 640, 480, false);
resizeImageHelper.ResizeImage(path + ImageFile.FileName, path + "thumb" + ImageFile.FileName, 74, 74, false);
image.imageLocation = ImageFile.FileName;
image.imageThumb = "thumb" + ImageFile.FileName;
imageRepository.Add(image);
imageRepository.Save();
}
This way you can make sure to show the image perfectly.
Hope you understand and this helps.

Embedded SVG is trimmed

I embedded an SVG object in an HTML page as follows:
<embed id="svgImg" src="bo5rkbgs5vtsmv053regld2t14.svg"
type="image/svg+xml" />
However, the resulting image (both on Firefox and Chrome) is trimmed and it look like that:
(Notice how "Step Response" and "Time" do not appear properly). I have checked the file on the server side and it is fine. Any ideas?
Having had a second look at your code your svg image is still too small.
You need to set the height to 100% and the image height to 500px.
<embed id="svgImg" height="100%" type="image/svg+xml" src="s1d5ckv8bojltpturlonh1uap5.svg">
<svg width="576" height="500" viewBox="0 0 576 432" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
....
This fixes the problem in firefox v8.0
Just tweak your viewBox attribute so that it fits your image.
For example, you could try this:
viewBox="-10 -10 596 452"
Update:
To generate the viewBox dynamically with JS, something like this should work:
var bbox = document.documentElement.getBBox();
var viewbox = document.documentElement.viewBox.baseVal;
viewbox.x = bbox.x;
viewbox.y = bbox.y;
viewbox.width = bbox.width;
viewbox.height = bbox.height;
Note that this needs to be done on the svg document, so you may need to reach into it from the html document that embeds it, if so have a look at this example.

crop an image from the centre using coldfusion

This is my first bit of programming in quite a while, so I'm basically starting from scratch, and I'm using coldfusion 8.
What I'm trying to do is create a series of uniform thumbnail images (always 68 X 46) from a variety of larger images, some portrait, some landscape. In both cases resizing the image to fill the height or width of the thumbnail and then cropping the excess image equally off either side (top/bottom, left/right). Just as photoshop does by default with canvas resize.
The code below works really well as long as the source images dimensions/ratio is perfect, but I've started to run into cases where the code fails. In this case, when a resized images width ends up being less than 68.
<cfif FileExists(ExpandPath('images/gallery/thumbs/thm_'&imageMed[i].medium.XmlText)) IS false> <!--- If the thumb doesn't exist, create it. --->
<cfif imageDataThumb.width gt imageDataThumb.height >
<!--- Landscape --->
<cfset ImageResize(cfImageThumb,"","46")>
<cfset ImageCrop(cfImageThumb,(cfImageThumb.width-68)/2,0,68,46)> <!--- Crop left/right edges of images --->
<cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
<cfelse>
<!--- Portrait --->
<cfset ImageResize(cfImageThumb,"68","")>
<cfset ImageCrop(cfImageThumb,0,(cfImageThumb.height-23)/2,68,46)>
<!--- Crop top/bottom edges of images --->
<cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
</cfif>
Trying to solve these "edge cases" is turning the code into a mess. Is there a better way to approach this? Something in coldfusion or a cfc?
I know this is old, but you could just use AspectCrop in the imageUtils library from ben nadel. Check riaforge.com. It does exactly what you are looking for. I know, i wrote it. :)
I'd check the proportions of the image first and adjust the short side crop accordingly. I'm using CFScript for conciseness, but this could easily be converted to Coldfusion tags.
<cfscript>
minimumRatio = 68 / 46; // 1.478 (68 pixels / 46 pixels)
width = ImageGetWidth(imageDataThumb);
height = ImageGetHeight(imageDataThumb);
// determine the longside and the aspect
if (width GT height) {
longside = width;
shortside = height;
aspect = "landscape";
} else {
longside = height;
shortside = width;
aspect = "portrait";
}
// determine the aspect ratio
if (longside / shortside GTE minimumRatio) {
// Do normal resize / crop
if (width EQ longside) {
// landscape
} else {
// portrait
}
} else {
// ratio is too small - perform some additional calculations before resize / crop
// in this case, you'll likely need to resize for the opposite side then crop the excess
}
</cfscipt>
<cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
If you don't want to check for ratio, then verify the length and width is enough before resizing using similar logic.
The code needs to not only pay attention to the current ratio, but how you're changing the ratio with the crop.
Also, the height calculation was based on half the desired height, and should have been the full height.
Assuming all else is correct with your code, you should be able to place the code below into your FileExists if block, replacing the current content.
<cfset CurrentWidth = imageDataThumb.width>
<cfset CurrentHeight = imageDataThumb.height>
<cfset CurrentRatio = CurrentWidth / CurrentHeight>
<cfset DesiredRatio = 68 / 46>
<cfif CurrentWidth GTE CurrentHeight>
<!--- Landscape Image --->
<cfif CurrentRatio LT DesiredRatio>
<!--- More Landscape --->
<cfset Keep = "width">
<cfelse>
<!--- Less Landscape --->
<cfset Keep = "height">
</cfif>
<cfelse>
<!--- Portrait Image --->
<cfif CurrentRatio GT DesiredRatio>
<!--- More Portrait --->
<cfset Keep = "height">
<cfelse>
<!--- Less Portrait --->
<cfset Keep = "width">
</cfif>
</cfif>
<cfif Keep EQ "width">
<!--- Crop top/bottom edges of images --->
<cfset ImageResize(cfImageThumb,"68","")>
<cfset ImageCrop(cfImageThumb,0,(cfImageThumb.height-46)/2,68,46)>
<cfelse>
<!--- Crop left/right edges of images --->
<cfset ImageResize(cfImageThumb,"","46")>
<cfset ImageCrop(cfImageThumb,(cfImageThumb.width-68)/2,0,68,46)>
</cfif>
<cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
No. ImageScaleToFit would makes sure the image fits inside the bounds, which in this case leaves white space. I need to completely fill the available space with image.

Resources