<?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);
}
}
}
}
}