@@ -120,8 +120,8 @@ gdImagePtr gdImageCreateFromPngCtx (gdIOCtx * infile)
120
120
#endif
121
121
png_structp png_ptr ;
122
122
png_infop info_ptr ;
123
- png_uint_32 width , height , rowbytes , w , h ;
124
- int bit_depth , color_type , interlace_type ;
123
+ png_uint_32 width , height , rowbytes , w , h , res_x , res_y ;
124
+ int bit_depth , color_type , interlace_type , unit_type ;
125
125
int num_palette , num_trans ;
126
126
png_colorp palette ;
127
127
png_color_16p trans_gray_rgb ;
@@ -226,6 +226,20 @@ gdImagePtr gdImageCreateFromPngCtx (gdIOCtx * infile)
226
226
}
227
227
#endif
228
228
229
+ #ifdef PNG_pHYs_SUPPORTED
230
+ /* check if the resolution is specified */
231
+ if (png_get_valid (png_ptr , info_ptr , PNG_INFO_pHYs )) {
232
+ if (png_get_pHYs (png_ptr , info_ptr , & res_x , & res_y , & unit_type )) {
233
+ switch (unit_type ) {
234
+ case PNG_RESOLUTION_METER :
235
+ im -> res_x = DPM2DPI (res_x );
236
+ im -> res_y = DPM2DPI (res_y );
237
+ break ;
238
+ }
239
+ }
240
+ }
241
+ #endif
242
+
229
243
switch (color_type ) {
230
244
case PNG_COLOR_TYPE_PALETTE :
231
245
png_get_PLTE (png_ptr , info_ptr , & palette , & num_palette );
@@ -526,6 +540,12 @@ void gdImagePngCtxEx (gdImagePtr im, gdIOCtx * outfile, int level, int basefilte
526
540
png_set_filter (png_ptr , PNG_FILTER_TYPE_BASE , basefilter );
527
541
}
528
542
543
+ #ifdef PNG_pHYs_SUPPORTED
544
+ /* 2.1.0: specify the resolution */
545
+ png_set_pHYs (png_ptr , info_ptr , DPI2DPM (im -> res_x ), DPI2DPM (im -> res_y ),
546
+ PNG_RESOLUTION_METER );
547
+ #endif
548
+
529
549
/* can set this to a smaller value without compromising compression if all
530
550
* image data is 16K or less; will save some decoder memory [min == 8]
531
551
*/
0 commit comments