GD库图片处理

JerryXia 发表于 , 阅读 (2)

慕课网 BobWang http://www.imooc.com/learn/329

GD库处理原理总结: 需要再内存中生成和原图相同类型的图片, 使用完需要再内存中清理掉
缩略图的原理: 在内存中生成2个图片, 一张为缩略图, 一张为原图, 将原图复制到缩略图上.

素材准备: 原图; 缩略图; 字体(c:/windows/fonts中复制一个就行了); 开启php的 gd2 扩展

原始代码

/* 打开图片 */$src = 'img/1.jpg'; // 配置图片路径$info = getimagesize($src); // 获取图片信息$type = image_type_to_extension($info[2], false); // 获取图片类型, 默认true, 显示 .$imgCreateFunc = "imagecreatefrom{$type}";$img = $imgCreateFunc($src); // 内存中创建相同类型的图片/* 操作图片 */// 设置水印文字$font = 'img/Inconsolata.otf'; // 设置字体路径$content = 'daydaygo'; // 水印内容$col = imagecolorallocatealpha($img, 255, 255, 255, 50); //设置字体文件颜色和透明度, imagecolorallocatealpha($img, red, green, blue, alpha);//写入文字imagettftext($img, 100, 0, 200, 200, $col, $font, $content);  //imagettftext(image, size, angle, x, y, color, fontfile, text);// 创建水印图片$src2 = 'img/chen.png';$info2 = getimagesize($src2);$type2 = image_type_to_extension($info2[2], false);$imgCreateFunc2 = "imagecreatefrom{$type2}";$img2 = $imgCreateFunc2($src2);// 合并图片imagecopymerge($img, $img2, 20, 30, 0, 0, $info2[0], $info2[1], 50);    //imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct);imagedestroy($img2);// 缩略图// 创建缩略图$imgChange = imagecreatetruecolor(300, 200); //imagecreatetruecolor(width, height)// 将原图复制到缩略图上imagecopyresampled($imgChange, $img, 0, 0, 0, 0, 300, 200, $info[0], $info[1]); //imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)imagedestroy($img);/* 输出图片 */// 浏览器输出header("Content-type:".$info['mime']);$imgOutFunc = "image{$type}";$imgOutFunc($imgChange);//保存图片$imgOutFunc($imgChange,'img/newimg3.'.$type); // 第二个参数为路径/* 销毁图片 */imagedestroy($img);

简单封装的包含缩略图的GD类

/*** my GD class*/class mygd{    /**     * 图片的信息     * @var array     */    private $info;    /**     * 在内存中创建图片     * @var image     */    private $img;    /**     * 在内存中创建一张和需要修改的图片的类型相同的图片     */    function __construct($src)    {        $info = getimagesize($src);        $this->info = [            'width' => $info[0],            'height' => $info[1],            'type' => image_type_to_extension($info[2], false),            'mime' => $info['mime']        ];        $imgCreateFunc = "imagecreatefrom{$this->info['type']}";        $this->img = $imgCreateFunc($src);    }    /**     * 制作缩略图     * @param  int $width  缩略图的宽度     * @param  int $height 缩略图的高度     */    public function thumb($width, $height)    {        $imgThumb = imagecreatetruecolor($width, $height);        imagecopyresampled($imgThumb, $this->img, 0, 0, 0, 0, $width, $height, $this->info['width'], $this->info['height']);        // imagecopyresampled($imgChange, $img, 0, 0, 0, 0, 300, 200, $info[0], $info[1]);        imagedestroy($this->img);        $this->img = $imgThumb;    }    /**     * 输出图片到浏览器     */    public function show()    {        header("Content-type:".$this->info['mime']);        $imgOutFunc = "image{$this->info['type']}";        $imgOutFunc($this->img);    }    /**     * 保存图片     * @param  string $newname 文件名     */    public function save($newname)    {        $imgOutFunc = "image{$this->info['type']}";        $imgOutFunc($this->img, $newname. '.'. $this->info['type']);    }    /**     * 销毁内存中创建的图片     */    function __destruct()    {        imagedestroy($this->img);    }}$src = 'img/1.jpg';$img = new mygd($src);$img->thumb(300,200);$img->show();