亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2022年11月03日 14:59:55   作者:huaweichenai  
這篇文章主要為大家詳細介紹了PHP如何實現(xiàn)圖片旋轉功能,文中的示例代碼講解詳細,對我們學習PHP有一定幫助,感興趣的小伙伴可以了解一下

最近有一個需求需要將前端上傳過來的圖片進行逆時針旋轉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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論