PHP實現(xiàn)圖片旋轉的方法詳解
最近有一個需求需要將前端上傳過來的圖片進行逆時針旋轉90°,這個主要需要使用到php的imagerotate方法對于圖片進行旋轉,具體實現(xiàn)方法如下:
<?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); //創(chuàng)建新圖 $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('本地圖片地址', '旋轉角度', '保存地址');
根據(jù)上面的方法我們就可以實現(xiàn)圖片的旋轉功能了
到此這篇關于PHP實現(xiàn)圖片旋轉的方法詳解的文章就介紹到這了,更多相關PHP圖片旋轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
用PHP將網(wǎng)址字符串轉換成超鏈接(網(wǎng)址或email)
該函數(shù)將 URL 和 E-mail 地址字符串轉換為可點擊的超級鏈接。2010-05-05PHP基于GD2函數(shù)庫實現(xiàn)驗證碼功能示例
這篇文章主要介紹了PHP基于GD2函數(shù)庫實現(xiàn)驗證碼功能,簡單介紹了GD2函數(shù)庫的常用函數(shù),并結合實例形式分析了php實現(xiàn)驗證碼功能相關操作技巧,需要的朋友可以參考下2019-01-01php中current、next與reset函數(shù)用法實例
這篇文章主要介紹了php中current、next與reset函數(shù)用法,以實例形式詳細講述了PHP中針對數(shù)組操作的函數(shù)current、next與reset的具體用法,對于深入了解數(shù)組的用法具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11PHP使用debug_backtrace方法跟蹤調試代碼調用詳解
這篇文章主要介紹了PHP使用debug_backtrace方法跟蹤調試代碼調用,結合實例形式詳細分析了debug_backtrace函數(shù)的功能、參數(shù)、使用方法及相關操作注意事項,需要的朋友可以參考下2018-07-07PHP 開發(fā)環(huán)境配置(測試開發(fā)環(huán)境)
PHP發(fā)開環(huán)境配置(測試開發(fā)環(huán)境),測試環(huán)境是否配置的正確。2010-04-04探討多鍵值cookie(php中cookie存取數(shù)組)的詳解
本篇文章是對多鍵值cookie(php中cookie存取數(shù)組)進行了詳細的分析介紹,需要的朋友參考下2013-06-06