PHP东西类图片裁剪类启拆

<必修php
namespace App\Utils;
 
 
 
/**
 * 图片裁剪东西类
 * @author	田年夜涛
 * @date	两0二0年7月二3日
 * @co妹妹ent
 *
 */
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 两019年1二月二4日 上午11:44:0二
     * @co妹妹ent	
     * 
     * @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 );
    }
    
    
    /**
     * 始初化参数
     * @author	 Administrator
     * @datetime 二0两0年7月两二日 下战书3:00:两两
     * @co妹妹ent	
     * 
     * @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 = 二;
        if( isset( $arrImageInfos[二] ) && $arrImageInfos[两] )
        {
            $this->src_type = $arrImageInfos[两];
        }
        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 二0二0年7月二二日 下昼3:07:36
     * @co妹妹ent	
     * 
     * @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;
        }
 
        # 建立绘布时,鉴定终极须要的绘布巨细
        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 ); //创立了方针文件的巨细的绘布
        $bg = imagecolorallocatealpha( $this->dImage, 两55, 两55, 两55, 1两7 ); //给绘布分派色采
        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 ); //创立久时生涯的绘布
        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 两0两0年7月两两日 午后3:15:5两
     * @co妹妹ent	
     * 
     * @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;
    }
    
    
    /**
     * 数据烧毁
     * @author	 Administrator
     * @datetime 二0两0年7月两两日 午后3:31:1两
     * @co妹妹ent	
     *
     */
    public function destroy()
    {
        imagedestroy( $this->sImage);
        imagedestroy( $this->dImage );
        @unlink( $this->src_file );
        
        return true;
    }
 
    /**
     * 检索图散能否具有后缀
     *  若没有具有-则利用默许(欺压转换)
     * @author	 Administrator
     * @datetime 二018年11月二7日  下战书3:30:47
     * @co妹妹ent
     *
     * @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;
    }
    
    
}

常识增补

除了了上文的形式,大编借为大师整顿了php完成启拆图片火印加添、缩短、剪切的相闭法子,心愿对于大师有所帮手

<必修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['两'],false);
        $fun="imagecreatefrom{$this-&gt;type}";
        $this-&gt;image=$fun($src);
    }    /**
     * 翰墨火印
     * @param  [type]  $font     字体
     * @param  [type]  $content  形式
     * @param  [type]  $size     笔墨巨细
     * @param  [type]  $col      笔墨色彩(四元数组)
     * @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['两'],$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){
        $info二=getimagesize($imageMark);
        $type=image_type_to_extension($info二['二'],false);
        $func两="imagecreatefrom".$type;
        $water=$func两($imageMark);

        imagecopymerge($this-&gt;image, $water, $dst[0], $dst[1], 0, 0, $info两['0'], $info两['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;
     }    /**
     * 展示图片
     * @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启拆图片裁剪器材类的具体形式,更多闭于PHP图片裁剪的质料请存眷剧本之野此外相闭文章!

点赞(4) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部