-
注意视频里写的两个info:
函数里的$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]将水印图片从原图的左上角顶点开始复制,0,0代表有多高复制多高,有多宽复制多宽。
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();
查看全部 -
我的经过测试没有问题(代码如下):
<?php //封装成类-压缩图片 class Image{ private $image; private $info; //1.打开一张图片,读取到内存中 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='您好,翌创'; $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; /** * 构造函数 */ 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] ); //在内存中创建一个相同类型的图片 $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.通过图片编号来确认图片类型,取消false参数获取值为 .png ; $type = image_type_to_extension($info[2],false); // print_r($type); //4.在内存中创建一个和打开图片类型一样的图像 $fun = "imagecreatefrom{$type}"; //相当于 $fun = imagecreatefrompng(); //5.把图片复制到内存中 $image = $fun($src_url); //相当于 imagecreatefrompng($src) /*操作图片*/ //1.设置字体路径 $font = "image/msyhl.ttc"; //2.填写水印内容 $content = "xiaoweiba.ml"; //3.设置字体颜色RGB值和透明度, //参数1:内存中的图片; 参数2:颜色; 参数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就调用 imagejpeg()方法,如果是peg就会调用imagepng()方法 $func($image); //2.保存图片 $func($image,'image/newimage.'.$type); //imagepng($image,'image/newimage.png'); //显示图片 //输出生成的图片,配置文件路径 $src_new = "image/newimage.png"; //无法直接输出图片,必须将图片复制到内存中。 $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); } } ?>查看全部
举报