PHP實(shí)現(xiàn)圖片旋轉(zhuǎn)的方法詳解
最近有一個(gè)需求需要將前端上傳過來的圖片進(jìn)行逆時(shí)針旋轉(zhuǎn)90°,這個(gè)主要需要使用到php的imagerotate方法對于圖片進(jìn)行旋轉(zhuǎn),具體實(shí)現(xiàn)方法如下:
<?php
namespace common\traits;
use Yii;
use yii\helpers\FileHelper;
/**
* 圖片旋轉(zhuǎn)處理trait
*
* @author wangjian
* @since 1.0
*/
class ImageRotate
{
/**
* base64圖片旋轉(zhuǎn)
* @param $image 需要旋轉(zhuǎn)的base64圖片
* @param string $rotate 逆時(shí)針旋轉(zhuǎn)角度
* @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];
//設(shè)置臨時(shí)目錄
$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); //旋轉(zhuǎn)圖片
//刪除臨時(shí)文件
@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;
}
/**
* 本地圖片旋轉(zhuǎn)
* @param $image 需要旋轉(zhuǎn)的本地圖片
* @param string $rotate 逆時(shí)針旋轉(zhuǎn)角度
* @param false $savePath 保存的圖片路徑,false返回替換原圖
*/
public static function imageRotate($image, $rotate = '90', $savePath = false)
{
if (empty($image)) {
return false;
}
//旋轉(zhuǎn)圖片
$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 需要旋轉(zhuǎn)的圖片
* @param $rotate 逆時(shí)針旋轉(zhuǎn)角度
*/
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); //逆時(shí)針旋轉(zhuǎn)
//獲取旋轉(zhuǎn)后的寬高
$srcWidth = imagesx($rotateImage);
$srcHeight = imagesy($rotateImage);
//創(chuàng)建新圖
$newImage = imagecreatetruecolor($srcWidth, $srcHeight);
//分配顏色 + alpha,將顏色填充到新圖上
$alpha = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
imagefill($newImage, 0, 0, $alpha);
//將源圖拷貝到新圖上,并設(shè)置在保存 PNG 圖像時(shí)保存完整的 alpha 通道信息
imagecopyresampled($newImage, $rotateImage, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
imagesavealpha($newImage, true);
return $newImage;
}
}
具體使用:
1:base64圖片旋轉(zhuǎn)并輸出base64
ImageRotate::base64Rotate('base64圖片', '旋轉(zhuǎn)角度');
2:base64圖片旋轉(zhuǎn)并保存
ImageRotate::base64Rotate('base64圖片', '旋轉(zhuǎn)角度', '保存地址');
3:本地圖片旋轉(zhuǎn)
ImageRotate::imageRotate('本地圖片地址', '旋轉(zhuǎn)角度', '保存地址');
根據(jù)上面的方法我們就可以實(shí)現(xiàn)圖片的旋轉(zhuǎn)功能了
到此這篇關(guān)于PHP實(shí)現(xiàn)圖片旋轉(zhuǎn)的方法詳解的文章就介紹到這了,更多相關(guān)PHP圖片旋轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Laravel中使用FormRequest進(jìn)行表單驗(yàn)證方法及問題匯總
Laravel 5.0 帶來了FormRequests, 這是一種特殊的類型, 用于在提交表單時(shí)進(jìn)行數(shù)據(jù)的檢查和驗(yàn)證. 每個(gè)FormRequest類至少包含一個(gè)rules()方法, 這個(gè)方法返回一組驗(yàn)證規(guī)則. 除此之外還必須包含一個(gè)authorize()方法, 該方法返回一個(gè)布爾值, 代表是否允許用戶執(zhí)行本次請求.2016-06-06
利用PHP擴(kuò)展vld查看PHP opcode操作步驟
首先下載最新版vld擴(kuò)展接下來編譯安裝vld擴(kuò)展最后將生成的vld.so復(fù)制到extension_dir目錄下然后修改php.ini文件接下來創(chuàng)建test.php文件,感興趣的你可以參考下本文2013-03-03
PHP數(shù)據(jù)庫操作四:mongodb用法分析
這篇文章主要介紹了PHP數(shù)據(jù)庫操作mongodb用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MongoDB的功能、安裝、基本命令、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
php頁碼形式分頁函數(shù)支持靜態(tài)化地址及ajax分頁
這篇文章主要介紹了php頁碼形式分頁函數(shù),此分頁支持靜態(tài)化地址分頁和無鏈接地址時(shí)的ajax分頁,需要的朋友可以參考下2014-03-03
PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫操作基類
這篇文章主要介紹了PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫操作基類,涉及PHP操作數(shù)據(jù)庫的基本配置與增刪改查等操作技巧,需要的朋友可以參考下2016-01-01
PHP實(shí)現(xiàn)瀏覽器中直接輸出圖片的方法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)瀏覽器中直接輸出圖片的方法,結(jié)合實(shí)例形式分析了php輸出圖片的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
如何使用Laravel Eloquent來開發(fā)無限極分類
在網(wǎng)上商城上,我們經(jīng)??梢钥吹蕉嗉壏诸?、子分類、甚至無限極分類。本文將向你展示如何優(yōu)雅的通過 Laravel Eloquent 將其實(shí)現(xiàn)。2021-05-05

