php圖片中寫入字符串然後生成圖片下載到本地

<?php
/**
 * 生成卡片得類
 * Enter description here ...
 * @author perry
 * @time   2014-03-03 10:02:20
 */

class CreateImg{
	public $destination;  //默認圖片
	public $fontname;     //默認的字體
	public $font_size;    //字體大小
	public $company  ;    //字符串
	public $booth;        //字符串
	public $im;           //圖片對象
	public $filedir;      //圖片零時目錄
	function CreateImg($destination,$company,$booth,$fontname='font/ADOBEGOTHICSTD-BOLD.OTF',$font_size='14'){
	   $this->destination = $destination;
	   $this->company=$company;
	   $this->booth = $booth;
	   $this->fontname = $fontname;
	   $this->font_size = $font_size;
	}
	
	/**
	 * 生成一個圖片
	 * Enter description here ...
	 * @param unknown_type $dir  圖片零時保存目錄
	 */
	function createNew($dir='attachment'){
		//Header('Content-type: image/jpg');
		$this->im = imagecreatefromjpeg($this->destination); 
		$black = imagecolorallocate($this->im, 0, 160, 236); //字體顏色
		//文字放在圖片位置
		imagettftext($this->im, $this->font_size,0, 395, 410, $black, $this->fontname, $this->company);  
        imagettftext($this->im, $this->font_size,0, 365, 437, $black, $this->fontname, $this->booth);
        //保存目录是否存在,否則創建一個
        if(!is_dir($dir)){
        	mkdir($dir);
        }
        $this->filename=date('YmdHis',time()).rand(10000,999999).".jpg";  //新文件名称
        $this->filedir=$dir."/".$this->filename;
        imagejpeg($this->im);
        imagedestroy($this->im); 
        
	}
	
	/**
	 * 下載圖片到本地
	 * Enter description here ...
	 * @param unknown_type $ispre 是否保存在空間:1保存  0不保存
	 */
	
	function downNew($ispre=0){
		Header("Content-type: application/octet-stream");
		Header("Accept-Ranges: bytes");
		Header("Accept-Length: ".filesize($this->filedir));
		Header("Content-Disposition: attachment; filename=e-invitation.jpg");
		$fh = fopen($this->filedir, 'rb');
		fpassthru($fh);
		fclose($fh);
		empty($ispre)? unlink($this->filedir):null;
		exit();
	}
}

  

posted @ 2014-03-04 18:27  蚂蚁小圆  阅读(176)  评论(0编辑  收藏  举报