H
H
HaruAtari2014-01-09 09:27:16
Yii
HaruAtari, 2014-01-09 09:27:16

Why does the transparent background fill incorrectly when converting png to jpg?

Good afternoon.
When uploading a png image to the site, you need to convert it to jpg to reduce the volume. The site runs on Yii and for this task I use this class (GD is used internally).
I encountered strange behavior of the program when converting images to a transparent background. Usually a transparent background is filled with black, but sometimes there are files that have a torn black stroke around the main object in the photo, and then everything is white. This problem does not occur with all files.
Here's an example of an image that converts fine (first a png, then a jpg result):

490Kb

fda96437bffef2b87f747af330322786.png
e6f396d9b8a5d6e45381440375f08e68.jpg

But this is processed with an error:
525Kb

7c101cef06d5cdbc23f012b97b091cdf.png
8249731bfe1cd3290e311d4372c8bc32.jpg

To convert I use the following code:
$h = new СImageHandler();
$h->load('source.png')->save('res.jpg', $h::IMG_JPEG, 90);

Tell me why this can happen? And how can you fight it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tesla, 2014-01-09
@HaruAtari

It's all about saving png in some editors www.artlebedev.ru/tools/technogrette/img/png-3 Try gluing translucent images with a homogeneous background before saving to jpg.

V
Vit, 2014-01-09
@fornit1917

GD is a strange thing. Try php5-imagick. It works faster, more stable and you need to write very little code with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question