How to convert php jpg to png picture

藏色散人
Release: 2023-03-11 17:26:01
Original
3098 people have browsed it

How to convert php jpg to png image: first install the php extension gd and exif; then use the "function transform_image($image_path, $to_ext = 'png'...){...}" method Just convert.

How to convert php jpg to png picture

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP simply realizes image format conversion (jpg to png, gif to png, etc.)

Requirements

During the development process, there will always be some requirements that require conversion of image formats. For example, gif to png, jpg to png

If you have recently used the image file recognition of a certain platform and it does not support the gif format, then you need to process the gif into png, etc.

Dependency

php extends gd and exif

implementation

/**
 * 图片格式转换
 * @param string $image_path 文件路径或url
 * @param string $to_ext 待转格式,支持png,gif,jpeg,wbmp,webp,xbm
 * @param null|string $save_path 存储路径,null则返回二进制内容,string则返回true|false
 * @return boolean|string $save_path是null则返回二进制内容,是string则返回true|false
 * @throws Exception 
 * @author klinson <klinson@163.com>
 */
function transform_image($image_path, $to_ext = &#39;png&#39;, $save_path = null)
{
  if (! in_array($to_ext, [&#39;png&#39;, &#39;gif&#39;, &#39;jpeg&#39;, &#39;wbmp&#39;, &#39;webp&#39;, &#39;xbm&#39;])) {
    throw new \Exception(&#39;unsupport transform image to &#39; . $to_ext);
  }
  switch (exif_imagetype($image_path)) {
    case IMAGETYPE_GIF :
      $img = imagecreatefromgif($image_path);
      break;
    case IMAGETYPE_JPEG :
    case IMAGETYPE_JPEG2000:
      $img = imagecreatefromjpeg($image_path);
      break;
    case IMAGETYPE_PNG:
      $img = imagecreatefrompng($image_path);
      break;
    case IMAGETYPE_BMP:
    case IMAGETYPE_WBMP:
      $img = imagecreatefromwbmp($image_path);
      break;
    case IMAGETYPE_XBM:
      $img = imagecreatefromxbm($image_path);
      break;
    case IMAGETYPE_WEBP: //(从 PHP 7.1.0 开始支持)
      $img = imagecreatefromwebp($image_path);
      break;
    default :
      throw new \Exception(&#39;Invalid image type&#39;);
  }
  $function = &#39;image&#39;.$to_ext;
  if ($save_path) {
    return $function($img, $save_path);
  } else {
    $tmp = __DIR__.&#39;/&#39;.uniqid().&#39;.&#39;.$to_ext;
    if ($function($img, $tmp)) {
      $content = file_get_contents($tmp);
      unlink($tmp);
      return $content;
    } else {
      unlink($tmp);
      throw new \Exception(&#39;the file &#39;.$tmp.&#39; can not write&#39;);
    }
  }
}
Copy after login

use

// 转换后保存在test.png
transform_image($url, &#39;png&#39;, &#39;./test.png&#39;);
transform_image($filepath, &#39;png&#39;, &#39;./test.png&#39;);
// 转换后二进制结果直接返回
transform_image($url, &#39;png&#39;);
transform_image($filepath, &#39;png&#39;);
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php jpg to png picture. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!