本教程操作环境:windows7系统、php8.1版、dell g3电脑。
php图片旋转方向代码是什么?
php实现图片旋转
最近有一个需求需要将前端上传过来的图片进行逆时针旋转90°,这个主要需要使用到php的imagerotate方法对于图片进行旋转,具体实现方法如下:
<?php namespace common\traits; use yii;use yii\helpers\filehelper; /** * 图片旋转处理trait * * @author wangjian * @since 1.0 */class imagerotate{ /** * base64图片旋转 * @param $image 需要旋转的base64图片 * @param string $rotate 逆时针旋转角度 * @param false $savepath 保存的图片路径,false返回base64格式 */ public static function base64rotate($image, $rotate = '90', $savepath = false) { if (empty($image)) { return false; } if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image, $result)) { $type = $result[2]; //设置临时目录 $temporarypath = '/tmp/'; $temporarypath = dirname(yii::getalias('@common')) . '/web' . $temporarypath; filehelper::createdirectory($temporarypath); //将原图保存到零食目录 $temporaryimage = date('ymdhis') . rand(1000, 9999) . '.' . $type; if (file_put_contents($temporarypath . $temporaryimage, base64_decode(str_replace($result[1], '', $image)))) { $newimage = self::rotateimage($temporarypath . $temporaryimage, $rotate); //旋转图片 //删除临时文件 @unlink($temporarypath . $temporaryimage); ob_start(); if ($savepath === false) { //返回base imagepng($newimage); $imagestring = $result[1] . base64_encode(ob_get_contents()); @unlink($newimage); } else { $imagestring = imagepng($newimage, $savepath); } ob_end_clean(); return $imagestring; } } return false; } /** * 本地图片旋转 * @param $image 需要旋转的本地图片 * @param string $rotate 逆时针旋转角度 * @param false $savepath 保存的图片路径,false返回替换原图 */ public static function imagerotate($image, $rotate = '90', $savepath = false) { if (empty($image)) { return false; } //旋转图片 $newimage = self::rotateimage($image, $rotate); ob_start(); if ($savepath === false) { //替换原图 $url = $image; } else { $url = $savepath; } $imagestring = imagepng($newimage, $url); ob_end_clean(); return $imagestring; } /** * @param $file 需要旋转的图片 * @param $rotate 逆时针旋转角度 */ private static function rotateimage($file, $rotate) { $imagesize = getimagesize($file); $imagesize = explode('/', $imagesize['mime']); $type = $imagesize[1]; switch ($type) { case "png": $image = imagecreatefrompng($file); break; case "jpeg": $image = imagecreatefromjpeg($file); break; case "jpg": $image = imagecreatefromjpeg($file); break; case "gif": $image = imagecreatefromgif($file); break; } $rotateimage = imagerotate($image, $rotate, 0); //逆时针旋转 //获取旋转后的宽高 $srcwidth = imagesx($rotateimage); $srcheight = imagesy($rotateimage); //创建新图 $newimage = imagecreatetruecolor($srcwidth, $srcheight); //分配颜色 + alpha,将颜色填充到新图上 $alpha = imagecolorallocatealpha($newimage, 0, 0, 0, 127); imagefill($newimage, 0, 0, $alpha); //将源图拷贝到新图上,并设置在保存 png 图像时保存完整的 alpha 通道信息 imagecopyresampled($newimage, $rotateimage, 0, 0, 0, 0, $srcwidth, $srcheight, $srcwidth, $srcheight); imagesavealpha($newimage, true); return $newimage; } }
具体使用:
1:base64图片旋转并输出base64
imagerotate::base64rotate('base64图片', '旋转角度');
2:base64图片旋转并保存
imagerotate::base64rotate('base64图片', '旋转角度', '保存地址');
3:本地图片旋转
imagerotate::imagerotate('本地图片地址', '旋转角度', '保存地址');
根据上面的方法我们就可以实现图片的旋转功能了
推荐学习:《php视频教程》
以上就是php图片旋转方向代码是什么的详细内容。
