How to convert PNG to JPG in PHP?

Guanhui
Release: 2023-03-01 15:16:02
Original
4158 people have browsed it

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 "转换失败!";
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template