/fwoq/core/libs/img_engine/image_gd.php
PHP | 493 lines | 337 code | 30 blank | 126 comment | 62 complexity | 80f6ef5ebe5836b3add7aa3e7fa0c4e2 MD5 | raw file
- <?php
- /**
- * $Id: image_gd.php 228 2011-09-19 03:39:11Z lingter@gmail.com $
- *
- * @author : Lingter
- * @support : http://www.meiu.cn
- * @copyright : (c)2010 meiu.cn lingter@gmail.com
- */
- class image_gd {
- /**
- * ??????
- *
- * @var image
- */
- var $image;
- /**
- * ????
- *
- * @var imagetype
- */
- var $image_type;
-
- var $image_quality=90;
- var $true_color = false;
-
- /**
- * ????
- *
- * @param string $filename ??????
- * @return void
- */
- function load($filename) {
- $image_info = getimagesize($filename);
- $this->image_type = $image_info[2];
- if( $this->image_type == IMAGETYPE_JPEG ) {
- $this->image = imagecreatefromjpeg($filename);
- } elseif( $this->image_type == IMAGETYPE_GIF ) {
- $this->image = imagecreatefromgif($filename);
- } elseif( $this->image_type == IMAGETYPE_PNG ) {
- $this->image = imagecreatefrompng($filename);
- }else{
- return false;
- }
- if(function_exists("imagecopyresampled") && function_exists("imagecreatetruecolor") && $this->image_type != IMAGETYPE_GIF){
- $this->true_color = true;
- }
- return true;
- }
-
- function supportType(){
- return array('jpg','jpeg','gif','png');
- }
-
- function setQuality($q){
- if($q>0)
- $this->image_quality = $q;
- }
- /**
- * ?????
- *
- * @return string ???
- */
- function getExtension(){
- if( $this->image_type == IMAGETYPE_JPEG ) return 'jpg';
- elseif( $this->image_type == IMAGETYPE_GIF ) return 'gif';
- elseif( $this->image_type == IMAGETYPE_PNG ) return 'png';
- }
- /**
- * ??????????
- *
- * @param string $filename ???
- * @param int $image_type ????
- * return volid
- */
- function save($filename) {
- $image_type = $this->image_type;
- if( $image_type == IMAGETYPE_JPEG ) {
- imagejpeg($this->image,$filename,$this->image_quality);
- } elseif( $image_type == IMAGETYPE_GIF ) {
- imagegif($this->image,$filename);
- } elseif( $image_type == IMAGETYPE_PNG ) {
- imagepng($this->image,$filename);
- }
- }
-
- /**
- * ?????????
- *
- * @param int $image_type ????
- * @return void
- */
- function output() {
- $image_type = $this->image_type;
- if( $image_type == IMAGETYPE_JPEG ) {
- header('Content-Type: image/jpeg');
- imagejpeg($this->image,NULL,$this->image_quality);
- } elseif( $image_type == IMAGETYPE_GIF ) {
- header('Content-type: image/gif');
- imagegif($this->image);
- } elseif( $image_type == IMAGETYPE_PNG ) {
- header('Content-type: image/png');
- imagepng($this->image);
- }
- }
- /**
- * ??????
- *
- * @return int ????
- */
- function getWidth() {
- return imagesx($this->image);
- }
- /**
- * ??????
- *
- * @return int ????
- */
- function getHeight() {
- return imagesy($this->image);
- }
- /**
- * ??????????
- *
- * @param int $height ????
- */
- function resizeToHeight($height) {
- $ratio = $height / $this->getHeight();
- $width = $this->getWidth() * $ratio;
- $this->resize($width,$height);
- }
- /**
- * ???????
- *
- * @param int $w ????
- * @param int $h ????
- */
- function resizeTo($w=0, $h=0) {
- if($w>0 && $h>0) return $this->resize($w,$h);
- else if($w>0) return $this->resizeToWidth($w);
- else if($h>0) return $this->resizeToHeight($h);
- }
- /**
- * ???????????
- * @param int $w ????
- * @param int $h ????
- */
- function resizeScale($w=0,$h=0){
- if($w == 0 && $h>0){
- return $this->resizeToHeight($h);
- }
- if($h == 0 && $w>0){
- return $this->resizeToWidth($w);
- }
- if($w == 0 && $h==0){
- return false;
- }
- $maxwidth = $w;
- $maxheight = $h;
-
- $width = $this->getWidth();
- $height = $this->getHeight();
-
- $RESIZEWIDTH = $RESIZEHEIGHT = false;
- if($maxwidth && $width > $maxwidth){
- $widthratio = $maxwidth/$width;
- $RESIZEWIDTH=true;
- }
- if($maxheight && $height > $maxheight){
- $heightratio = $maxheight/$height;
- $RESIZEHEIGHT=true;
- }
- if($RESIZEWIDTH && $RESIZEHEIGHT){
- if($widthratio < $heightratio){
- return $this->resizeToWidth($w);
- }else{
- return $this->resizeToHeight($h);
- }
- }elseif($RESIZEWIDTH){
- return $this->resizeToWidth($w);
- }elseif($RESIZEHEIGHT){
- return $this->resizeToHeight($h);
- }
- }
- /**
- * ????????????????
- *
- * @param int $v ????/??
- */
- function square($v){
- $width = $this->getWidth();
- $height = $this->getHeight();
- $left = 0;
- $top = 0;
- if($width>$height){
- $this->resizeToHeight($v);
- $left = ceil(($v/$height * $width - $v)/2);
- }else{
- $this->resizeToWidth($v);
- $top = ceil(($v/$width * $height - $v)/2);
- }
- $this->cut($v,$v,$left,$top);
- }
- /**
- * ??????????
- *
- * @param int $width ????
- */
- function resizeToWidth($width) {
- if($width>=$this->getWidth()) return;
- $ratio = $width / $this->getWidth();
- $height = $this->getHeight() * $ratio;
- $this->resize($width,$height);
- }
- /**
- * ???????????
- *
- * @param int $scale ????
- */
- function scale($scale) {
- $width = $this->getWidth() * $scale/100;
- $height = $this->getHeight() * $scale/100;
- $this->resize($width,$height);
- }
- /**
- * ??????
- *
- * @param int $width ????
- * @param int $height ????
- */
- function resize($width,$height) {
- if($this->true_color){
- $newim = imagecreatetruecolor($width, $height);
- imagecopyresampled($newim, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
- }else{
- $newim = imagecreate($width, $height);
- imagecopyresized($newim, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
- }
- $this->image = $newim;
- }
- /**
- * ????
- *
- * @param int $width ????
- * @param int $height ????
- */
- function cut($width,$height,$left = 0,$top = 0){
- if($this->true_color){
- $new_image = imagecreatetruecolor($width, $height);
- }else{
- $new_image = imagecreate($width, $height);
- }
- imagecopy($new_image, $this->image, 0, 0, $left, $top, $width, $height);
- $this->image = $new_image;
- }
- /**
- * ?????????????????
- *
- * @param int $top ????
- * @param int $height ????
- */
- function vcut($top,$height){
- $width = $this->getWidth();
- $height = $this->getHeight()-$top+$height;
- if($height<200) return;
- if($this->true_color){
- $new_image = imagecreatetruecolor($width, $height);
- }else{
- $new_image = imagecreate($width, $height);
- }
- imagecopy($new_image, $this->image, 0, 0, 0, $top, $width, $height);
- $this->image = $new_image;
- }
-
- /*
- ????
- */
- function rotate($dgree){
- $tran = imagecolortransparent($this->image,NULL);
- $new_image = imagerotate($this->image, $dgree , $tran);
- $this->image = $new_image;
- }
-
- function waterMarkSetting($param){
- $this->param = $param;
- }
-
- function waterMarkImg(){
- if(empty($this->param['water_mark_image']) || !file_exists($this->param['water_mark_image'])){
- return false;
- }
-
- $water_info = getimagesize($this->param['water_mark_image']);
- $w = $water_info[0];//????????
- $h = $water_info[1];//????????
- switch($water_info[2])//?????????
- {
- case 1:$water_im = imagecreatefromgif($this->param['water_mark_image']);break;
- case 2:$water_im = imagecreatefromjpeg($this->param['water_mark_image']);break;
- case 3:$water_im = imagecreatefrompng($this->param['water_mark_image']);break;
- default:return false;
- }
- $ground_w = $this->getWidth();
- $ground_h = $this->getHeight();
-
- if( $ground_w<$w || $ground_h<$h ){
- return false;
- }
- switch($this->param['water_mark_pos'])
- {
- case 0://??
- $posX = rand(5,($ground_w - $w - 5));
- $posY = rand(5,($ground_h - $h - 5));
- break;
- case 1://1?????
- $posX = 5;
- $posY = 5;
- break;
- case 2://2?????
- $posX = ($ground_w - $w) / 2;
- $posY = 5;
- break;
- case 3://3?????
- $posX = $ground_w - $w -5;
- $posY = 5;
- break;
- case 4://4?????
- $posX = 5;
- $posY = ($ground_h - $h) / 2;
- break;
- case 5://5?????
- $posX = ($ground_w - $w) / 2;
- $posY = ($ground_h - $h) / 2;
- break;
- case 6://6?????
- $posX = $ground_w - $w - 5;
- $posY = ($ground_h - $h) / 2;
- break;
- case 7://7?????
- $posX = 5;
- $posY = $ground_h - $h - 5;
- break;
- case 8://8?????
- $posX = ($ground_w - $w) / 2;
- $posY = $ground_h - $h - 5;
- break;
- case 9://9?????
- $posX = $ground_w - $w - 5;
- $posY = $ground_h - $h - 5;
- break;
- default://??
- $posX = rand(5,($ground_w - $w - 5));
- $posY = rand(5,($ground_h - $h - 5));
- break;
- }
- //?????????
- imagealphablending($this->image, true);
- if(function_exists('imagecopymerge') && $this->param['water_mark_opacity'] != 0){
- @imagecopymerge($this->image, $water_im, $posX, $posY, 0, 0, $w,$h,$this->param['water_mark_opacity']);
- }else{
- imagecopy($this->image, $water_im, $posX, $posY, 0, 0, $w,$h);//?????????
- }
- imagedestroy($water_im);
- }
-
- function waterMarkFont(){
- if($this->param['water_mark_color']){
- $color = $this->param['water_mark_color'];
- }else{
- $color = '#000000';
- }
- $r = hexdec( substr( $color, 1, 2 ) );
- $g = hexdec( substr( $color, 3, 2 ) );
- $b = hexdec( substr( $color, 5, 2 ) );
-
- if($this->param['water_mark_opacity']>0 && $this->param['water_mark_opacity']<100){
- $fontcolor = imagecolorallocatealpha( $this->image, $r, $g, $b ,$this->param['water_mark_opacity']);
- }else{
- $fontcolor = imagecolorallocate( $this->image, $r, $g, $b );
- }
-
- $box = ImageTTFBBox(
- $this->param['water_mark_fontsize'],
- $this->param['water_mark_angle'],
- $this->param['water_mark_font'],
- $this->param['water_mark_string']);
- $ground_w = $this->getWidth();
- $ground_h = $this->getHeight();
- $h = max($box[1], $box[3]) - min($box[5], $box[7]);
- $w = max($box[2], $box[4]) - min($box[0], $box[6]);
- $ax = min($box[0], $box[6]) * -1;
- $ay = min($box[5], $box[7]) * -1;
- switch($this->param['water_mark_pos'])
- {
- case 0://??
- $posX = rand(5,($ground_w - $w - 5));
- $posY = rand(5,($ground_h - $h - 5));
- break;
- case 1://1?????
- $posX = 5;
- $posY = 5;
- break;
- case 2://2?????
- $posX = ($ground_w - $w) / 2;
- $posY = 5;
- break;
- case 3://3?????
- $posX = $ground_w - $w -5;
- $posY = 5;
- break;
- case 4://4?????
- $posX = 5;
- $posY = ($ground_h - $h) / 2;
- break;
- case 5://5?????
- $posX = ($ground_w - $w) / 2;
- $posY = ($ground_h - $h) / 2;
- break;
- case 6://6?????
- $posX = $ground_w - $w - 5;
- $posY = ($ground_h - $h) / 2;
- break;
- case 7://7?????
- $posX = 5;
- $posY = $ground_h - $h - 5;
- break;
- case 8://8?????
- $posX = ($ground_w - $w) / 2;
- $posY = $ground_h - $h - 5;
- break;
- case 9://9?????
- $posX = $ground_w - $w - 5;
- $posY = $ground_h - $h - 5;
- break;
- default://??
- $posX = rand(5,($ground_w - $w - 5));
- $posY = rand(5,($ground_h - $h - 5));
- break;
- }
- /*if($this->param['water_mark_shadow'] == 'gray'){
- $shadowcolor = imagecolorallocate( $this->image, 160, 160, 160 );
- imagettftext($this->image,
- $this->param['water_mark_fontsize'],
- $this->param['water_mark_angle'],
- $posX + $ax+1,
- $posY + $ay+1,
- $shadowcolor,
- $this->param['water_mark_font'],
- $this->param['water_mark_string']);
- }elseif($this->param['water_mark_shadow'] == 'white'){
- $shadowcolor = imagecolorallocate( $this->image, 255, 255, 255 );
- imagettftext($this->image,
- $this->param['water_mark_fontsize'],
- $this->param['water_mark_angle'],
- $posX + $ax + 1,
- $posY + $ay + 1,
- $shadowcolor,
- $this->param['water_mark_font'],
- $this->param['water_mark_string']);
- }*/
-
- imagettftext($this->image,
- $this->param['water_mark_fontsize'],
- $this->param['water_mark_angle'],
- $posX + $ax,
- $posY + $ay,
- $fontcolor,
- $this->param['water_mark_font'],
- $this->param['water_mark_string']);
- }
-
- function waterMark(){
- //??????
- if($this->param['water_mark_type'] == 'image'){
- $this->waterMarkImg();
- }elseif($this->param['water_mark_type'] == 'font'){
- $this->watermarkFont();
- }
- return false;
- }
- }