Maison  >  Article  >  développement back-end  >  Comment convertir le type d'image php

Comment convertir le type d'image php

藏色散人
藏色散人original
2020-10-10 10:06:312293parcourir

Comment implémenter la conversion de type d'image PHP : installez d'abord les extensions PHP gd et exif ; puis implémentez la conversion de type d'image via la méthode "transform_image" et enfin renvoyez le résultat de la conversion.

Comment convertir le type d'image php

Recommandé : "Tutoriel vidéo PHP"

Exigences

Il y aura toujours des rencontres pendant le processus de développement Certaines exigences nécessitent la conversion des formats d'image. Par exemple, convertissez un gif en png, convertissez un jpg en png

Si vous avez récemment utilisé la reconnaissance de fichier image d'une certaine plate-forme et qu'elle ne prend pas en charge le format gif, vous devez alors traiter le gif en png, etc.

Dépend de

extension php gd et exif

implémentation

/**
 * 图片格式转换
 * @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;);
    }
  }
}

utilisations

// 转换后保存在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;);

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn