-
注意視頻里寫的兩個(gè)info:
函數(shù)里的$info和$this->info是不同的變量查看全部 -
<?php
class Image{
private $image;
private $info;
public function __construct($src)
{
$info2 = getimagesize($src);
$this->info = array(
'width' => $info2[0],
'height' => $info2[1],
'type' => image_type_to_extension($info2['2'],false),
'mime' => $info2['mime']
);
$fun = "imagecreatefrom{$this->info['type']}";
$this->image = $fun($src);
}
public function thumb($width,$height)
{
$image_thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($image_thumb, $this->image, 0, 0, 0, 0, $width, $height, $this->info['width'],$this->info['height']);
imagedestroy($this->image);
$this->image = $image_thumb;
}
public function show()
{
header("Content-type:".$this->info['mime']);
$funs = "image{$this->info['type']}";
$funs($this->image);
}
public function save($newimage)
{
$funs = "image{$this->info['type']}";
$funs($this->image,$newname.'.'.$this->info['type']);
}
public function __destruct()
{
imagedestroy($this->image);
}
}
?>
查看全部 -
<title>給圖片添加圖片水印</title>
<?php
/*一、打開圖片*/
$src = "001.png";
$info = getimagesize($src);
$type = image_type_to_extension($info[2],false);
$fun = "imagecreatefrom{$type}";
$image = $fun($src);
/*二、操作圖片*/
$image_Mark = "logo.png";
$info2 = getimagesize($image_Mark);
$type2 = image_type_to_extension($info[2],false);
$fun2 = "imagecreatefrom{$type2}";
$water = $fun2($image_Mark);
//合并圖片 ?0,0,$info[0],$info[1]將水印圖片從原圖的左上角頂點(diǎn)開始復(fù)制,0,0代表有多高復(fù)制多高,有多寬復(fù)制多寬。
imagecopymerge($image,$water,20,30,0,0,$info2[0],$info2[1],30);
imagedestroy($water);
/*三、輸出圖片*/
ob_clean();
header("content-type:".$info['mime']);
$funs = "image{$type}";
$funs($image);
$funs($image,"hi.".$type); ?//保存圖片
/*四、銷毀圖片*/
imagedestroy($image);
?>
查看全部 -
teat.php
<?php? ????require?"image_class.php"; ????$src?=?'1.jpg'; ????$image?=?new?Image($src); ????$image->thumb(300,200); ????$image->show();
查看全部 -
我的經(jīng)過測試沒有問題(代碼如下):
<?php? //封裝成類-壓縮圖片 class?Image{ ????private?$image; ????private?$info; ????//1.打開一張圖片,讀取到內(nèi)存中 ????public?function?__construct($src){ ????????$info?=?getimagesize($src);//獲取圖片信息放在$info里 ????????$this->info?=?array( ????????'width'?=>?$info[0], ????????'height'?=>?$info[1], ????????'type'?=>?image_type_to_extension($info[2],false), ????????'mime'?=>?$info['mime']); ????????$fun?=?"imagecreatefrom{$this->info['type']}"; ????????$this->image?=?$fun($src); } ????????//2.操作圖片(壓縮) ????????public?function?thumb($width,$height){ ????????????$image_thumb?=?imagecreatetruecolor($width,$height); ????????????imagecopyresampled($image_thumb,$this->image,0,0,0,0,$width,$height,$this->info['width'],$this->info['height']); ????????????imagedestroy($this->image); ????????????$this->image?=?$image_thumb; ????????} ????????//3.在瀏覽器中輸出圖片 ????????public?function?show(){ ????????????header("Content-type:".$this->info['mime']); ????????????$funs?=?"image{$this->info['type']}"; ????????????$funs($this->image); ????????} ????????//4.把圖片保存到硬盤里 ????????public?function?save($newname){ ????????????$funs?=?"image{$this->info['type']}"; ????????????$funs($this->image,$newname.'.'.$this->info['type']); ????????} //5.銷毀圖片 ????????public?function?__destruct(){ ????????????????imagedestroy($this->image); ????????} ?????}
查看全部 -
筆記1查看全部
-
成功了,代碼如下: header('Content-type:text/html;charset=utf8'); //打開圖片 $pic_src='p02.jpg'; $pic_info=getimagesize($pic_src); $pic_type=image_type_to_extension($pic_info[2],false); $fun="imagecreatefrom{$pic_type}"; $image=$fun($pic_src); //操作圖片 $font='msyh.ttf'; $content='您好,翌創(chuàng)'; $col=imagecolorallocatealpha($image,255,255,255,50); imagettftext($image,20,0,20,30,$col,$font,$content); //輸出圖片 header("Content-type:".$pic_info['mime']); $func="image{$pic_type}"; $func($image); //保存圖片 $func($image,'images/new.'.$pic_type); //清理圖片 imagedestory($images);查看全部
-
test.php <?php require "image.class.php"; $src = "image/test.jpg"; $image = new Image($src); $image->image_Thumb(300,200); $image->image_Show(); $image->image_Destroy(); ?>查看全部
-
image.class.php <?php /** * 圖片工具類 */ class Image{ private $image; private $info; /** * 構(gòu)造函數(shù) */ public function __construct($src){ //獲取圖片信息 $info = getimagesize($src); $this->info = array( 'width' => $info[0], 'height' => $info[1], 'type' => image_type_to_extension($info[2],false), 'mime' => $info[3] ); //在內(nèi)存中創(chuàng)建一個(gè)相同類型的圖片 $new_cache = "imagecreatefrom{$this->info['type']}"; $this->image = $new_cache($src); }查看全部
-
/** * 操作圖片 縮略圖 */ public function image_Thumb($dstWidth,$dstHeight){ $image_thumb = imagecreatetruecolor($dstWidth,$dstHeight); imagecopyresampled($image_thumb,$this->image,0,0,0,0,$dstWidth,$dstHeight,$this->info['width'],$this->info['height']); //原始圖片已使用完畢 可以銷毀 imagedestroy($this->image); $this->image = $image_thumb; } /** * 顯示圖片 */ public function image_Show(){ header("content-type:".$this->info['mime']); $new_cache = "image{$this->info['type']}"; $new_cache($this->image); } /** * 銷毀圖片 */ public function image_Destroy(){ imagedestroy($this->image); } } ?>查看全部
-
<?php /*打開圖片*/ //1.配置圖片路徑 $src_url = "image/GD_test.png"; //2.獲取圖片信息 $info = getimagesize($src_url); // echo "<pre>"; //print_r($info); //3.通過圖片編號(hào)來確認(rèn)圖片類型,取消false參數(shù)獲取值為 .png ; $type = image_type_to_extension($info[2],false); // print_r($type); //4.在內(nèi)存中創(chuàng)建一個(gè)和打開圖片類型一樣的圖像 $fun = "imagecreatefrom{$type}"; //相當(dāng)于 $fun = imagecreatefrompng(); //5.把圖片復(fù)制到內(nèi)存中 $image = $fun($src_url); //相當(dāng)于 imagecreatefrompng($src) /*操作圖片*/ //1.設(shè)置字體路徑 $font = "image/msyhl.ttc"; //2.填寫水印內(nèi)容 $content = "xiaoweiba.ml"; //3.設(shè)置字體顏色RGB值和透明度, //參數(shù)1:內(nèi)存中的圖片; 參數(shù)2:顏色; 參數(shù)3:透明度 //注意?。?! png圖片無法正常渲染字體顏色!!?。? $col = imagecolorallocatealpha($image,0,0,0,15);查看全部
-
imagettftext($image,20,0,20,30,$col,$font,$content); /*輸出圖片*/ //1.瀏覽器輸出 header("Content-type:".$info['mime']); $func = "image{$type}"; //如果是jpeg就調(diào)用 imagejpeg()方法,如果是peg就會(huì)調(diào)用imagepng()方法 $func($image); //2.保存圖片 $func($image,'image/newimage.'.$type); //imagepng($image,'image/newimage.png'); //顯示圖片 //輸出生成的圖片,配置文件路徑 $src_new = "image/newimage.png"; //無法直接輸出圖片,必須將圖片復(fù)制到內(nèi)存中。 $fun_new = imagecreatefrompng($src_new); //輸出圖片 imagepng($fun_new); // imagepng(imagecreatefrompng("image/newimage.png")); /*銷毀圖片*/ imagedestroy($image); imagedestroy($fun_new); ?>查看全部
-
/* 輸出圖片 */ public function show() { header("Content-type:".$this->info['mime']); $funs="image{$this->info['type']}"; $funs($this->image); } /* 保存圖片 */ public function save($newImage) { $funs="image{$this->info['type']}"; $funs($this->image,$newImage.'.'.$this->info['type']); } /* 銷毀圖片 */ public function __destruct() { imagedestroy($this->image); } } ?>查看全部
舉報(bào)