search
Homephp教程php手册高质量缩略图的生成函数(多种剪切模式,按高度宽度最佳缩放等)

函数|缩略图

/**

* 可扩展的缩略图生成函数

* 在http://yodoo.com的论坛里可以获得最新版本(注册用户)

* 演示效果也请登录http://yodoo.com看看,该站所有的缩略图(jpg,png)都是使用该函数生成的

*

* 转载请保留完整信息

*

* @author Austin Chin http://yodoo.com

* @version $Revision: 1.7 $

*

*

* version

*

* + 表示增加的功能

* - 表示丢弃的功能

* C 表示修正的功能

* E 表示扩展了功能

*

* v1.5

* makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=1)

*

* v1.6

* + 增加了剪切模式

* + $option 8: 宽度最佳缩放

* + $option 16: 高度最佳缩放

* makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=1, $cutmode=0, $startX=0,

*     $startY=0)

*

* v1.7

* E 返回值改为数组,第一个元素是代码 0 表示正常, 其它位错误代码;第二个元素是错误描述。

* 错误代码:

* -1 源文件不存在。

* -2 不支持的图片输出函数

* -3 不支持的图片创建函数

* -4 HTTP头信息已经输出,无法向浏览器输出图片。

* -5 无法检测输出的图片类型

* + 增加函数 message2Image 可以把字符串输出成图片格式

*/





/**

* 可扩展的缩略图生成函数

*

* @param string $srcFile 源文件

* @param string $srcFile 目标文件

* @param int $dstW 目标图片的宽度(单位:像素)

* @param int $dstH 目标图片的高度(单位:像素)

* @param int $option 附加参数,可以相加使用,如1+2(或者 1|2)表示同时执行1和2的功能。

*      1: 默认,输出到指定文件 2: 图片内容输出到浏览器 4: 不保持图片比例

*      8:宽度最佳缩放 16:高度最佳缩放

* @param int $cutmode 剪切模式 0: 默认模式,剪切模式 1: 左或上 2: 中 3: 右或下

* @param int $startX 剪切的起始横坐标(像素)

* @param int $startY 剪切的起始纵坐标(像素)

* @return array return[0]=0: 正常; return[0] 为错误代码 return[1] string: 错误描述

*/

define(OP_TO_FILE, 1);              //输出到指定文件

define(OP_OUTPUT, 2);               //图片内容输出到浏览器

define(OP_NOT_KEEP_SCALE, 4);       //不保持图片比例, 即使用拉伸

define(OP_BEST_RESIZE_WIDTH, 8);    //宽度最佳缩放

define(OP_BEST_RESIZE_HEIGHT, 16);  //高度最佳缩放



define(CM_DEFAULT,  0);             // 默认模式

define(CM_LEFT_OR_TOP,  1);         // 左或上

define(CM_MIDDLE,       2);         // 中

define(CM_RIGHT_OR_BOTTOM,  3);     // 右或下



function makeThumb($srcFile, $dstFile, $dstW, $dstH, $option=OP_TO_FILE,

    $cutmode=CM_DEFAULT, $startX=0, $startY=0) {



    $img_type = array(1=>"gif", 2=>"jpeg", 3=>"png");

    $type_idx = array("gif"=>1, "jpg"=>2, "jpeg"=>2, "jpe"=>2, "png"=>3);



    if (!file_exists($srcFile)) {

        return array(-1, "Source file not exists: $srcFile.");

    }



    $path_parts = @pathinfo($dstFile);

    $ext = strtolower ($path_parts["extension"]);

    if ($ext == "") {

        return array(-5, "Can't detect output image's type.");

    }

    $func_output = "image" . $img_type[$type_idx[$ext]];

    if (!function_exists ($func_output)) {

        return array(-2, "Function not exists for output:$func_output.");

    }



    $data = @GetImageSize($srcFile);

    $func_create = "imagecreatefrom" . $img_type[$data[2]];

    if (!function_exists ($func_create)) {

        return array(-3, "Function not exists for create:$func_create.");

    }



    $im = @$func_create($srcFile);



    $srcW = @ImageSX($im);

    $srcH = @ImageSY($im);

    $srcX = 0;

    $srcY = 0;

    $dstX = 0;

    $dstY = 0;

   



    if ($option & OP_BEST_RESIZE_WIDTH) {

        $dstH = round($dstW * $srcH / $srcW);

    }



    if ($option & OP_BEST_RESIZE_HEIGHT) {

        $dstW = round($dstH * $srcW / $srcH);

    }



    $fdstW = $dstW;

    $fdstH = $dstH;



    if ($cutmode != CM_DEFAULT) { // 剪切模式 1: 左或上 2: 中 3: 右或下

        $srcW -= $startX;

        $srcH -= $startY;

        if ($srcW*$dstH > $srcH*$dstW) {

            $testW = round($dstW * $srcH / $dstH);

            $testH = $srcH;

        } else {

            $testH = round($dstH * $srcW / $dstW);

            $testW = $srcW;

        }

        switch ($cutmode) {

            case CM_LEFT_OR_TOP: $srcX = 0; $srcY = 0; break;

            case CM_MIDDLE: $srcX = round(($srcW - $testW) / 2);

                            $srcY = round(($srcH - $testH) / 2); break;

            case CM_RIGHT_OR_BOTTOM: $srcX = $srcW - $testW;

                                     $srcY = $srcH - $testH;

        }

        $srcW = $testW;

        $srcH = $testH;

        $srcX += $startX;

        $srcY += $startY;

    } else { // 原始缩放



        if (!($option & OP_NOT_KEEP_SCALE)) {

            // 以下代码计算新大小,并保持图片比例

            if ($srcW*$dstH>$srcH*$dstW) {

                $fdstH=round($srcH*$dstW/$srcW);

                $dstY=floor(($dstH-$fdstH)/2);

                $fdstW=$dstW;

            } else {

                $fdstW=round($srcW*$dstH/$srcH);

                $dstX=floor(($dstW-$fdstW)/2);

                $fdstH=$dstH;

            }

       

            $dstX=($dstX
            $dstY=($dstX
            $dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;

            $dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;

        }

    } /// end if ($cutmode != CM_DEFAULT) { // 剪切模式



    if( function_exists("imagecopyresampled") and

        function_exists("imagecreatetruecolor") ){

        $func_create = "imagecreatetruecolor";

        $func_resize = "imagecopyresampled";

    } else {

        $func_create = "imagecreate";

        $func_resize = "imagecopyresized";

    }



    $newim = @$func_create($dstW,$dstH);

    $black = @ImageColorAllocate($newim, 0,0,0);

    $back = @imagecolortransparent($newim, $black);

    @imagefilledrectangle($newim,0,0,$dstW,$dstH,$black);

    @$func_resize($newim,$im,$dstX,$dstY,$srcX,$srcY,$fdstW,$fdstH,$srcW,$srcH);



    if ($option & OP_TO_FILE) {

        @$func_output($newim,$dstFile);

    }



    if ($option & OP_OUTPUT) {

        if (function_exists("headers_sent")) {

            if (headers_sent()) {

                return array(-4, "HTTP already sent, can't output image to browser.");

            }

        }

        header("Content-type: image/" . $img_type[$type_idx[$ext]]);

        @$func_output($newim);

    }



    @imagedestroy($im);

    @imagedestroy($newim);



    return array(0, "OK");

}



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.