-
-
/** - *
- * Image processing class
- * @author FC_LAMP
- * @internal functions include: watermark, thumbnail
- */
- class Img
- {
- //Image format
- private $exts = array ('jpg', 'jpeg', 'gif ', 'bmp', 'png' );
/**
- *
- *
- * @throws Exception
- */
- public function __construct()
- {
- if (! function_exists ( 'gd_info' ))
- {
- throw new Exception ( 'Loading GD library failed!' );
- }
- }
/**
- *
- * Cropping and compression
- * @param $src_img picture
- * @param $save_img generated picture
- * @param $option parameter options, including: $maxwidth width $maxheight height
- * array('width'=> xx,'height'=>xxx)
- * @internal
- * General image compression method, when the image is too long or too wide, the generated image
- * will be "squashed". For this, crop first and then press Proportional compression method
- */
- public function thumb_img($src_img, $save_img = '', $option)
- {
-
if (empty ( $option ['width'] ) or empty ( $option ['height'] ))
- {
- return array ('flag' => False, 'msg' => 'Original The length and width of the image cannot be less than 0' );
- }
- $org_ext = $this->is_img ( $src_img );
- if (! $org_ext ['flag'])
- {
- return $org_ext;
- }< /p>
//If there is a save path, determine whether the path is correct
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if ( ! $f ['flag'])
- {
- return $f;
- }
- }
//Get the corresponding method
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
//Get the original size
- $source = $org_funcs ['create_func'] ( $src_img );
- $src_w = imagesx ( $source );
- $ src_h = imagesy ( $source );
//Adjust the original image (keep the original shape of the image and crop the image)
- $dst_scale = $option ['height'] / $option ['width'] ; //Target image aspect ratio
- $src_scale = $src_h / $src_w; // Original image aspect ratio
- if ($src_scale >= $dst_scale)
- { // Too high
- $w = intval ( $src_w );
- $h = intval ( $dst_scale * $w );
$x = 0;
- $y = ($src_h - $h) / 3;
- } else
- { / / Too wide
- $h = intval ( $src_h );
- $w = intval ( $h / $dst_scale );
$x = ($src_w - $w) / 2;
- $y = 0;
- }
- // Crop
- $croped = imagecreatetruecolor ( $w, $h );
- imagecopy ( $croped, $source, 0, 0, $x, $y, $src_w, $src_h );
- // Scaling
- $scale = $option ['width'] / $w;
- $target = imagecreatetruecolor ( $option ['width'], $option ['height'] );
- $final_w = intval ( $w * $scale );
- $final_h = intval ( $h * $scale );
- imagecopyresampled ( $target, $croped, 0, 0, 0, 0, $final_w, $final_h, $w, $h );
- imagedestroy ( $croped );
//Output (save) picture
- if (! empty ( $save_img ))
- {
$org_funcs ['save_func' ] ( $target, $save_img );
- } else
- {
- header ( $org_funcs ['header'] );
- $org_funcs ['save_func'] ( $target );
- }
- imagedestroy ( $target );
- return array ('flag' => True, 'msg' => '' );
- }
/**
- *
- * Scale image
- * @param $src_img Original image
- * @param $save_img Where to save
- * @param $option Parameter setting array('width'=>xx,'height'=> xxx)
- *
- */
- function resize_image($src_img, $save_img = '', $option)
- {
- $org_ext = $this->is_img ( $src_img );
- if (! $org_ext ['flag'])
- {
- return $org_ext;
- }
-
//If there is a save path, determine whether the path is correct
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f [ 'flag'])
- {
- return $f;
- }
- }
//Get the corresponding method
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg' ] );
//Get the original size
- $source = $org_funcs ['create_func'] ( $src_img );
- $src_w = imagesx ( $source );
- $src_h = imagesy ( $source );
if (($option ['width'] && $src_w > $option ['width']) || ($option ['height'] && $src_h > $option ['height']))
- {
- if ($option ['width'] && $src_w > $option ['width'])
- {
- $widthratio = $option ['width'] / $src_w;
- $resizewidth_tag = true;
- }
if ($option ['height'] && $src_h > $option ['height'])
- {
- $heightratio = $ option ['height'] / $src_h;
- $resizeheight_tag = true;
- }
if ($resizewidth_tag && $resizeheight_tag)
- {
- if ($widthratio < $heightratio)
- $ ratio = $widthratio;
- else
- $ratio = $heightratio;
- }
if ($resizewidth_tag && ! $resizeheight_tag)
- $ratio = $widthratio;
- if ($resizeheight_tag && ! $resizewidth_tag)
- $ratio = $heightratio;
$newwidth = $src_w * $ratio;
- $newheight = $src_h * $ratio;
if (function_exists ( "imagecopyresampled" ))
- {
- $newim = imagecreatetruecolor ( $newwidth, $newheight );
- imagecopyresampled ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
- } else
- {
- $newim = imagecreate ( $newwidth, $newheight );
- imagecopyresized ( $newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src_w, $src_h );
- }
- }
- //输出(保存)图片
- if (! empty ( $save_img ))
- {
$org_funcs ['save_func'] ( $newim, $save_img );
- } else
- {
- header ( $org_funcs ['header'] );
- $org_funcs ['save_func'] ( $newim );
- }
- imagedestroy ( $newim );
- return array ('flag' => True, 'msg' => '' );
- }
/**
- *
- * Generate watermark image
- * @param $org_img Original image
- * @param $mark_img Watermark image
- * @param $save_img When its directory does not exist, it will try to create the directory
- * @param array $option is Some basic settings of the watermark include:
- * x: the horizontal position of the watermark, the default is the value after subtracting the width of the watermark image
- * y: the vertical position of the watermark, the default is the value after subtracting the height of the watermark image
- * alpha: alpha Value (control transparency), default is 50
- */
- public function water_mark($org_img, $mark_img, $save_img = '', $option = array())
- {
- //检查图片
- $org_ext = $this->is_img ( $org_img );
- if (! $org_ext ['flag'])
- {
- return $org_ext;
- }
- $mark_ext = $this->is_img ( $mark_img );
- if (! $mark_ext ['flag'])
- {
- return $mark_ext;
- }
- //如果有保存路径,则确定路径是否正确
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f ['flag'])
- {
- return $f;
- }
- }
//获取相应画布
- $org_funcs = $this->get_img_funcs ( $org_ext ['msg'] );
- $org_img_im = $org_funcs ['create_func'] ( $org_img );
$mark_funcs = $this->get_img_funcs ( $mark_ext ['msg'] );
- $mark_img_im = $mark_funcs ['create_func'] ( $mark_img );
//拷贝水印图片坐标
- $mark_img_im_x = 0;
- $mark_img_im_y = 0;
- //拷贝水印图片高宽
- $mark_img_w = imagesx ( $mark_img_im );
- $mark_img_h = imagesy ( $mark_img_im );
$org_img_w = imagesx ( $org_img_im );
- $org_img_h = imagesx ( $org_img_im );
//合成生成点坐标
- $x = $org_img_w - $mark_img_w;
- $org_img_im_x = isset ( $option ['x'] ) ? $option ['x'] : $x;
- $org_img_im_x = ($org_img_im_x > $org_img_w or $org_img_im_x < 0) ? $x : $org_img_im_x;
- $y = $org_img_h - $mark_img_h;
- $org_img_im_y = isset ( $option ['y'] ) ? $option ['y'] : $y;
- $org_img_im_y = ($org_img_im_y > $org_img_h or $org_img_im_y < 0) ? $y : $org_img_im_y;
//alpha
- $alpha = isset ( $option ['alpha'] ) ? $option ['alpha'] : 50;
- $alpha = ($alpha > 100 or $alpha < 0) ? 50 : $alpha;
//合并图片
- imagecopymerge ( $org_img_im, $mark_img_im, $org_img_im_x, $org_img_im_y, $mark_img_im_x, $mark_img_im_y, $mark_img_w, $mark_img_h, $alpha );
//输出(保存)图片
- if (! empty ( $save_img ))
- {
$org_funcs ['save_func'] ( $org_img_im, $save_img );
- } else
- {
- header ( $org_funcs ['header'] );
- $org_funcs ['save_func'] ( $org_img_im );
- }
- //销毁画布
- imagedestroy ( $org_img_im );
- imagedestroy ( $mark_img_im );
- return array ('flag' => True, 'msg' => '' );
}
/**
- *
- * Check the image
- * @param unknown_type $img_path
- * @return array('flag'=>true/false,'msg'=>ext/error message)
- */
- private function is_img($img_path)
- {
- if (! file_exists ( $img_path ))
- {
- return array ('flag' => False, 'msg' => "加载图片 $img_path 失败!" );
- }
- $ext = explode ( '.', $img_path );
- $ext = strtolower ( end ( $ext ) );
- if (! in_array ( $ext, $this->exts ))
- {
- return array ('flag' => False, 'msg' => "图片 $img_path 格式不正确!" );
- }
- return array ('flag' => True, 'msg' => $ext );
- }
/**
- *
- * Return the correct image function
- * @param unknown_type $ext
- */
- private function get_img_funcs($ext)
- {
- //选择
- switch ($ext)
- {
- case 'jpg' :
- $header = 'Content-Type:image/jpeg';
- $createfunc = 'imagecreatefromjpeg';
- $savefunc = 'imagejpeg';
- break;
- case 'jpeg' :
- $header = 'Content-Type:image/jpeg';
- $createfunc = 'imagecreatefromjpeg';
- $savefunc = 'imagejpeg';
- break;
- case 'gif' :
- $header = 'Content-Type:image/gif';
- $createfunc = 'imagecreatefromgif';
- $savefunc = 'imagegif';
- break;
- case 'bmp' :
- $header = 'Content-Type:image/bmp';
- $createfunc = 'imagecreatefrombmp';
- $savefunc = 'imagebmp';
- break;
- default :
- $header = 'Content-Type:image/png';
- $createfunc = 'imagecreatefrompng';
- $savefunc = 'imagepng';
- }
- return array ('save_func' => $savefunc, 'create_func' => $createfunc, 'header' => $header );
- }
/**
- *
- * Check and try to create the directory
- * @param $save_img
- */
- private function check_dir($save_img)
- {
- $dir = dirname ( $save_img );
- if (! is_dir ( $dir ))
- {
- if (! mkdir ( $dir, 0777, true ))
- {
- return array ('flag' => False, 'msg' => "图片保存目录 $dir 无法创建!" );
- }
- }
- return array ('flag' => True, 'msg' => '' );
- }
- }
if (! empty ( $_FILES ['test'] ['tmp_name'] ))
- {
- //例子
- $img = new Img ();
- //原图
- $name = explode ( '.', $_FILES ['test'] ['name'] );
- $org_img = 'D:/test.' . end ( $name );
- move_uploaded_file ( $_FILES ['test'] ['tmp_name'], $org_img );
- $option = array ('width' => $_POST ['width'], 'height' => $_POST ['height'] );
- if ($_POST ['type'] == 1)
- {
- $s = $img->resize_image ( $org_img, '', $option );
- } else
- {
- $img->thumb_img ( $org_img, '', $option );
- }
- unlink ( $org_img );
- }
-
复制代码
使用方式:
php水印
-
- $img = new Img ();
- $org_img = 'D:/tt.png';
- $mark_img = 'D:/t.png';
- //保存水印图片(如果$save_img为空时,将会直接输出图片)
- $save_img = 'D:/test99h/testone/sss.png';
- //水印设置调节
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 80 );
- //生成水印图片
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
复制代码
当调节 $option 参数时,会有相应变化:
-
-
//例子
- $img = new Img ();
- $org_img = 'D:/tt.png';
- //压缩图片(100*100)
- $option = array ('width' => 100, 'height' => 100 );
//$save_img为空时,将会直接输出图像到浏览器
$save_img = 'D:/test99h/testone/sss_thumb.png';
- $flag = $img->thumb_img ( $org_img, $save_img, $option );
-
-
复制代码
调节$option的大小值:
-
- $img = new Img ();
- //原图
- $org_img = 'D:/tt.png';
- //水印标记图
- $mark_img = 'D:/t.png';
- //保存水印图片
- $save_img = 'D:/test99h/testone/sss.png';
- //水印设置调节
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 60 );
- //生成水印图片
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
- //压缩水印图片
- $option = array ('width' => 200, 'height' => 200 );
- //保存压缩图
- $save_img2 = 'D:/test99h/testone/sss2_thumb.png';
- $flag = $img->thumb_img ( $save_img, $save_img2, $option ); //等比例压缩类似
-
复制代码
在压缩生成的水印图像时,压缩后生成的图像格式应与原图像,水印图像一致。
不然,会出现一些未知错误。
|