⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.image.php5.php

📁 axjx工具代码给大家交流
💻 PHP
📖 第 1 页 / 共 2 页
字号:
	public function crop($iNewWidth, $iNewHeight, $iResize = 0) {
  /* crop image (first resize with keep ratio) */
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    }   
		
		/* resize imageobject in memory if resize percentage is set */
		if ($iResize > 0) {
		  $this->resizetopercentage($iResize);
		}		 
		
		/* constrain width and height values */
    if (($iNewWidth > $this->width) || ($iNewWidth < 0)) {
		  $this->printError('width out of range',__METHOD__,__LINE__); 
    } 
    if (($iNewHeight > $this->height) || ($iNewHeight < 0)) {
		  $this->printError('height out of range',__METHOD__,__LINE__); 
    }	
	  
		/* create blank image with new sizes */
		$CroppedImageStream = ImageCreateTrueColor($iNewWidth,$iNewHeight);
		
		/* calculate size-ratio */
		$iWidthRatio = $this->width / $iNewWidth;
		$iHeightRatio = $this->height / $iNewHeight;
		$iHalfNewHeight = $iNewHeight / 2;
		$iHalfNewWidth = $iNewWidth / 2;
		
		/* if the image orientation is landscape */
		if($this->orientation == 'landscape') {
		  /* calculate resize width parameters */
			$iResizeWidth = $this->width / $iHeightRatio;
			$iHalfWidth = $iResizeWidth / 2;
			$iDiffWidth = $iHalfWidth - $iHalfNewWidth;
			
			if(function_exists("imagecopyresampled")){
				imagecopyresampled($CroppedImageStream,$this->ImageStream,-$iDiffWidth,0,0,0,$iResizeWidth,$iNewHeight,$this->width,$this->height);
			}
			else {
				imagecopyresized($CroppedImageStream,$this->ImageStream,-$iDiffWidth,0,0,0,$iResizeWidth,$iNewHeight,$this->width,$this->height);
			}		
		}
		/* if the image orientation is portrait or square */
		elseif(($this->orientation == 'portrait') || ($this->orientation == 'square')) {
		  /* calculate resize height parameters */		
			$iResizeHeight = $this->height / $iWidthRatio;
			$iHalfHeight = $iResizeHeight / 2;
			$iDiffHeight = $iHalfHeight - $iHalfNewHeight;
			
			if(function_exists("imagecopyresampled")){
				imagecopyresampled($CroppedImageStream,$this->ImageStream,0,-$iDiffHeight,0,0,$iNewWidth,$iResizeHeight,$this->width,$this->height);
			}
			else {
				imagecopyresized($CroppedImageStream,$this->ImageStream,0,-$iDiffHeight,0,0,$iNewWidth,$iResizeHeight,$this->width,$this->height);
			}	
		}
		else { 
			if(function_exists("imagecopyresampled")){
				imagecopyresampled($CroppedImageStream,$this->ImageStream,0,0,0,0,$iNewWidth,$iNewHeight,$this->width,$this->height);
			}
			else {
				imagecopyresized($CroppedImageStream,$this->ImageStream,0,0,0,0,$iNewWidth,$iNewHeight,$this->width,$this->height);
			}	
		}		

		$this->ImageStream = $CroppedImageStream;
		$this->width = $iNewWidth;
		$this->height = $iNewHeight;
		$this->setImageOrientation();											
	}				
	
	public function get_font_list(){
    $dict = Array(
      "Aldos Nova" => Array("fontfile"=>"aldos_nova.ttf","preview"=>"aldos_nova.gif"),
      "Beware" => Array("fontfile"=>"beware.ttf","preview"=>"beware.gif"),
      "Chopin Script" => Array("fontfile"=>"chopin_script.ttf","preview"=>"chopin_script.gif"),
      "Early Tickertape" => Array("fontfile"=>"early_tickertape.ttf","preview"=>"early_tickertape.gif"),
      "English Gothic" => Array("fontfile"=>"english_gothic.ttf","preview"=>"english_gothic.gif"),
      "Flores" => Array("fontfile"=>"flores.ttf","preview"=>"flores.gif"),
      "Fortunadot" => Array("fontfile"=>"fortunadot.ttf","preview"=>"fortunadot.gif"),
      "Funkin Frat" => Array("fontfile"=>"funkin_frat.ttf","preview"=>"funkin_frat.gif"),
      "Laurehead" => Array("fontfile"=>"laurehead.ttf","preview"=>"laurehead.gif"),
      "Plasmadrip" => Array("fontfile"=>"plasmadrip.ttf","preview"=>"plasmadrip.gif"),
      "Plump" => Array("fontfile"=>"pleasantly_plump.ttf","preview"=>"pleasantly_plump.gif")
      
    );
    return $dict;
  }
	
	public function writetext($sText, $iFontSize = 10, $sTextColor = '0,0,0', $sFontFilename = 'arial', $iXPos = 5, $iYPos = 15, $iTextAngle = 0) {
  /* write text on image */
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
		
    if (($iXPos > $this->width) || ($iXPos < 0)) {
		  $this->printError('x-pos out of range',__METHOD__,__LINE__); 
    } 
		
    if (($iYPos > $this->height) || ($iYPos < 0)) {
		  $this->printError('y-pos out of range',__METHOD__,__LINE__); 
    } 				
    
    $dict = $this->get_font_list();
    $sFontFilename = $dict[$sFontFilename]["fontfile"];
		
		$sFont = clsImage::IMAGEFONTDIR . DIRECTORY_SEPARATOR . $sFontFilename;
	  $aTextColor = explode(',',$sTextColor,3);
	  $ImageColor = imagecolorallocate($this->ImageStream,$aTextColor[0],$aTextColor[1],$aTextColor[2]);
		$iLineWidth = imagettfbbox($iFontSize, $iTextAngle, $sFont, $sText);
		imagettftext($this->ImageStream, $iFontSize, $iTextAngle, $iXPos, $iYPos, $ImageColor, $sFont, $sText);			
	}
  
  ///////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////////////
  //// IMG_FILTER_GRAYSCALE: Converts the image into grayscale. 
  public function filter_grayscale(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_GRAYSCALE);
  }
   
  //// IMG_FILTER_NEGATE: Reverses all colors of the image. 
  public function filter_negate(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_NEGATE);
  }  
  
  //// IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. 
  ////Use $level to set the level of brightness. 
  public function filter_brightness($level){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_BRIGHTNESS, $level);
  }  
  
  //// IMG_FILTER_CONTRAST: Changes the contrast of the image. 
  ///  Use $level to set the level of contrast. 
  public function filter_contrast($level){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_CONTRAST, $level);
  }
  
  //// IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. 
  //// Use  $red , $blue , $green and $alpha for the alpha channel. The range for each color is 0 to 255. 
  public function filter_colorize($red , $blue , $green, $alpha){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_COLORIZE, $red , $blue , $green, $alpha);
  }
  
  //// IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
  public function filter_edgedetect(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_EDGEDETECT);
  }
  
  //// IMG_FILTER_EMBOSS: Embosses the image. 
  public function filter_embosses(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_EMBOSS);
  }
    //// IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method. 
  public function filter_gaussian_blur(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_GAUSSIAN_BLUR);
  }
    //// IMG_FILTER_SELECTIVE_BLUR: Blurs the image. 
  public function filter_selective_blur(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_SELECTIVE_BLUR);
  }
  
    //// IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a "sketchy" effect. 
  public function filter_mean_removal(){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_MEAN_REMOVAL);
  }
    //// IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness. 
  public function filter_smooth($level){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    imagefilter($this->ImageStream, IMG_FILTER_SMOOTH,$level);
  }
  
  
  
  public function watermark_frame($frameFile){
  
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    $frame = imagecreatefromjpeg($frameFile);
    $frame2 = imagecreatetruecolor($this->width,$this->height); 
    imagecopyresampled($frame2, $frame, 0,0,0,0, $this->width,$this->height, $this->width,$this->height);
    //IMG_EFFECT_REPLACE - Use pixel replacement (equivalent of passing TRUE to imagealphablending()) 
    //IMG_EFFECT_ALPHABLEND - Use normal pixel blending (equivalent of passing FALSE to imagealphablending()) 
    //IMG_EFFECT_NORMAL -Same as IMG_EFFECT_ALPHABLEND. 
    //IMG_EFFECT_OVERLAY - Overlay has the effect that black background pixels will remain black, white background pixels will remain white, but grey background pixels will take the colour of the foreground pixel.  
    // Apply the overlay alpha blending flag
    //imagelayereffect($this->ImageStream, IMG_EFFECT_NORMAL);
    $black = imagecolorallocate($frame, 255, 255, 255);
    // Make the background transparent
    imagecolortransparent($frame, $black);

    for ($i=0;$i<$this->height;$i++) {
      for ($j=0;$j<$this->width;$j++){
        $rgb = imagecolorat($this->ImageStream,$i,$j);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        
        $rgb2 = imagecolorat($frame2,$i,$j);
        $r2 = ($rgb2 >> 16) & 0xFF;
        $g2 = ($rgb2 >> 8) & 0xFF;
        $b2 = $rgb2 & 0xFF;

        //This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
        $r = $r & $r2;
        $g = $g & $g2;
        $b = $b & $b2;
        imagesetpixel($this->ImageStream,$i,$j, ($r << 16) + ($g << 8) + $b);
      }
    } 

    //imagecopymergegray($this->ImageStream , $frame , 0 , 0 , 0 , 0 ,$this->width, $this->height,100);
    //imagelayereffect($this->ImageStream, IMG_EFFECT_NORMAL);
  }
  
  public function get_modern_list(){
    $dict = Array(
      "bikini" => Array("filename"=>"bikini.png","x"=>35, "y"=>36, "width"=>383, "height"=>342),
      "colorpen" => Array("filename"=>"colorpen.png","x"=>36, "y"=>36, "width"=>397, "height"=>557),
      "03" => Array("filename"=>"03.png","x"=>18, "y"=>15, "width"=>405, "height"=>305),
      "02" => Array("filename"=>"02.png","x"=>36, "y"=>40, "width"=>406, "height"=>303),
      "01" => Array("filename"=>"01.png","x"=>0, "y"=>0, "width"=>400, "height"=>300),
      "08" => Array("filename"=>"08.png","x"=>15, "y"=>15, "width"=>358, "height"=>296),
      "07" => Array("filename"=>"07.png","x"=>17, "y"=>147, "width"=>248, "height"=>174),
      "06" => Array("filename"=>"06.png","x"=>12, "y"=>8, "width"=>308, "height"=>291),
      "05" => Array("filename"=>"05.png","x"=>13, "y"=>15, "width"=>404, "height"=>302),
      "04" => Array("filename"=>"04.png","x"=>163, "y"=>14, "width"=>343, "height"=>307)
      
    );
    return $dict;
  }
	
	public function modern_frame($frameFile,$bgcolor="", $x="", $y="", $owidth="", $oheight=""){
	  if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    }
    
    $dict = $this->get_modern_list(false);
    if($x==""){
      $x = $dict[$frameFile]["x"];
      $y = $dict[$frameFile]["y"];
    }
    if($owidth==""){
      $owidth = $dict[$frameFile]["width"];
      $oheight = $dict[$frameFile]["height"];
    }
    $frameFile = MODERNPATH . DIRECTORY_SEPARATOR . $dict[$frameFile]["filename"];
    $frame = imagecreatefrompng($frameFile);
    //getting the width and height of that image
    list($fwidth, $fheight) = getimagesize($frameFile);
    
    //the background canvas, it will be the same width and height
    $background = imagecreatetruecolor($fwidth, $fheight);
    if(substr_count($bgcolor, "#")>0){
	    //converting the 16 digit hex value to the standard 10 digit value
	    $int = hexdec(str_replace("#", "", $bgcolor));
	    //getting rgb color
	    $background_color = imagecolorallocate ($background, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
	    //filling the background image with that color 
	    imagefill($background, 0,0,$background_color); 
    }
    
    //This function copies and resizes the  image onto the background canvas
    imagecopyresampled($background ,$this->ImageStream, $x, $y, 0, 0, $owidth, $oheight, $this->width, $this->height);

    //making sure that alpha blending is enabled
    imageAlphaBlending($frame, true);
    //making sure to preserve the alpha info
    imageSaveAlpha($frame, true);
    //This function copies and resizes the  image onto the background canvas
    imagecopyresampled($background ,$frame, 0,0,0,0,$fwidth, $fheight, $fwidth, $fheight); 
    
    imagedestroy($this->ImageStream);
    $this->ImageStream = $background;
    
    $this->width = $fwidth;
		$this->height = $fheight;
		$this->setImageOrientation();	
  }
  
  
  public function get_avatar_list(){
    $dict = Array(
      "hellokitty" => Array("filename"=>"hellokitty.png"),
      "fistplace" => Array("filename"=>"fistplace.png"),
      "mirror" => Array("filename"=>"mirror.png"),
      "snowman1" => Array("filename"=>"snowman1.png"),
      "snowman2" => Array("filename"=>"snowman2.png"),
      "rowpaul" => Array("filename"=>"rowpaul.png"),
      "lip" => Array("filename"=>"lip.png"),
      "sale1" => Array("filename"=>"sale1.png")
    );
    return $dict;
  }
  
  public function avatar($faceFile, $x, $y, $width="", $height=""){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    }
    
    $dict = $this->get_avatar_list(false);
    $faceFile = AVATARPATH . DIRECTORY_SEPARATOR . $dict[$faceFile]["filename"];
    $face = imagecreatefrompng($faceFile);
    list($bg_w, $bg_h) = getimagesize($faceFile); 
    $x = $x?$x:0;
    $y = $y?$y:0;
    $width = $width?$width:$bg_w;
    $height = $height?$height:$bg_h;
    //making sure that alpha blending is enabled
	  imageAlphaBlending($face, true);
	  //making sure to preserve the alpha info
	  imageSaveAlpha($face, true);
	  imagecopyresampled($this->ImageStream, $face, $x,$y,0,0, $width, $height, $bg_w, $bg_h);
    
  }
	
	
	
	///////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
  
  
  public function rotate($degrees, $bgd_color, $ignore_transparent=0){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    $background_color = imagecolorallocate ($this->ImageStream, 0, 0, 0);
    // Rotate
    $this->ImageStream = imagerotate($this->ImageStream, $degrees, $bgd_color, $ignore_transparent);
    
    $degrees = $degrees * 3.1415926 * 2 / 360;
    //print sin($degrees) ." " .  cos($degrees). "<br>";
    $newWidth = abs(sin($degrees))*$this->height + abs(cos($degrees))*$this->width;
    $newHeight = abs(sin($degrees))*$this->width + abs(cos($degrees))*$this->height;
    
    $this->width = $newWidth;
    $this->height = $newHeight;
    $this->setImageOrientation();
    
  }
  
  public function mirror($vertical=true){
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
    $temp = imagecreatetruecolor($this->width, $this->height);
    if($vertical){
      //vertical
      imagecopyresampled($temp, $this->ImageStream, 0, 0, 0, ($this->height-1), $this->width, $this->height, $this->width, 0-$this->height);
    }else{
      imagecopyresampled($temp, $this->ImageStream, 0, 0, ($this->width-1), 0, $this->width, $this->height, 0-$this->width, $this->height);
    }
    imagedestroy($this->ImageStream);
    $this->ImageStream = $temp;
  }
  	
	
	public function convert($sTargetType) {
  /* convert image to given type [ jpg | gif | png ] */
    if (!$this->ImageStream) {
		  $this->printError('image not loaded',__METHOD__,__LINE__); 
    } 
		
		switch($sTargetType) {
		  case 'gif': 
			  $this->setImageType(1);
			  break;
			case 'jpg': 
			  $this->setImageType(2);
			  break;
			case 'png': 
			  $this->setImageType(3);
			  break;
		  default: $this->printError('invalid imagetype',__METHOD__,__LINE__); 
		}	  
	}			
}
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -