[PHP GD库]①④--图片水印封装

[PHP GD库]①④--图片水印封装_第1张图片
Paste_Image.png
function water_pic($dstName, $srcName, $pos = 0, $dest = 'waterPic', $pre = 'waterPic', $pct = 50, $delSource = false)
{
    $dstInfo = getImageInfo($dstName);
    $srcInfo = getImageInfo($srcName);
    $dst_im = $dstInfo['createFun']($dstName);
    $src_im = $srcInfo['createFun']($srcName);
    $src_width = $srcInfo['width'];
    $src_height = $srcInfo['height'];
    $dst_width = $dstInfo['width'];
    $dst_height = $dstInfo['height'];
    switch ($pos) {
        case 0:
            $x = 0;
            $y = 0;
            break;
        case 1:
            $x = ($dst_width - $src_width) / 2;
            $y = 0;
            break;
        case 2:
            $x = $dst_width - $src_width;
            $y = 0;
            break;
        case 3:
            $x = 0;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 4:
            $x = ($dst_width - $src_width) / 2;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 5:
            $x = $dst_width - $src_width;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 6:
            $x = 0;
            $y = $dst_height - $src_height;
            break;
        case 7:
            $x = ($dst_width - $src_width) / 2;
            $y = $dst_height - $src_height;
            break;
        case 8:
            $x = $dst_width - $src_width;
            $y = $dst_height - $src_height;
            break;
        default:
            $x = 0;
            $y = 0;
            break;
    }

    imagecopymerge($dst_im, $src_im, $x, $y, 0, 0, $src_width, $src_height, $pct);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 0777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}_{$randNum}" . $dstInfo['ext'];
    $destination = $dest ? $dest . "/" . $dstName : $dstName;
    $dstInfo['outFun']($dst_im, $destination);
    imagedestroy($src_im);
    imagedestroy($dst_im);
    if ($delSource) {
        @unlink($srcName);
    }
    return $destination;
}

test.php


image.func.php

 int 756
     * 1 => int 960
     * 2 => int 2
     * 3 => string 'width="756" height="960"' (length=24)
     * 'bits' => int 8
     * 'channels' => int 3
     * 'mime' => string 'image/jpeg' (length=10)
     */
    $fileInfo['width'] = $info[0];//756
    $fileInfo['height'] = $info[1];//960
    $mime = image_type_to_mime_type($info[2]);//image/jpeg
    $createFun = str_replace('/', 'createfrom', $mime);
    $outFun = str_replace('/', '', $mime);
    $fileInfo['createFun'] = $createFun;
    $fileInfo['outFun'] = $outFun;
    $fileInfo['ext'] = strtolower(image_type_to_extension($info[2]));
    return $fileInfo;
}

/**
 * 形成缩略图
 * @param $filename 文件名
 * @param string $dest 缩略图保存路径,默认'thumb'
 * @param string $pre 默认前缀thumb_
 * @param null $dst_w 最大宽度
 * @param null $dst_h 最大高度
 * @param float $scale 默认缩放比例
 * @param boolean $delSource 是否删除源文件标志
 * @return string 最终保存路径及文件名
 *
 */
function thumb($filename, $dest = 'thumb', $pre = 'thumb_',
               $dst_w = null, $dst_h = null, $scale = 0.5, $delSource = false)
{
    $fileInfo = getImageInfo($filename);
    $src_w = $fileInfo['width'];
    $src_h = $fileInfo['height'];
//如果指定最大宽度和高度,按照等比例缩放进行处理
    if (is_numeric($dst_w) && is_numeric($dst_h)) {
        $ratio_orig = $src_w / $src_h;
        if ($dst_w / $dst_h > $ratio_orig) {
            $dst_w = $dst_h * $ratio_orig;
        } else {
            $dst_h = $dst_w / $ratio_orig;
        }
    } else {
        $dst_w = ceil($src_w * $scale);
        $dst_h = ceil($src_h * $scale);
    }
    $dst_image = imagecreatetruecolor($dst_w, $dst_h);
    $src_image = $fileInfo['createFun']($filename);
    imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 07777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
    $destination = $dest ? $dest . '/' . $dstName : $dstName;
    $fileInfo['outFun']($dst_image, $destination);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if ($delSource) {
        @unlink($filename);
    }
    return $destination;
}

/**文字水印
 * @param $filename
 * @param string $fontfile
 * @param string $text
 * @param string $dest
 * @param string $pre
 * @param bool $delSource
 * @param int $r
 * @param int $g
 * @param int $b
 * @param int $alpha
 * @param int $angle
 * @param int $size
 * @param int $x
 * @param int $y
 * @return string
 */
function water_text($filename, $fontfile = 'fonts/couri.ttf', $text = 'Imooc', $dest = 'waterText', $pre = 'waterText', $delSource = false, $r = 255,
                    $g = 0, $b = 0, $alpha = 60, $angle = 0, $size = 30, $x = 0, $y = 30)
{
    $fileInfo = getImageInfo($filename);
    $image = $fileInfo['createFun']($filename);
    $color = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
    imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 0777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}{$randNum}" . $fileInfo['ext'];
    $destination = $dest ? $dest . '/' . $dstName : $dstName;
    $fileInfo['outFun']($image, $destination);
    imagedestroy($image);
    if ($delSource) {
        @unlink($filename);
    }
    return $destination;
}

function water_pic($dstName, $srcName, $pos = 0, $dest = 'waterPic', $pre = 'waterPic', $pct = 50, $delSource = false)
{
    $dstInfo = getImageInfo($dstName);
    $srcInfo = getImageInfo($srcName);
    $dst_im = $dstInfo['createFun']($dstName);
    $src_im = $srcInfo['createFun']($srcName);
    $src_width = $srcInfo['width'];
    $src_height = $srcInfo['height'];
    $dst_width = $dstInfo['width'];
    $dst_height = $dstInfo['height'];
    switch ($pos) {
        case 0:
            $x = 0;
            $y = 0;
            break;
        case 1:
            $x = ($dst_width - $src_width) / 2;
            $y = 0;
            break;
        case 2:
            $x = $dst_width - $src_width;
            $y = 0;
            break;
        case 3:
            $x = 0;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 4:
            $x = ($dst_width - $src_width) / 2;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 5:
            $x = $dst_width - $src_width;
            $y = ($dst_height - $src_height) / 2;
            break;
        case 6:
            $x = 0;
            $y = $dst_height - $src_height;
            break;
        case 7:
            $x = ($dst_width - $src_width) / 2;
            $y = $dst_height - $src_height;
            break;
        case 8:
            $x = $dst_width - $src_width;
            $y = $dst_height - $src_height;
            break;
        default:
            $x = 0;
            $y = 0;
            break;
    }

    imagecopymerge($dst_im, $src_im, $x, $y, 0, 0, $src_width, $src_height, $pct);
    if ($dest && !file_exists($dest)) {
        mkdir($dest, 0777, true);
    }
    $randNum = mt_rand(100000, 999999);
    $dstName = "{$pre}_{$randNum}" . $dstInfo['ext'];
    $destination = $dest ? $dest . "/" . $dstName : $dstName;
    $dstInfo['outFun']($dst_im, $destination);
    imagedestroy($src_im);
    imagedestroy($dst_im);
    if ($delSource) {
        @unlink($srcName);
    }
    return $destination;
}

?>
[PHP GD库]①④--图片水印封装_第2张图片
Paste_Image.png

你可能感兴趣的:([PHP GD库]①④--图片水印封装)