PHP 图像尺寸大小缩放(缩略图)及图片水印等处理并以 MySQL 存储

作者:wiLdGoose 发布时间:2016 年 10 月 2 日 分类:技术 Technology

这是在一个 BT 的运行环境下诞生的需求,折腾数日,写下来自己马克一下。

1、需求:

1.1、表单上传本地图片;
1.2、以指定最大宽或指定最大高处理缩略图;
1.3、以指定最大宽高缩放原图并添加图片水印;
1.4、缩略图及水印图须连同原图一同入库存储。

2、函数部分:

2.1、获取图片信息函数

function get_image_info($file) {
    $result = getimagesize($file);
    $info = array();
    $info['width'] = $result[0];
    $info['height'] = $result[1];
    $info['type'] = $result[2];
    return $info;
}

2.2、原图宽高与缩放宽高运算函数

function fix_image_size($source_width, $source_height, $max_width, $max_height) {
    $size = array();
    if ($max_width == false) {
        if ($max_height < $source_height) {
            $size['width'] = $max_width < $source_width ? floor($max_height / ($source_height / $source_width)) : $source_width;
            $size['height'] = $max_height;
        }
        else {
            $size['width'] = $source_width;
            $size['height'] = $source_height;
        }
    }
    elseif ($max_height == false) {
        if ($max_width < $source_width) {
            $size['width'] = $max_width;
            $size['height'] = $max_height < $source_height ? floor($max_width / ($source_width / $source_height)) : $source_height;
        }
        else {
            $size['width'] = $source_width;
            $size['height'] = $source_height;
        }
    }
    else {
        if (($max_width && $source_width > $max_width) || ($max_height && $source_height > $max_height)) {
            if ($max_width && $source_width > $max_width) {
                $width_ratio = $max_width / $source_width;
                $resize_width_tag = true;
            }
            if ($max_height && $source_height > $max_height) {
                $height_ratio = $max_height / $source_height;
                $resize_height_tag = true;
            }
            if ($resize_width_tag && $resize_height_tag) $ratio = $width_ratio < $height_ratio ? $width_ratio : $height_ratio;
            if ($resize_width_tag && !$resize_height_tag) $ratio = $width_ratio;
            if ($resize_height_tag && !$resize_width_tag) $ratio = $height_ratio;
            $size['width'] = floor($source_width * $ratio);
            $size['height'] = floor($source_height * $ratio);
        }
        else {
            $size['width'] = $source_width;
            $size['height'] = $source_height;
        }
    }
    return $size;
}

2.3、缩略图处理函数

function resize_image($source_image_string, $source_width, $source_height, $resize_width, $resize_height) {
    if (!empty($source_image_string) && !empty($source_width) && !empty($source_height) && (!empty($resize_width) || !empty($resize_height))) {
        $resize_image = imagecreatetruecolor($resize_width, $resize_height);
        $source_image = imagecreatefromstring(base64_decode($source_image_string));
        if (function_exists('imagecopyresampled')) imagecopyresampled($resize_image, $source_image, 0, 0, 0, 0, $resize_width, $resize_height, $source_width, $source_height);
        else imagecopyresized($resize_image, $source_image, 0, 0, 0, 0, $resize_width, $resize_height, $source_width, $source_height);
        $image_stream = array();
        ob_start();
        imagejpeg($resize_image, null, 100);
        $image_stream['data'] = ob_get_contents();
        $image_stream['length'] = ob_get_length();
        ob_end_clean();
        if (!empty($image_stream['data']) && $image_stream['length'] > 0) {
            imagedestroy($source_image);
            imagedestroy($resize_image);
            return $image_stream;
        }
        else return false;
    }
    else return false;
}

2.4、图片水印处理函数

