正確代碼的寫法
<?php
class Image{
/*打開圖片*/
private $info;
private $image;
private $imagemark;
private $type;
public function __construct($src){
//配置圖片路徑
$imagesrc=$src;
//獲取圖片信息
$this->info=getimagesize($imagesrc);
$this->info=array(
"width"=>$this->info[0],
"height"=>$this->info[1],
"type"=>image_type_to_extension($this->info[2],FALSE),
"mime"=>$this->info["mime"],
);
//根據(jù)圖片的編號獲得圖片的類型
$this->type=$this->info["type"];
//在內(nèi)存中創(chuàng)建一個(gè)和我們圖片類型一樣的對象
$func="imagecreatefrom{$this->type}";
$this->image=$func($imagesrc);
}
/*操作圖片*/
/*給圖片添加文字水印*/
public function fontmark(){
//設(shè)置字體路徑
$font="msyh.ttf";
//設(shè)置文字內(nèi)容
$content="hello world";
//設(shè)置文字水印透明度
$color=imagecolorallocatealpha($this->image,255,0,0,50);
//添加水印
imagettftext($this->image,20,0,20,200,$color,$font,$content);
}
/*給圖片添加圖片水印*/
public function imagemark(){
//配置圖片路徑
$imagemark='logo.jpg';
//獲取圖片信息
$this->info=getimagesize($imagemark);
$this->info=array(
"width"=>$this->info[0],
"height"=>$this->info[1],
"type"=>image_type_to_extension($this->info[2],FALSE),
"mime"=>$this->info["mime"],
);
//根據(jù)圖片的編號獲得圖片的類型
$this->type=$this->info["type"];
//在內(nèi)存中創(chuàng)建一個(gè)和我們圖片類型一樣的對象
$func="imagecreatefrom{$this->type}";
$this->imagemark=$func($imagemark);
imagecopymerge($this->image,$this->imagemark,0,0,100,90,200,40,100);
}
/*將圖片按照一定大小進(jìn)行縮略*/
public function thumb(){
//創(chuàng)建一個(gè)大小固定的真空彩色圖像
$imagethumb=imagecreatetruecolor(100,100);
//將原圖縮略成我們所指定的圖像大小
imagecopyresampled($imagethumb,$this->image,0,0,0,0,100,100,$this->info["width"],$this->info["height"]);
$this->image=$imagethumb;
}
/*輸出圖片*/
public function outputimage(){
//print_r($this->type);die();
header("content-type:".$this->info["mime"]);
$func="image{$this->type}";
$func($this->image);
}
/*銷毀圖片*/
function destroy(){
imagedestroy($this->image);
}
}
2017-06-20
嗯,有什么問題嗎?