设为首页 加入收藏

TOP

PHP-生成缩略图和添加水印图-学习笔记(二)
2017-10-10 11:31:34 】 浏览:10166
Tags:PHP- 生成 缩略 添加 水印 学习 笔记
ht; } else { //计算缩放比率 $per = 1; if (($this->newHeight / $this->height) > ($this->newWidth / $this->width)) { $per = $this->newHeight / $this->height; } else { $per = $this->newWidth / $this->width; } if ($per < 1) { $this->newWidth = $this->width * $per; $this->newHeight = $this->height * $per; } } $this->newImg = $this->_CreateImg($gd_width, $gd_height, $this->type); imagecopyresampled($this->newImg, $this->img, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); }

生成缩略图函数_thumb() ,是按照前面的分析来进行编码。  

   5.3. 添加水印图片函数 _addWaterMark()     

     /** * 添加水印 */ private function _addWaterMark() { $ratio = 1 / 5; //水印缩放比率 $Width = imagesx($this->newImg); $Height = imagesy($this->newImg); $n_width = $Width * $ratio; $n_height = $Width * $ratio; list($markWidth, $markHeight, $markType) = getimagesize($this->waterMarkPath); if ($n_width > $markWidth) $n_width = $markWidth; if ($n_height > $markHeight) $n_height = $markHeight; $Img = $this->_loadImg($this->waterMarkPath, $markType); $Img = $this->_thumb1($Img, $markWidth, $markHeight, $markType, $n_width, $n_height); $markWidth = imagesx($Img); $markHeight = imagesy($Img); imagecopyresampled($this->newImg, $Img, $Width - $markWidth - 10, $Height - $markHeight - 10, 0, 0, $markWidth, $markHeight, $markWidth, $markHeight); imagedestroy($Img); }

在添加水印图片中,用到一个_thumb1()函数来缩放水印图片:

    /** * 缩略图(按等比例) * @param resource $img 图像流 * @param int $width * @param int $height * @param int $type * @param int $new_width * @param int $new_height * @return resource */ private function _thumb1($img, $width, $height, $type, $new_width, $new_height) { if ($width < $height) { $new_width = ($new_height / $height) * $width; } else { $new_height = ($new_width / $width) * $height; } $newImg = $this->_CreateImg($new_width, $new_height, $type); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $newImg; }

   5.4. 完整代码:

<?php

/**
 * 图片处理,生成缩略图和添加水印图片
 * Created by PhpStorm.
 * User: andy
 * Date: 17-1-3
 * Time: 上午11:55
 */
class Image
{
    //原图
    private $imgPath;   //图片地址
    private $width;     //图片宽度
    private $height;    //图片高度
    private $type;      //图片类型
    private $img;       //图片(图像流)

    //缩略图
    private $newImg;    //缩略图(图像流)
    private $newWidth;
    private $newHeight;

    //水印图路径
    private $waterMarkPath;

    //输出图像质量,jpg有效
    private $quality;

    /**
     * Image constructor.
     * @param string $imagePath 图片路径
     * @param string $markPath 水印图片路径
     * @param int $new_width 缩略图宽度
     * @param int $new_height 缩略图高度
     * @param int $quality JPG图片格输出质量
     */
    public function __construct(string $imagePath,
                                string $markPath = null,
                                int $new_width = null,
                                int $new_height = null,
                                int $quality = 75)
    {
        $this->imgPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
        $this->waterMarkPath = $markPath;
        $this->newWidth = $new_width ? $new_width : $this->width;
        $this->newHeight = $new_height ? $new_height : $this->height;
        $this->quality = $quality ? $quality : 75;

        list($this->width, $this->height, $this->type) = getimagesize($this->imgPath);
        $this->img = $this->_loadImg($this->imgPath, $this->type);


        //生成缩略图
        $this->_thumb();
        //添加水印图片
        if (!em
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php-gd库的使用——跟招财圆一起.. 下一篇家具电商物流配送服务的痛点及解..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目