您正在使用 IPV4 [18.232.187.47] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
悟空收录网       [公益]保护绿色环境,构建和谐社会      

【腾讯云】 爆款2核2G3M云服务器首年 61元,叠加红包再享折上折      
[公益] 地球是我家,绿化靠大家      
2024年 清明节 006
2024年 劳动节 033
2025年 元 旦 278
2025年 春 节 306
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP实现图片旋转的方法详解
内容摘要: ?phpnamespacecommon\traits;useYii;useyii\helpers\FileHelper;/***图片旋转处理trait**@authorwangjian*@since1.0*/classImageRotate{/***base64图片旋转*@param$image需要旋转的base64图片*@paramstring$rotat......
<?php

namespacecommon\traits;

useYii;

useyii\helpers\FileHelper;

/**

*图片旋转处理trait

*

*@authorwangjian

*@since1.0

*/

classImageRotate

{

/**

*base64图片旋转

*@param$image需要旋转的base64图片

*@paramstring$rotate逆时针旋转角度

*@paramfalse$savePath保存的图片路径,false返回base64格式

*/

publicstaticfunctionbase64Rotate($image,$rotate='90',$savePath=false)

{

if(empty($image)){

returnfalse;

}

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;

}

}

returnfalse;

}

/**

*本地图片旋转

*@param$image需要旋转的本地图片

*@paramstring$rotate逆时针旋转角度

*@paramfalse$savePath保存的图片路径,false返回替换原图

*/

publicstaticfunctionimageRotate($image,$rotate='90',$savePath=false)

{

if(empty($image)){

returnfalse;

}

//旋转图片

$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逆时针旋转角度

*/

privatestaticfunctionrotateImage($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图片','旋转角度');

ImageRotate::base64Rotate('base64图片','旋转角度');

2:base64图片旋转并保存

ImageRotate::base64Rotate('base64图片','旋转角度','保存地址');

3:本地图片旋转

ImageRotate::imageRotate('本地图片地址','旋转角度','保存地址');

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:php substr()去掉最后一位字符的实例方法

 

下一篇:php旋转图片90度的方法

发布日期:2023/2/20
手机扫二维码直达本页
发布时间:15:39:21
点  击:12
录  入:壹家怡园
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,Cloudflare提供加速防护
运行时间载入中.....