<?PHP /* @ file:pullImageEx.php @ explain : draw text on picture @ author : 左鹞飞 zuo_yfATyahoo.com @ create time: 2005/02/16 */ class pullImageEx{ var $font_size=3; var $origin_file_path=""; var $dest_file_path=""; var $text="testing"; var $text_color="FFFFFF"; var $bg_color="000000"; var $textlines=1; var $line_space=4; var $bg_rectangle_width=120; var $bg_rectangle_height=80; var $Opacity=50; function pullImageEx() { } function setBgRectangle($w=120,$h=80){$this->bg_rectangle_width=$w;$this->bg_rectangle_height=$h;} function setOriginFilePath($fp=""){$this->origin_file_path=$fp;} function setDestFilePath($fp=""){$this->dest_file_path=$fp;} function setText($txt="testing"){$this->text=$txt;} function setLineSpace($ls=4){$this->line_space=$ls;} function setTextFontSize($fs=1){$this->font_size=$fs;} function setTextColor($tc="FFFFFF"){$this->text_color=$tc;} function setRectangleBgColor($bc="000000"){$this->bg_color=$bc;} function setOpacity($opacities=50){$this->Opacity=$opacities;} function colordecode($hex){ $code[r] = hexdec(substr($hex, 0 ,2)); $code[g] = hexdec(substr($hex, 2 ,2)); $code[b] = hexdec(substr($hex, 4 ,2)); return $code; } function drawText() { $im=$this->createOverlayPic($this->bg_rectangle_width,$this->bg_rectangle_height,$this->bg_color,$this->text_color,$this->font_size,$this->text,$this->line_space); $this->mergePix($this->origin_file_path,$im,$this->dest_file_path,0,$this->Opacity); } function preProcessText($text) { } function createOverlayPic($pic_width,$pic_height,$pic_bg_color,$txt_color,$txt_font_size,$text,$line_space) { $im = @imagecreate ($pic_width, $pic_height) or die ("Cannot Initialize new GD image stream"); //set backgroud canvas $bg_color=$this->colordecode($pic_bg_color); $background_color = imagecolorallocate ($im, $bg_color[r], $bg_color[g], $bg_color[b]); @imagefilledrectangle ($im, 0, 0, $pic_width, $pic_height, $background_color); $t_color=$this->colordecode($txt_color); $text_color = imagecolorallocate ($im, $t_color[r], $t_color[g], $t_color[b]); //get font pix size $text_ary=explode("\r\n",$text); $lines=sizeof($text_ary); $val_span=ImageFontHeight($txt_font_size) + $line_space; $text_height=$lines*$val_span; $init_height=($pic_height/2)-($text_height/2); for($i=0;$i<$lines;$i++) { $c_width = ImageFontWidth($txt_font_size) * strlen($text_ary[$i]); $dest_x = ( $pic_width / 2 ) - ( $c_width / 2 ); $dest_y = $init_height + $val_span*$i; imagestring ($im, $txt_font_size, $dest_x, $dest_y, $text_ary[$i], $text_color); } //imagepng ($im); return $im; } function mergePix($canvas_png,$overlay_img, $targetfile, $pos=0,$Opacity=50) { //Get the resource handle of the pictures //$overlay_img = imageCreateFromPng($overlay_png); $canvas_img = imageCreateFromPNG($canvas_png);
//Get the sizes of both pix $canvas_png_width=imageSX($canvas_img); $canvas_png_height=imageSY($canvas_img); $overlay_png_width=imageSX($overlay_img); $overlay_png_height=imageSY($overlay_img);
//middle if( $pos == 0 ) { $dest_x = ( $canvas_png_width / 2 ) - ( $overlay_png_width / 2 ); $dest_y = ( $canvas_png_height / 2 ) - ( $overlay_png_height / 2 ); }
//top left if( $pos == 1 ) { $dest_x = 0; $dest_y = 0; }
//top right if( $pos == 2 ) { $dest_x = $canvas_png_width - $overlay_png_width; $dest_y = 0; }
//bottom right if( $pos == 3 ) { $dest_x = $canvas_png_width - $overlay_png_width; $dest_y = $canvas_png_height - $overlay_png_height; }
//bottom left if( $pos == 4 ) { $dest_x = 0; $dest_y = $canvas_png_height - $overlay_png_height; }
//top middle if( $pos == 5 ) { $dest_x = ( ( $canvas_png_width - $overlay_png_width ) / 2 ); $dest_y = 0; }
//middle right if( $pos == 6 ) { $dest_x = $canvas_png_width - $overlay_png_width; $dest_y = ( $canvas_png_height / 2 ) - ( $overlay_png_height / 2 ); } //bottom middle if( $pos == 7 ) { $dest_x = ( ( $canvas_png_width - $overlay_png_width ) / 2 ); $dest_y = $canvas_png_height - $overlay_png_height; }
//middle left if( $pos == 8 ) { $dest_x = 0; $dest_y = ( $canvas_png_height / 2 ) - ( $overlay_png_height / 2 ); } //The main thing : merge the two pix imageCopyMerge($canvas_img, $overlay_img,$dest_x,$dest_y,0,0,$overlay_png_width,$overlay_png_height,$Opacity);
//Create a png out of the modified picture imagepng ($canvas_img,"$targetfile"); imagedestroy($overlay_img); imagedestroy($canvas_img); } } ?>
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >>
|