PHP 图片操作相关

<?php
class image{

    function __construct($configs=null){

    }

    
    function getIm($src_file){
        $src_type = end(explode(".",strtolower($src_file)));
        if($src_type=='jpg' || $src_type=='jpeg'){
            $im  =  imagecreatefromjpeg ($src_file);
        }else if($src_type=='png'){
            $im  =  imagecreatefrompng ($src_file);
        }else if($src_type=='gif'){
            $im  =  imagecreatefromgif ($src_file);
        }
        return $im;
    }

    function colorat($im,$x,$y){
        $rgb  =  imagecolorat( $im ,  $x ,  $y );
        $r  = ( $rgb  >>  16 ) &  0xFF ;
        $g  = ( $rgb  >>  8 ) &  0xFF ;
        $b  =  $rgb  &  0xFF ;
        return array('r'=>$r,'g'=>$g,'b'=>$b);
    }

    //颜色区间
    function colorRegion($color){
        return $color['r']>220 && $color['g']<20 && $color['b']<20;
    }

    function sizeFile($src_file){
        list( $width ,  $height ,  $type ,  $attr ) =  getimagesize ( $src_file );
        return array('width'=>$width,'height'=>$height);
    }

    function sizeIm($im){
        return array('width'=>imagesx($im),'height'=>imagesy($im));
    }

    function saveas($im,$dest_file, $dst_x=0 , $dst_y=0 , $src_x=0 , $src_y=0 , $dst_w=0 , $dst_h=0 , $src_w=0 , $src_h=0){
        if($dst_w==0 || $dst_h==0 || $src_w==0 || $src_h==0){
            $src_w = imagesx($im);
            $src_h = imagesy($im);
            $dst_w = $src_w;
            $dst_h = $src_h;
        }
        $dest_img = imagecreatetruecolor($dst_w,$dst_h);
        imagecopyresampled($dest_img, $im, $dst_x , $dst_y , $src_x , $src_y , $dst_w , $dst_h , $src_w , $src_h);

        $dest_type = end(explode(".",strtolower($dest_file)));
        if($dest_type=='jpg' || $dest_type=='jpeg'){
            $quality = 75; //jpg质量
            imagejpeg ( $dest_img, $dest_file, $quality);
        }else if($dest_type=='png'){
            imagepng ( $dest_img, $dest_file );
        }else if($dest_type=='gif'){
            imagegif ( $dest_img, $dest_file );
        }
        imagedestroy ( $dest_img );
    }

    //画一个点
    function pixel($im,$x, $y, $r, $g, $b){
        $color = imagecolorallocate($im, $r, $g, $b);
        imagesetpixel($im, $x, $y, $color);
    }

    //颜色差异区别值
    function diffColor($c1,$c2,$diff){
         return ($c1 >= $c2 && $c1 < $c2+$diff) || ($c1 <= $c2 && $c1 > $c2-$diff);
    }
    //图片的颜色替换
    function relplacePix($im,$colorOld,$colorNew,$colorDiff){
        $size = $this->sizeIm($im);
        for($x=0;$x<$size['width'];$x++){
            for($y=0;$y<$size['height'];$y++){
                $color = $this->colorat($im,$x,$y);
                if( $this->diffColor($color['r'],$colorOld['r'],$colorDiff['r']) &&
                    $this->diffColor($color['g'],$colorOld['g'],$colorDiff['g']) &&
                    $this->diffColor($color['b'],$colorOld['b'],$colorDiff['b'])
                )
                    
                {
                    $color = imagecolorallocate($im, $colorNew['r'], $colorNew['g'], $colorNew['b']);
                    imagesetpixel($im, $x, $y, $color);
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值