PHP将图片转换为png格式

PHP将图片转换为png格式

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
</head>

<body>
    <form action="test.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/>
        <input type="submit" name="submit"/>
        <input type="button" value="清空" onclick="window.location.href='test.php'"/>
        <br/>
        <?php
        $url = $_GET['url'];
            if(!empty($url)&& file_exists('test/'.$url)){
                echo "<a href='test/$url' download='$url' title='仅支持谷歌下载'>$url</a>";
            }
        ?>
    </form>
</body>
</html>

<?php
if(!empty($_FILES['file'])){
    // 存储原始图片
    require_once('function/uploadUtil.php');
    $upload = new upload();
    $upload->uploadPath = 'test/tmp';
    $arr = $upload->uploadFile();
    // 格式转换
    $image_path = 'test/tmp/'.$arr['msg'];
    $save_path = substr($arr['msg1'], 0, strpos($arr['msg1'], ".")) . '.png';

    if (preg_match("/[\x7f-\xff]/", $save_path)) {
        //路径中有中文
        $save_path = substr($arr['msg'], 0, strpos($arr['msg'], ".")) . '.png';
    }
    transform_image($image_path, 'png', 'test/' . $save_path);
    // 删除旧图片
    @unlink('test/tmp/'.$arr['msg']);
    $save_path = htmlspecialchars ($save_path);

    echo "<script>window.location.href='test.php?url=$save_path'</script>";
}

/**
* 图片格式转换 https://www.php.cn/php-ask-458661.html
* @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
*/
function transform_image($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');
        }
    }
}
?>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值