function watermark_image($source_image_string, $source_width, $source_height, $watermark_image_file) {
    if (!empty($source_image_string) && !empty($source_width) && !empty($source_height) && !empty($watermark_image_file)) {
        $source_image = imagecreatefromstring(base64_decode($source_image_string));
        $watermark_image_info = get_image_info($watermark_image_file);
        if ($watermark_image_info['type'] == 1) $watermark_image = imagecreatefromgif($watermark_image_file);
        elseif ($watermark_image_info['type'] == 2) $watermark_image = imagecreatefromjpeg($watermark_image_file);
        elseif ($watermark_image_info['type'] == 3) $watermark_image = imagecreateFrompng($watermark_image_file);
        else return false;
        if (empty($watermark_image)) return false;
        $x = $source_width - $watermark_image_info['width'];
        $y = $source_height - $watermark_image_info['height'];
        imagecopy($source_image, $watermark_image, $x, $y, 0, 0, $watermark_image_info['width'], $watermark_image_info['height']) or die('Error');
        ob_start();
        imagejpeg($source_image, null, 100);
        $image_stream['data'] = ob_get_contents();
        $image_stream['length'] = ob_get_length();
        ob_end_clean();
        if (!empty($image_stream['data']) && $image_stream['length'] > 0) {
            imagedestroy($source_image);
            imagedestroy($watermark_image);
            return $image_stream;
        }
        else return false;
    }
    return false;
}

3、调用部分

3.1、表单数据获取

$attach_type = $_FILES['attach']['type'];
$attach_tmp_name = $_FILES['attach']['tmp_name'];
$attach_error = $_FILES['attach']['error'];
$attach_size = $_FILES['attach']['size'];

3.2、元数据处理

$source_image_file = $attach_tmp_name;
$source_image_info = get_image_info($source_image_file);
$source_image_string = base64_encode(file_get_contents($source_image_file));

3.3、入库原图处理

$fp_original_image = fopen($attach_tmp_name, 'rb');
if (!$fp_original_image) die('Error');
$original_image_content = addslashes(fread($fp_original_image, filesize($attach_tmp_name)));
fclose($fp_original_image);
unlink($attach_tmp_name);

3.4、缩略图处理

$thumbnail_image_width = 300;
$thumbnail_image_height = false;
$thumbnail_image_size = fix_image_size($source_image_info['width'], $source_image_info['height'], $thumbnail_image_width, $thumbnail_image_height);
$thumbnail_image = resize_image($source_image_string, $source_image_info['width'], $source_image_info['height'], $thumbnail_image_size['width'], $thumbnail_image_size['height']);
if (empty($thumbnail_image['data']) || $thumbnail_image['length'] <= 0) die('Error');

3.5、图片水印定义

$watermark_image_file = 'path/to/watermark.png';
$watermark_image_width = 900;
$watermark_image_height = 600;
$watermark_image_size = fix_image_size($source_image_info['width'], $source_image_info['height'], $watermark_image_width, $watermark_image_height);

3.6、处理水印前缩放处理

$watermark_resize_image = resize_image($source_image_string, $source_image_info['width'], $source_image_info['height'], $watermark_image_size['width'], $watermark_image_size['height']);
if (empty($watermark_resize_image['data']) || $watermark_resize_image['length'] <= 0) die('Error');

3.7、水印处理

$watermark_image = watermark_image(base64_encode($watermark_resize_image['data']), $watermark_image_size['width'], $watermark_image_size['height'], $watermark_image_file);
if (empty($watermark_image['data']) || $watermark_image['length'] <= 0) die('Error');

4、返回值

4.1、图片类型

$attach_type

4.2、原图

4.2.1、原图二进制数据

$original_image_content

4.2.2、原图大小

$attach_size

4.3、缩略图

4.3.1、缩略图二进制数据

addslashes($thumbnail_image['data'])

4.3.2、缩略图大小

$thumbnail_image['length']

4.4、水印图

4.4.1、水印图二进制数据

addslashes($watermark_image['data'])

4.4.2、水印图大小

$watermark_image['length']

5、入库 SQL 构造

略。

- EOF -

标签: php, mysql, form, image

仅有一条评论 »

  1. 宁波宣传片 宁波宣传片 2017 年 3 月 28 日 20 时 29 分

    学习了

添加新评论 »