Home > Article > Backend Development > How to convert PNG to JPG in PHP?
How to convert PNG to JPG with PHP?
First get the path of the PNG image to be converted to JPG; then use the "imagecreatefrompng()" function and create a PNG image source based on the path; finally use the function "imagejpeg()" to convert the PNG image source Just convert it to JPG.
$pngfile = "./user.png"; $res = imagecreatefrompng($pngfile); $res = imagejpeg($res, 'user.jpg'); if ($res) { echo "转换成功!"; } else { echo "转换失败!"; }
PHP GD library
GD library is an extension library for PHP to process graphics. The GD library provides a series of API used to process images. Use the GD library to process images, generate images, or add watermarks to images.
On the website, the GD library is usually used to generate thumbnails, or to add watermarks to pictures, or to generate Chinese character verification codes, or to generate reports on website data, etc. To process images in PHP, you can use the GD library. The GD library initially supported GIF. However, because GIF used the copyright-controversial LZW algorithm, it would cause legal issues, so starting from GD library version 1.6, all GIF support was moved. Except, but it was added back in GD library version 2.0.28. GIF related functions are not available when using an intermediate version of the GD library.
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of How to convert PNG to JPG in PHP?. For more information, please follow other related articles on the PHP Chinese website!