Maison  >  Article  >  développement back-end  >  Comment implémenter la conversion de format d'image en php

Comment implémenter la conversion de format d'image en php

coldplay.xixi
coldplay.xixioriginal
2020-08-24 13:36:413607parcourir

Comment convertir le format d'image en PHP : convertissez d'abord et enregistrez dans [test.png], le code est [transform_image($url, 'png', './test.png')] puis convertissez; Le résultat binaire est renvoyé directement.

Comment implémenter la conversion de format d'image en php

[Recommandations d'apprentissage associées : programmation php (vidéo)]

images d'implémentation php Méthode de conversion de format :

Au cours du processus de développement, vous rencontrerez toujours certains besoins qui vous obligent à convertir 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

pour implémenter

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

en utilisant

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

[Recommandations d'apprentissage associées : diagramme php Tutoriel texte

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