I encountered one problem while trying to convert my image to PHP.
First, what I do, I have different image formats, I need to resize them according to the container where it will be used, for example, in a slide show.
Honestly, this is a standard implementation of the OpenCart engine.
What is wrong with this? When I resize the png image, everything seems to work as a perfect, transparent background ....
But when I convert JPEG images, I get a background, by default the rectangle is filled with white. I clearly understand that it is not possible to use jpeg with a transparent background due to the format algorithm and specification.
I want to have. I want to change JPEG images with a transparent background (rectangle), of course, a JPEG image cannot be transparent, but I just want to make a rectangle (the size of which is set according to the container) transparent.
Perhaps convert JPEG to format, draw a transparent rectangle, place JPEG on this rectangle and save it in png or another format that allows you to use a transparent background.
Here is my code (not mine, but by default).
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, 255, 255, 255);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
imagedestroy($image_old);
I tried to find solutions, read manuals and documentation, but all my attempts failed. (black background, poor quality, but still filled background)
Please help get the desired result.
Thank you all for your help and advice.