欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于PHP封裝圖片裁剪工具類

 更新時間:2024年04月12日 14:13:29   作者:田小濤  
這篇文章主要為大家詳細介紹了如何基于PHP封裝圖片裁剪工具類,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下

PHP工具類圖片裁剪類封裝

<?php
namespace App\Utils;
 
 
 
/**
 * 圖片裁剪工具類
 * @author	田小濤
 * @date	2020年7月23日
 * @comment
 *
 */
class ImageCropUtils
{
    
    private $sImage;
    private $dImage;
    private $src_file;
    private $dst_file;
    private $src_width;
    private $src_height;
    private $src_ext;
    private $src_type;
    private $mime;
    
    //上傳基礎路徑處
    private $basisUploadPath;
    
    public function __construct( $file = null, $distFile = null )
    {
        $this->dst_file = $distFile;
        $this->basisUploadPath = storage_path( 'app/uploads/' );
        if( isset( $file ) && $file )
        {
            $this->src_file = $file;
            $this->init( $file );
        }
    }
 
    
    /**
     * 生成唯一的文件名稱
     * @author	 Administrator
     * @datetime 2019年12月24日 上午11:44:02
     * @comment	
     * 
     * @param string $salt
     * @return string
     */
    protected function getUniqueDiskName($salt = '')
    {
        if( empty( $salt ) )
        {
            $salt = mt_rand(1,1000000);
        }
        list($usec, $sec) = explode(" ", microtime());
        
        $micros = str_replace('.', '', ((float)$usec + (float)$sec)).$salt;
        
        return md5( $micros );
    }
    
    
    /**
     * 初始化參數(shù)
     * @author	 Administrator
     * @datetime 2020年7月22日 下午3:00:22
     * @comment	
     * 
     * @return boolean
     */
    public function init( $url )
    {
        $strExt = $this->getImgExt( $url );
        $filename = $this->getUniqueDiskName( md5( $url ) );
        $path = date( 'y' ) . '/' . date( 'm' ) . '/' . date( 'd' ) . '/' . $filename;
        
        $dowRes = new \generalDowmload( $url,  $path . $strExt );
        if( !empty( $dowRes ) && isset( $dowRes->basename ) )
        {
            if( isset( $dowRes->location ) && strlen( $dowRes->location ) )
            {
                $this->src_file = $dowRes->location;
            }
            else
            {
                $this->src_file = $this->basisUploadPath .  $path . $strExt;
            }
        }
        else
        {
            return false;
        }
        if( !isset( $this->src_file ) || is_null( $this->src_file ) || !file_exists( $this->src_file ) )
        {
            return false;
        }
        
        $arrImageInfos = @getimagesize( $this->src_file );
        if( !isset( $arrImageInfos ) || empty( $arrImageInfos ) )
        {
            return false;
        }
        if( isset( $arrImageInfos[0] ) && $arrImageInfos[0] )
        {
            $this->src_width    = $arrImageInfos[0];
        }
        if( isset( $arrImageInfos[1] ) && $arrImageInfos[1] )
        {
            $this->src_height   = $arrImageInfos[1];
        }
        $this->src_type = 2;
        if( isset( $arrImageInfos[2] ) && $arrImageInfos[2] )
        {
            $this->src_type = $arrImageInfos[2];
        }
        if( isset( $arrImageInfos[ 'mime' ] ) )
        {
            $this->mime     = $arrImageInfos[ 'mime' ];
        }
        switch( $this->src_type ) 
        {
            case IMAGETYPE_JPEG :
                ini_set( 'gd.jpeg_ignore_warning', true );
                $this->sImage   =  @imagecreatefromjpeg( $this->src_file );
                $this->ext      =  'jpg';
                if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 )
                {
                    $this->mime = 'image/jpeg';
                }
            break;
            case IMAGETYPE_PNG :
                $this->sImage   =  @imagecreatefrompng( $this->src_file );
                $this->ext      =  'png';
                if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 )
                {
                    $this->mime = 'image/' . $this->ext;
                }
            break;
            case IMAGETYPE_GIF :
                $this->sImage   =  imagecreatefromgif( $this->src_file );
                $this->ext      =  'gif';
                if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 )
                {
                    $this->mime = 'image/' . $this->ext;;
                }
            break;
            case 18:
                $this->sImage   = @imagecreatefromwebp( $this->src_file );
                $this->ext      =  'webp';
                if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 )
                {
                    $this->mime = 'image/' . $this->ext;;
                }
            break;
            default:
                return false;
        }
        
        return true;
    }
    
    
    
    /**
     * 裁剪
     * @author	 Administrator
     * @datetime 2020年7月22日 下午3:07:36
     * @comment	
     * 
     * @param unknown $dst_width
     * @param unknown $dst_height
     * @param unknown $dst_x
     * @param unknown $dst_y
     * @param string $dst_file
     * @return boolean
     */
    public function cutImage( $dst_width, $dst_height, $dst_x, $dst_y, $originWidth, $originHeight )
    {
        if( !$dst_width || !$dst_height )
        {
            return false;
        }
 
        # 創(chuàng)建畫布時,判斷最終需要的畫布大小
        if ($originWidth && $originHeight)
        {
            $dst_w = $originWidth;
            $dst_h = $originHeight;
        } 
        else
        {
            $dst_w = $dst_width;
            $dst_h = $dst_height;
        }
 
        $this->dImage = imagecreatetruecolor( $dst_w, $dst_h ); //創(chuàng)建了目標文件的大小的畫布
        $bg = imagecolorallocatealpha( $this->dImage, 255, 255, 255, 127 ); //給畫布分配顏色
        imagefill( $this->dImage, 0, 0, $bg ); //給圖像用顏色進行填充
        imagecolortransparent( $this->dImage, $bg ); //背景定義成透明色
        
        $ratio_w = 1.0 * $dst_width / $this->src_width; //橫向縮放的比例
        $ratio_h = 1.0 * $dst_height / $this->src_height; //縱向縮放的比例
        //不進行縮放,直接對圖像進行裁剪
        $ratio = 1.0;
        $tmp_w = (int)($dst_width / $ratio);
        $tmp_h = (int)($dst_height / $ratio);
        
        $tmp_img = imagecreatetruecolor( $dst_width, $dst_height ); //創(chuàng)建暫時保存的畫布
        imagecopy( $tmp_img, $this->sImage, 0, 0, $dst_x,$dst_y, $dst_width, $dst_height ); //拷貝出圖像的一部分,進行裁切
        imagecopyresampled( $this->dImage, $tmp_img, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h ); //把暫時緩存的圖片,放到目標文件里面
        imagedestroy( $tmp_img );
 
        return true;
    }
    
    
    /**
     * 存儲
     * @author	 Administrator
     * @datetime 2020年7月22日 下午3:15:52
     * @comment	
     * 
     * @param unknown $file
     * @return boolean
     */
    public function save( $file = null )
    {
        if( !isset( $file ) || is_null( $file ) )
        {
            $this->dst_file = $this->src_file;
        }
        else
        {
            $this->dst_file = $this->basisUploadPath . '/'. $file;
        }
        try{
            switch( $this->src_type )
            {
                case IMAGETYPE_JPEG :
                    @imagejpeg( $this->dImage, $this->dst_file, 98 );
                break;
                case IMAGETYPE_PNG :
                    imagepng( $this->dImage, $this->dst_file );
                break;
                case IMAGETYPE_GIF :
                    imagegif( $this->dImage, $this->dst_file );
                break;
                case 18:
                    @imagejpeg( $this->dImage, $this->dst_file, 98 );
                break;
                default:
                    return false;
            }
        }catch( \Exception $e ){
            
        }
        
        $strExt = $this->getImgExt( $this->dst_file );
        
        $tmpImageInfo = @getimagesize( $this->dst_file );
        $width = 0;
        $height = 0;
        if( isset( $tmpImageInfo[0] ) && $tmpImageInfo[0] > 0 )
        {
            $width = $tmpImageInfo[0];
        }
        if( isset( $tmpImageInfo[1] ) && $tmpImageInfo[1] > 0 )
        {
            $height = $tmpImageInfo[1];
        }
        
        $objRet = new \stdClass();
        $objRet->mime       = $this->mime;
        $objRet->filename   = basename( $this->dst_file );
        $objRet->ext        = $strExt;
        $objRet->width      = $width;
        $objRet->height     = $height;
        
        return $objRet;
    }
    
    
    /**
     * 數(shù)據(jù)銷毀
     * @author	 Administrator
     * @datetime 2020年7月22日 下午3:31:12
     * @comment	
     *
     */
    public function destroy()
    {
        imagedestroy( $this->sImage);
        imagedestroy( $this->dImage );
        @unlink( $this->src_file );
        
        return true;
    }
 
    /**
     * 檢索圖集是否存在后綴
     *  若不存在-則使用默認(強制轉(zhuǎn)換)
     * @author	 Administrator
     * @datetime 2018年11月27日  下午3:30:47
     * @comment
     *
     * @param unknown $url
     * @return string|unknown
     */
    protected function getImgExt( $url )
    {
        
        $iLastSlash = strrpos( $url, '/' );
        $strFileName = mb_substr( $url, intval($iLastSlash+1) );
        $strExt = strrchr( $strFileName, '.' );
        preg_match( "/(.*?)\?.*?/", $strExt, $matches );
        if( !empty( $matches ) && isset( $matches[1] ) )
        {
            $strExt = $matches[1];
        }
        if( false == $strExt || is_null( $strExt ) || strlen( $strExt ) <= 0 )
        {
            $strExt = '.jpg';
        }
        
        return $strExt;
    }
    
    
}

