2
votes

When i resample a jpg in order to draw border around the image, the quality of image is horribly decreases and extremely low quality jpeg is gotten. enter image description here

Here is my code:

function addBorderpng($add,$bdr=0,$color='#000000'){
                $arr = explode('.', $add);
                $extension = strtolower(end($arr));
                $border=$bdr;

$im=imagecreatefromjpeg($add);

                $width=imagesx($im);
                $height=imagesy($im);
                $img_adj_width=$width+(2*$border);
                $img_adj_height=$height+(2*$border);
                $newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);

                imageantialias($newimage, true);
                $color_gb_temp =HexToRGB($color);
                $border_color = imagecolorallocate($newimage, $color_gb_temp['r'], $color_gb_temp['g'], $color_gb_temp['b']);
                imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);
                imagealphablending($newimage, true);
                imageantialias($newimage, true);
                     imagecopyresized($newimage,$im,$border,$border,0,0,$width,$height,$width,$height);
                    imagejpeg($newimage,$add,9);
            }
2

2 Answers

5
votes

The last argument to imagejpeg is quality.

quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).

You are setting it at 9, hence low quality.

1
votes

Also, you can try "imagecopyresampled" in place of "imagecopyresized" which handles interpolationPHP imagecopyresampled