温馨提示  2024年 6月我们已经停止开发者板块文章内容更新,谢谢来访。存档数据
知识 2023-08-29 29 次阅读

php image类型怎么实现转换

   

php image类型实现转换的方法 1. 创建一个php示例文件; 2. 通过“function image,change($image,path, $to,ext = 'png',$save,path=null){...}”方法实现转换即可。

本文操作环境windows7系统,php7.1版,dell g3电脑。

php image类型怎么实现转换?

php图片格式转换方法

/** * 图片格式转换 * @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 */function image,change($image,path, $to,ext = 'png', $save,path = null){ if (!in,array($to,ext, ['png', 'gif', 'jpeg', 'wbmp', 'webp', 'xbm'])) { throw new \\exception('unsupport transform image to ' . $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('invalid image type'); } $function = 'image' . $to,ext; if ($save,path) { return $function($img, $save,path); } else { $tmp = ,,dir,, . '/' . uniqid() . '.' . $to,ext; if ($function($img, $tmp)) { $content = file,get,contents($tmp); unlink($tmp); return $content; } else { unlink($tmp); throw new \\exception('the file ' . $tmp . ' can not write'); } }}