源码论坛公告:本站是一个交流学习建站资源的社区论坛,旨在交流学习源码脚本等资源技术,欢迎大家投稿发言! 【点击此处将锦尚放在桌面

源码论坛,商业源码下载,尽在锦尚中国商业源码论坛

 找回密码
 会员注册

QQ登录

只需一步,快速开始

查看: 5457|回复: 0
打印 上一主题 下一主题

[PHP编程] PHP实现图片旋转的方法详解

[复制链接]

3102

主题

3503

帖子

13万

金币

超级版主

Rank: 8Rank: 8

积分
271670
跳转到指定楼层
1#
发表于 2023-1-1 17:44:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这篇文章主要为大家详细介绍了PHP如何实现图片旋转功能,文中的示例代码讲解详细,对我们学习PHP有一定帮助,感兴趣的小伙伴可以了解一下


最近有一个需求需要将前端上传过来的图片进行逆时针旋转90°,这个主要需要使用到php的imagerotate方法对于图片进行旋转,具体实现方法如下:
  1. <?php

  2. namespace common\traits;

  3. use Yii;
  4. use yii\helpers\FileHelper;

  5. /**
  6. * 图片旋转处理trait
  7. *
  8. * @author wangjian
  9. * @since 1.0
  10. */
  11. class ImageRotate
  12. {

  13.     /**
  14.      * base64图片旋转
  15.      * @param $image 需要旋转的base64图片
  16.      * @param string $rotate 逆时针旋转角度
  17.      * @param false $savePath 保存的图片路径,false返回base64格式
  18.      */
  19.     public static function base64Rotate($image, $rotate = '90', $savePath = false)
  20.     {
  21.         if (empty($image)) {
  22.             return false;
  23.         }
  24.         if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image, $result)) {
  25.             $type = $result[2];
  26.             //设置临时目录
  27.             $temporaryPath = '/tmp/';
  28.             $temporaryPath = dirname(Yii::getAlias('@common')) . '/web' . $temporaryPath;
  29.             FileHelper::createDirectory($temporaryPath);

  30.             //将原图保存到零食目录
  31.             $temporaryImage = date('YmdHis') . rand(1000, 9999) . '.' . $type;
  32.             if (file_put_contents($temporaryPath . $temporaryImage, base64_decode(str_replace($result[1], '', $image)))) {
  33.                 $newImage = self::rotateImage($temporaryPath . $temporaryImage, $rotate); //旋转图片
  34.                 //删除临时文件
  35.                 @unlink($temporaryPath . $temporaryImage);

  36.                 ob_start();
  37.                 if ($savePath === false) { //返回base
  38.                     imagepng($newImage);
  39.                     $imageString = $result[1] . base64_encode(ob_get_contents());
  40.                     @unlink($newImage);
  41.                 } else {
  42.                     $imageString = imagepng($newImage, $savePath);
  43.                 }
  44.                 ob_end_clean();

  45.                 return $imageString;
  46.             }
  47.         }

  48.         return false;
  49.     }

  50.     /**
  51.      * 本地图片旋转
  52.      * @param $image 需要旋转的本地图片
  53.      * @param string $rotate 逆时针旋转角度
  54.      * @param false $savePath 保存的图片路径,false返回替换原图
  55.      */
  56.     public static function imageRotate($image, $rotate = '90', $savePath = false)
  57.     {
  58.         if (empty($image)) {
  59.             return false;
  60.         }
  61.         //旋转图片
  62.         $newImage = self::rotateImage($image, $rotate);
  63.         ob_start();
  64.         if ($savePath === false) {
  65.             //替换原图
  66.             $url = $image;
  67.         } else {
  68.             $url = $savePath;
  69.         }
  70.         $imageString = imagepng($newImage, $url);
  71.         ob_end_clean();
  72.         return $imageString;
  73.     }

  74.     /**
  75.      * @param $file 需要旋转的图片
  76.      * @param $rotate 逆时针旋转角度
  77.      */
  78.     private static function rotateImage($file, $rotate)
  79.     {
  80.         $imageSize = getimagesize($file);
  81.         $imageSize = explode('/', $imageSize['mime']);
  82.         $type = $imageSize[1];

  83.         switch ($type) {
  84.             case "png":
  85.                 $image = imagecreatefrompng($file);
  86.                 break;
  87.             case "jpeg":
  88.                 $image = imagecreatefromjpeg($file);
  89.                 break;
  90.             case "jpg":
  91.                 $image = imagecreatefromjpeg($file);
  92.                 break;
  93.             case "gif":
  94.                 $image = imagecreatefromgif($file);
  95.                 break;
  96.         }
  97.         $rotateImage = imagerotate($image, $rotate, 0); //逆时针旋转
  98.         //获取旋转后的宽高
  99.         $srcWidth = imagesx($rotateImage);
  100.         $srcHeight = imagesy($rotateImage);
  101.         //创建新图
  102.         $newImage = imagecreatetruecolor($srcWidth, $srcHeight);
  103.         //分配颜色 + alpha,将颜色填充到新图上
  104.         $alpha = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
  105.         imagefill($newImage, 0, 0, $alpha);
  106.         //将源图拷贝到新图上,并设置在保存 PNG 图像时保存完整的 alpha 通道信息
  107.         imagecopyresampled($newImage, $rotateImage, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
  108.         imagesavealpha($newImage, true);

  109.         return $newImage;
  110.     }

  111. }
复制代码

具体使用:

1:base64图片旋转并输出base64
  1. ImageRotate::base64Rotate('base64图片', '旋转角度');
复制代码

2:base64图片旋转并保存
  1. ImageRotate::base64Rotate('base64图片', '旋转角度', '保存地址');
复制代码

3:本地图片旋转
  1. ImageRotate::imageRotate('本地图片地址', '旋转角度', '保存地址');
复制代码


根据上面的方法我们就可以实现图片的旋转功能了

到此这篇关于PHP实现图片旋转的方法详解的文章就介绍到这了,更多相关PHP图片旋转内容请搜索锦尚中国源码论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持锦尚中国源码论坛!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

锦尚中国源码论坛

聚合标签|锦尚中国,为中国网站设计添动力 ( 鲁ICP备09033200号 ) |网站地图

GMT+8, 2024-5-1 12:30 , Processed in 0.026808 second(s), 16 queries .

带宽由 锦尚数据 提供 专业的数据中心

© 锦尚中国源码论坛 52jscn Inc. 非法入侵必将受到法律制裁 法律顾问:IT法律网 & 褚福省律师 锦尚爱心 版权申诉 版权与免责声明