亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

PHP實(shí)現(xiàn)圖片旋轉(zhuǎn)的方法詳解

瀏覽:164日期:2022-06-05 18:59:05

最近有一個(gè)需求需要將前端上傳過來的圖片進(jìn)行逆時(shí)針旋轉(zhuǎn)90°,這個(gè)主要需要使用到php的imagerotate方法對(duì)于圖片進(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)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: PHP
主站蜘蛛池模板: 999国产精品视频 | 国产剧情麻豆mv在线观看 | 国产美女网址 | 自偷自拍亚洲欧美清纯唯美 | 亚洲欧美国产精品专区久久 | 国产一级特黄毛片 | 一级特黄国产高清毛片97看片 | 精品欧美一区二区vr在线观看 | 美国毛片网 | 免费高清欧美大片在线观看 | 国产成人在线视频免费观看 | 亚洲国产精品a在线 | 亚洲黄v | 视频在线观看一区 | 国产成人一区二区三区高清 | 欧美黄色影院 | 国产久视频观看 | 一级片在线免费 | 久久国产精品免费网站 | 美国黄色一级片 | 日韩精品欧美亚洲高清有无 | 国产精品亚洲专区在线播放 | 国内成人精品视频 | 三级国产在线观看 | 久久黄色大片 | 国产亚洲精品久久久久久午夜 | 欧美搞黄视频 | 久草免费色站 | 欧美三级一区二区 | 日本欧美高清 | 一级做人爰a全过程免费视频 | 全免费a级毛片免费看不卡 全免费a级毛片免费看视频免 | 亚洲国产资源 | 92国产福利视频一区二区 | 欧美亚洲综合在线 | 免费人成年短视频在线观看免费网站 | 亚洲视频在线免费 | 国产成人免费网站在线观看 | 欧美在线性 | 99国产精品高清一区二区二区 | 欧美日韩国产高清一区二区三区 |