知識補充

除了上文的內(nèi)容,小編還為大家整理了php實現(xiàn)封裝圖片水印添加、壓縮、剪切的相關(guān)方法,希望對大家有所幫助

<?php  

class Image
{    
    private $info;    private $image;    public $type;    public function construct($src)
    {

        $this->info=getimagesize($src);
        $this-&gt;type=image_type_to_extension($this-&gt;info['2'],false);
        $fun="imagecreatefrom{$this-&gt;type}";
        $this-&gt;image=$fun($src);
    }    /**
     * 文字水印
     * @param  [type]  $font     字體
     * @param  [type]  $content  內(nèi)容
     * @param  [type]  $size     文字大小
     * @param  [type]  $col      文字顏色(四元數(shù)組)
     * @param  array   $location 位置 
     * @param  integer $angle    傾斜角度
     * @return [type]           
     */
    public function fontMark($font,$content,$size,$col,$location,$angle=0){
        $col=imagecolorallocatealpha($this-&gt;image, $col['0'], $col['1'], $col['2'],$col['3']);

        imagettftext($this-&gt;image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);
    }    
    /**
     * 圖片水印
     * @param  [type] $imageMark 水印圖片地址
     * @param  [type] $dst       水印圖片在原圖片中的位置
     * @param  [type] $pct       透明度
     * @return [type]            
     */
    public function imageMark($imageMark,$dst,$pct){
        $info2=getimagesize($imageMark);
        $type=image_type_to_extension($info2['2'],false);
        $func2="imagecreatefrom".$type;
        $water=$func2($imageMark);

        imagecopymerge($this-&gt;image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);
        imagedestroy($water);

    }    /**
     * 壓縮圖片
     * @param  [type] $thumbSize 壓縮圖片大小
     * @return [type]            [description]     */
    public function thumb($thumbSize){
        $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);
        
