Converting PNG images with transparency to jpg destroys the image

I am trying to convert any image that a user uploads to jpg format on a white background. However, I noticed that when a user loads a PNG image that contains transparency, ColdFusion destroys the entire image. It seems that digital corruption is happening.

Thus, the user first provides the URL where the image is located, and is cfhttpused for reading:

<cfhttp url="http://pathtoimage/image.png" method="get" useragent="#CGI.http_user_agent#" getasbinary="yes" result="PageResult">
<cfimage name="UserImg" source="#PageResult.FileContent#" />

So now UserImgis the image that the user wants to download. Then we do the usual anti-aliasing setting, and I also want the background to be white because the image could have a transparent background:

<cfset ImageSetAntialiasing(UserImg, "on")>
<cfset ImageSetBackgroundColor(UserImg, "white")>

The final step is to write it to the server as a jpg file:

<cfimage source="#UserImg#" action="write" destination="pathtoimages/userimage.jpg" overwrite="yes" format="jpg" />

, PNG . , jpg-, . :

: original png image

jpg: converted to jpg image

?

+4
1

ImageSetBackgroundColor . :

ColdFusion. . ImageClearRect.

jpeg , , jpeg.

PNG . .

<!--- use "rgb" to make background opaque --->
<cfset UserImgCopy = ImageNew("", UserImg.Width, UserImg.Height, "rgb", "white")>
<cfset ImagePaste(UserImgCopy, UserImg, 0, 0)>
<cfset ImageWrite(UserImgCopy, "c:\path\userimage.jpg", true)>
+7

Source: https://habr.com/ru/post/1626068/


All Articles