        imagecopyresampled($imageThumb, $this-&gt;image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this-&gt;info['0'], $this-&gt;info['1']);
        imagedestroy($this-&gt;image);
        $this-&gt;image=$imageThumb;
    }    /**
    * 裁剪圖片
     * @param  [type] $cutSize  裁剪大小
     * @param  [type] $location 裁剪位置
     * @return [type]           [description]     */
     public function cut($cutSize,$location){
         $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);

         imagecopyresampled($imageCut, $this-&gt;image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);
         imagedestroy($this-&gt;image);
         $this-&gt;image=$imageCut;
     }    /**
     * 展現(xiàn)圖片
     * @return [type] [description]     */
    public function show(){
        header("content-type:".$this-&gt;info['mime']);

        $funn="image".$this-&gt;type;

        $funn($this-&gt;image);
    }    /**
     * 保存圖片
 * @param  [type] $newname 新圖片名
 * @return [type]          [description] */
     public function save($newname){
         header("content-type:".$this-&gt;info['mime']);

         $funn="image".$this-&gt;type;

         $funn($this-&gt;image,$newname.'.'.$this-&gt;type);
     }     public function destruct(){
         imagedestroy($this-&gt;image);
     }

 }

以上就是基于PHP封裝圖片裁剪工具類的詳細內(nèi)容,更多關(guān)于PHP圖片裁剪的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論