検索
ホームページバックエンド開発PHPチュートリアルPHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート

機能:
1. 比率で縮小/拡大
2. 背景色を塗りつぶす
3. 透かしの位置、透明度などを追加する


imagemagick/GD ライブラリを使用する 実装するには、imagemagickアドレス: www.imagemagick.org imagemagick をインストールする必要があります。インストール方法は次のとおりです: クリックして表示します


PicThumb.class.php

<?php
/** 缩略图生成类,支持imagemagick及gd库两种处理
*   Date:   2013-07-15
*   Author: fdipzone
*   Ver:    1.2
*
*   Func:
*   public  set_config: 设置参数
*   public  create_thumb: 生成缩略图
*   private fit: 缩略图片
*   private crop: 裁剪图片
*   private gd_fit: GD库缩略图片
*   private gd_crop: GD库裁剪图片
*   private get_size: 获取要转换的size
*   private get_crop_offset: 获取裁图的偏移量
*   private add_watermark: 添加水印
*   private check_handler: 判断处理程序是否已安装
*   private create_dirs: 创建目录
*   private exists: 判断参数是否存在
*   private to_log: 记录log
*   private hex2rgb: hex颜色转rgb颜色
*   private get_file_ext: 获取图片类型
*
*   ver:    1.1 增加GD库处理
*   ver:    1.2 增加width,height错误参数处理
*               增加当图片colorspace不为RGB时作转RGB处理
*               修正使用crop保存为gif时出现透明无效区域问题,使用+repage参数,删除透明无效区域即可
*
*   tips:建议使用imagemagick
*        GD库不支持透明度水印,如果必须使用透明水印,请将水印图片做成有透明度。
*        GD库输出gif如加透明水印,会有问题。
*/

class PicThumb{ // class start

    private $_log = null;            // log file
    private $_handler = null;        // 进行图片处理的程序,imagemagick/gd库
    private $_type = &#39;fit&#39;;          // fit or crop
    private $_source = null;         // 原图路径
    private $_dest = null;           // 缩略图路径
    private $_watermark = null;      // 水印图片
    private $_opacity = 75;          // 水印圖片透明度,gd库不支持
    private $_gravity = &#39;SouthEast&#39;; // 水印摆放位置 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
    private $_geometry = &#39;+10+10&#39;;   // 水印定位,gd库不支持
    private $_croppos = &#39;TL&#39;;        // 截图的位置 TL TM TR ML MM MR BL BM BR
    private $_bgcolor = null;        // 填充的背景色
    private $_quality = 90;          // 生成的图片质量
    private $_width = null;          // 指定区域宽度
    private $_height = null;         // 指定区域高度


    // 初始化
    public function __construct($logfile=&#39;&#39;){
        if($logfile!=&#39;&#39;){
            $this->_log = $logfile;
        }
    }


    // 设置参数
    public function set_config($param=array()){
        $this->_handler = $this->exists($param, &#39;handler&#39;)? strtolower($param[&#39;handler&#39;]) : null;
        $this->_type = $this->exists($param, &#39;type&#39;)? strtolower($param[&#39;type&#39;]) : &#39;fit&#39;;
        $this->_watermark = $this->exists($param, &#39;watermark&#39;)? $param[&#39;watermark&#39;] : null;
        $this->_opacity = $this->exists($param, &#39;opacity&#39;)? $param[&#39;opacity&#39;] : 75;
        $this->_gravity = $this->exists($param, &#39;gravity&#39;)? $param[&#39;gravity&#39;] : &#39;SouthEast&#39;;
        $this->_geometry = $this->exists($param, &#39;geometry&#39;)? $param[&#39;geometry&#39;] : &#39;+10+10&#39;;
        $this->_croppos = $this->exists($param, &#39;croppos&#39;)? $param[&#39;croppos&#39;] : &#39;TL&#39;;
        $this->_bgcolor = $this->exists($param, &#39;bgcolor&#39;)? $param[&#39;bgcolor&#39;] : null;
        $this->_quality = $this->exists($param, &#39;quality&#39;)? $param[&#39;quality&#39;] : 90;
        $this->_width = $this->exists($param, &#39;width&#39;)? $param[&#39;width&#39;] : null;
        $this->_height = $this->exists($param, &#39;height&#39;)? $param[&#39;height&#39;] : null;
    }


    /** 创建缩略图
    * @param String $source 原图
    * @param String $dest   目标图
    * @return boolean
    */
    public function create_thumb($source, $dest){

        // 检查使用的handler是否已安装
        if(!$this->check_handler()){
            $this->to_log(&#39;handler not installed&#39;);
            return false;
        }

        // 判断区域宽高是否正确
        if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this->_height<=0){
            $this->to_log(&#39;width or height invalid&#39;);
            return false;
        }

        // 判断源文件是否存在
        if(!file_exists($source)){
            $this->to_log($source.&#39; not exists&#39;);
            return false;
        }

        // 创建目标文件路径
        if(!$this->create_dirs($dest)){
            $this->to_log(dirname($dest).&#39; create fail&#39;);
            return false;
        }

        $this->_source = $source;   // 源文件
        $this->_dest = $dest;       // 目标文件

        // 处理图片
        switch($this->_type){
            case &#39;fit&#39;:
                if($this->_handler==&#39;imagemagick&#39;){
                    return $this->fit();
                }else{
                    return $this->gd_fit();
                }
                break;

            case &#39;crop&#39;:
                if($this->_handler==&#39;imagemagick&#39;){
                    return $this->crop();
                }else{
                    return $this->gd_crop();
                }
                break;

            default:
                $this->to_log($this->_type.&#39; not fit and crop&#39;);
                return false;
        }

    }


    /** 按比例压缩或拉伸图片
    * @return boolean
    */
    private function fit(){

        // 判断是否填充背景
        $bgcolor = ($this->_bgcolor!=null)? 
        sprintf(" -background &#39;%s&#39; -gravity center -extent &#39;%sx%s&#39; ", $this->_bgcolor, $this->_width, $this->_height) : "";

        // 判断是否要转为RGB
        $source_info = getimagesize($this->_source);
        $colorspace = (!isset($source_info[&#39;channels&#39;]) || $source_info[&#39;channels&#39;]!=3)? &#39; -colorspace RGB &#39; : &#39;&#39;;

        // 命令行
        $cmd = sprintf("convert -resize &#39;%sx%s&#39; &#39;%s&#39; %s -quality %s %s &#39;%s&#39;", $this->_width, $this->_height, $this->_source, $bgcolor, 
        $this->_quality, $colorspace, $this->_dest);

        // 记录执行的命令
        $this->to_log($cmd);

        // 执行命令
        exec($cmd);

        // 添加水印
        $this->add_watermark($this->_dest);

        return is_file($this->_dest)? true : false;

    }


    /** 裁剪图片
    * @return boolean
    */
    private function crop(){

        // 获取生成的图片尺寸
        list($pic_w, $pic_h) = $this->get_size();

        // 获取截图的偏移量
        list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);

        // 判断是否要转为RGB
        $source_info = getimagesize($this->_source);
        $colorspace = (!isset($source_info[&#39;channels&#39;]) || $source_info[&#39;channels&#39;]!=3)? &#39; -colorspace RGB &#39; : &#39;&#39;;

        // 命令行
        $cmd = sprintf("convert -resize &#39;%sx%s&#39; &#39;%s&#39; -quality %s %s -crop %sx%s+%s+%s +repage &#39;%s&#39;", $pic_w, $pic_h, $this->_source, 
        $this->_quality, $colorspace, $this->_width, $this->_height, $offset_w, $offset_h, $this->_dest);

        // 记录执行的命令
        $this->to_log($cmd);

        // 执行命令
        exec($cmd);

        // 添加水印
        $this->add_watermark($this->_dest);

        return is_file($this->_dest)? true : false;

    }


    /** GD库按比例压缩或拉伸图片
    * @return boolean
    */
    private function gd_fit(){

        // 获取生成的图片尺寸
        list($pic_w, $pic_h) = $this->get_size();

        list($owidth, $oheight, $otype) = getimagesize($this->_source);

        switch($otype){
            case 1: $source_img = imagecreatefromgif($this->_source); break;
            case 2: $source_img = imagecreatefromjpeg($this->_source); break;
            case 3: $source_img = imagecreatefrompng($this->_source); break;
            default: return false;
        }

        // 按比例缩略/拉伸图片
        $new_img = imagecreatetruecolor($pic_w, $pic_h);
        imagecopyresampled($new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);

        // 判断是否填充背景
        if($this->_bgcolor!=null){
            $bg_img = imagecreatetruecolor($this->_width, $this->_height);
            $rgb = $this->hex2rgb($this->_bgcolor);
            $bgcolor =imagecolorallocate($bg_img, $rgb[&#39;r&#39;], $rgb[&#39;g&#39;], $rgb[&#39;b&#39;]);
            imagefill($bg_img, 0, 0, $bgcolor);
            imagecopy($bg_img, $new_img, (int)(($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);
            $new_img = $bg_img;
        }

        // 获取目标图片的类型
        $dest_ext = $this->get_file_ext($this->_dest);

        // 生成图片
        switch($dest_ext){
            case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
            case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
            case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
        }

        if(isset($source_img)){
            imagedestroy($source_img);
        }

        if(isset($new_img)){
            imagedestroy($new_img);
        }

        // 添加水印
        $this->add_watermark($this->_dest);

        return is_file($this->_dest)? true : false;

    }


    /** GD库裁剪图片
    * @return boolean
    */
    private function gd_crop(){

        // 获取生成的图片尺寸
        list($pic_w, $pic_h) = $this->get_size();

        // 获取截图的偏移量
        list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);

        list($owidth, $oheight, $otype) = getimagesize($this->_source);

        switch($otype){
            case 1: $source_img = imagecreatefromgif($this->_source); break;
            case 2: $source_img = imagecreatefromjpeg($this->_source); break;
            case 3: $source_img = imagecreatefrompng($this->_source); break;
            default: return false;
        }

        // 按比例缩略/拉伸图片
        $tmp_img = imagecreatetruecolor($pic_w, $pic_h);
        imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);

        // 裁剪图片
        $new_img = imagecreatetruecolor($this->_width, $this->_height);
        imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $this->_height);

        // 获取目标图片的类型
        $dest_ext = $this->get_file_ext($this->_dest);

        // 生成图片
        switch($dest_ext){
            case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
            case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
            case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
        }

        if(isset($source_img)){
            imagedestroy($source_img);
        }

        if(isset($tmp_img)){
            imagedestroy($tmp_img);
        }

        if(isset($new_img)){
            imagedestroy($new_img);
        }

        // 添加水印
        $this->add_watermark($this->_dest);

        return is_file($this->_dest)? true : false;

    }


    /** 获取目标图生成的size
    * @return Array $width, $height
    */
    private function get_size(){
        list($owidth, $oheight) = getimagesize($this->_source);
        $width = (int)($this->_width);
        $height = (int)($this->_height);
        
        switch($this->_type){
            case &#39;fit&#39;:
                $pic_w = $width;
                $pic_h = (int)($pic_w*$oheight/$owidth);
                if($pic_h>$height){
                    $pic_h = $height;
                    $pic_w = (int)($pic_h*$owidth/$oheight);
                }
                break;
            case &#39;crop&#39;:
                $pic_w = $width;
                $pic_h = (int)($pic_w*$oheight/$owidth);
                if($pic_h<$height){
                    $pic_h = $height;
                    $pic_w = (int)($pic_h*$owidth/$oheight);
                }
                break;
        }

        return array($pic_w, $pic_h);
    }


    /** 获取截图的偏移量
    * @param int $pic_w 图宽度
    * @param int $pic_h 图高度
    * @return Array $offset_w, $offset_h
    */
    private function get_crop_offset($pic_w, $pic_h){
        $offset_w = 0;
        $offset_h = 0;
        
        switch(strtoupper($this->_croppos)){
            case &#39;TL&#39;:
                $offset_w = 0;
                $offset_h = 0;
                break;

            case &#39;TM&#39;:
                $offset_w = (int)(($pic_w-$this->_width)/2);
                $offset_h = 0;
                break;

            case &#39;TR&#39;:
                $offset_w = (int)($pic_w-$this->_width);
                $offset_h = 0;
                break;

            case &#39;ML&#39;:
                $offset_w = 0;
                $offset_h = (int)(($pic_h-$this->_height)/2);
                break;

            case &#39;MM&#39;:
                $offset_w = (int)(($pic_w-$this->_width)/2);
                $offset_h = (int)(($pic_h-$this->_height)/2);
                break;

            case &#39;MR&#39;:
                $offset_w = (int)($pic_w-$this->_width);
                $offset_h = (int)(($pic_h-$this->_height)/2);
                break;

            case &#39;BL&#39;:
                $offset_w = 0;
                $offset_h = (int)($pic_h-$this->_height);
                break;

            case &#39;BM&#39;:
                $offset_w = (int)(($pic_w-$this->_width)/2);
                $offset_h = (int)($pic_h-$this->_height);
                break;

            case &#39;BR&#39;:
                $offset_w = (int)($pic_w-$this->_width);
                $offset_h = (int)($pic_h-$this->_height);
                break;
        }

        return array($offset_w, $offset_h);
    }


    /** 添加水印
    * @param String $dest 图片路径
    */
    private function add_watermark($dest){
        if($this->_watermark!=null && file_exists($this->_watermark) && file_exists($dest)){
            list($owidth, $oheight, $otype) = getimagesize($dest);
            list($w, $h, $wtype) = getimagesize($this->_watermark);

            // 水印图比原图要小才加水印
            if($w<=$owidth && $h<=$oheight){

                if($this->_handler==&#39;imagemagick&#39;){ // imagemagick 添加水印

                    $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s &#39;%s&#39; %s %s", $this->_gravity, $this->_geometry, 
                    $this->_opacity, $this->_watermark, $dest, $dest);

                    $this->to_log($cmd);

                    exec($cmd);

                }else{ // gd 添加水印

                    switch($wtype){
                        case 1: $water_img = imagecreatefromgif($this->_watermark); break;
                        case 2: $water_img = imagecreatefromjpeg($this->_watermark); break;
                        case 3: $water_img = imagecreatefrompng($this->_watermark); break;
                        default: return false;
                    }

                    switch($otype){
                        case 1: $dest_img = imagecreatefromgif($dest); break;
                        case 2: $dest_img = imagecreatefromjpeg($dest); break;
                        case 3: $dest_img = imagecreatefrompng($dest); break;
                        default: return false;
                    }

                    // 水印位置
                    switch(strtolower($this->_gravity)){
                        case &#39;northwest&#39;:
                            $posX = 0;
                            $posY = 0;
                            break;
                        case &#39;north&#39;:
                            $posX = ($owidth - $w) / 2;
                            $posY = 0;
                            break;
                        case &#39;northeast&#39;:
                            $posX = $owidth - $w;
                            $posY = 0;
                            break;
                        case &#39;west&#39;:
                            $posX = 0;
                            $posY = ($oheight - $h) / 2;
                            break;
                        case &#39;center&#39;:
                            $posX = ($owidth - $w) / 2;
                            $posY = ($oheight - $h) / 2;
                            break;
                        case &#39;east&#39;:
                            $posX = $owidth - $w;
                            $posY = ($oheight - $h) / 2;
                            break;
                        case &#39;southwest&#39;:
                            $posX = 0;
                            $posY = $oheight - $h;
                            break;
                        case &#39;south&#39;:
                            $posX = ($owidth - $w) / 2;
                            $posY = $oheight - $h;
                            break;
                        case &#39;southeast&#39;:
                            $posX = $owidth - $w;
                            $posY = $oheight - $h;
                            break;
                    }

                    imagealphablending($dest_img, true);
                    imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $h);

                    switch($otype){
                        case 1:imagegif($dest_img, $dest, $this->_quality); break;
                        case 2:imagejpeg($dest_img, $dest, $this->_quality); break;
                        case 3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10)); break;
                    }

                    if(isset($water_img)){
                        imagedestroy($water_img);
                    }

                    if(isset($dest_img)){
                        imagedestroy($dest_img);
                    }

                }
            }
        }
    }


    /** 判断处理程序是否已安装
    * @return boolean
    */
    private function check_handler(){

        $handler = $this->_handler;

        if(!in_array($handler, array(&#39;imagemagick&#39;, &#39;gd&#39;, null))){
            return false;
        }

        // 检查是否有安装imagemagick
        $imagemagick_installed = strstr(shell_exec(&#39;convert -version&#39;),&#39;Version: ImageMagick&#39;)!=&#39;&#39;? true : false;

        // 检查是否有安装gd库
        $gd_installed = function_exists(&#39;gd_info&#39;)? true : false;

        switch($handler){
            case &#39;imagemagick&#39;:
                return $imagemagick_installed;
                break;

            case &#39;gd&#39;:
                return $gd_installed;
                break;

            case null:
                if($imagemagick_installed){
                    $this->_handler = &#39;imagemagick&#39;;
                    return true;
                }

                if($gd_installed){
                    $this->_handler = &#39;gd&#39;;
                    return true;
                }
                break;
        }

        return false;
    }


    /** 创建图片目录
    * @param String $path
    * @return boolean
    */
    private function create_dirs($dest){
        if(!is_dir(dirname($dest))){
            return mkdir(dirname($dest), 0777, true);
        }
        return true;
    }


    /** 判断参数是否存在
    * @param  Array   $obj  数组对象
    * @param  String  $key  要查找的key
    * @return boolean
    */
    private function exists($obj,$key=&#39;&#39;){
        if($key==&#39;&#39;){
            return isset($obj) && !empty($obj);
        }else{
            $keys = explode(&#39;.&#39;,$key);
            for($i=0,$max=count($keys); $i<$max; $i++){
                if(isset($obj[$keys[$i]])){
                    $obj = $obj[$keys[$i]];
                }else{
                    return false;
                }
            }
            return isset($obj) && !empty($obj);
        }
    }


    /** 记录log
    * @param String $msg 要记录的log讯息
    */
    private function to_log($msg){
        if($this->_log){
            $msg = &#39;[&#39;.date(&#39;Y-m-d H:i:s&#39;).&#39;]&#39;.$msg."\r\n";
            file_put_contents($this->_log, $msg, FILE_APPEND);
        }
    }


    /** hex颜色转rgb颜色
    * @param  String $color hex颜色
    * @return Array
    */
    private function hex2rgb($hexcolor){
        $color = str_replace(&#39;#&#39;, &#39;&#39;, $hexcolor);
        if (strlen($color) > 3) {
            $rgb = array(
                &#39;r&#39; => hexdec(substr($color, 0, 2)),
                &#39;g&#39; => hexdec(substr($color, 2, 2)),
                &#39;b&#39; => hexdec(substr($color, 4, 2))
            );
        } else {
            $r = substr($color, 0, 1) . substr($color, 0, 1);
            $g = substr($color, 1, 1) . substr($color, 1, 1);
            $b = substr($color, 2, 1) . substr($color, 2, 1);
            $rgb = array(
                &#39;r&#39; => hexdec($r),
                &#39;g&#39; => hexdec($g),
                &#39;b&#39; => hexdec($b)
            );
        }
        return $rgb;
    }


    /** 获取图片类型
    * @param  String $file 图片路径
    * @return int
    */
    private function get_file_ext($file){
        $filename = basename($file);
        list($name, $ext)= explode(&#39;.&#39;, $filename);

        $ext_type = 0;

        switch(strtolower($ext)){
            case &#39;jpg&#39;:
            case &#39;jpeg&#39;:
                $ext_type = 2;
                break;
            case &#39;gif&#39;:
                $ext_type = 1;
                break;
            case &#39;png&#39;:
                $ext_type = 3;
                break;
        }

        return $ext_type;
    }

} // class end

?>

デモ:

<?php
define(&#39;ROOT&#39;, dirname(__FILE__));

require(ROOT."/PicThumb.class.php");

$logfile = ROOT.&#39;/PicThumb.log&#39;;
$source1 = ROOT.&#39;/pic/source.jpg&#39;;
$dest1 = ROOT.&#39;/pic/1.jpg&#39;;
$dest2 = ROOT.&#39;/pic/2.gif&#39;;
$dest3 = ROOT.&#39;/pic/3.png&#39;;

$source2 = ROOT.&#39;/pic/source_cmyk.jpg&#39;;
$dest4 = ROOT.&#39;/pic/4.jpg&#39;;
$dest5 = ROOT.&#39;/pic/5.gif&#39;;
$dest6 = ROOT.&#39;/pic/6.png&#39;;

$watermark = ROOT.&#39;/pic/watermark.png&#39;;

// 按比例生成缩略图
$param = array(
    &#39;type&#39; => &#39;fit&#39;,
    &#39;width&#39; => 100,
    &#39;height&#39; => 100,
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest1);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest1).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}


// 按比例生成缩略图,不足部分用#FF0000填充
$param = array(
    &#39;type&#39; => &#39;fit&#39;,
    &#39;width&#39; => 100,
    &#39;height&#39; => 100,
    &#39;bgcolor&#39; => &#39;#FFFF00&#39;
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest2);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest2).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}


// 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50
$param = array(
    &#39;type&#39; => &#39;crop&#39;,
    &#39;croppos&#39; => &#39;BM&#39;,
    &#39;width&#39; => 250,
    &#39;height&#39; => 250,
    &#39;watermark&#39; => $watermark,
    &#39;opacity&#39; => 50,
    &#39;gravity&#39; => &#39;SouthWest&#39;
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source1, $dest3);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest3).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}


// 按比例生成缩略图 CMYK格式
$param = array(
    &#39;type&#39; => &#39;fit&#39;,
    &#39;width&#39; => 100,
    &#39;height&#39; => 100,
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest4);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest4).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}


// 按比例生成缩略图,不足部分用#FF0000填充 CMYK格式
$param = array(
    &#39;type&#39; => &#39;fit&#39;,
    &#39;width&#39; => 100,
    &#39;height&#39; => 100,
    &#39;bgcolor&#39; => &#39;#FFFF00&#39;
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest5);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest5).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}


// 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50 CMYK格式
$param = array(
    &#39;type&#39; => &#39;crop&#39;,
    &#39;croppos&#39; => &#39;BM&#39;,
    &#39;width&#39; => 250,
    &#39;height&#39; => 250,
    &#39;watermark&#39; => $watermark,
    &#39;opacity&#39; => 50,
    &#39;gravity&#39; => &#39;SouthWest&#39;
);

$obj = new PicThumb($logfile);
$obj->set_config($param);
$flag = $obj->create_thumb($source2, $dest6);

if($flag){
    echo &#39;<img  src="/static/imghwm/default1.png"  data-src="pic/&#39;.basename($dest6).&#39;"  class="lazy"   alt="PHP サムネイル生成クラス、imagemagick および gd ライブラリ処理をサポート" >&#39;;
}else{
    echo &#39;create thumb fail&#39;;
}
?>

上記は、imagemagick および gd ライブラリ処理をサポートする PHP サムネイル生成クラスです。関連コンテンツの詳細については、PHP 中国語 Web サイト (www. php.cn)!


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
PHPセッションに保存されているデータをどのように変更しますか?PHPセッションに保存されているデータをどのように変更しますか?Apr 27, 2025 am 12:23 AM

tomodifydatainaphpsession、starthessession withsession_start()、$ _sessiontoset、modify、orremovevariables.1)startthessession.2)

PHPセッションに配列を保存する例を示します。PHPセッションに配列を保存する例を示します。Apr 27, 2025 am 12:20 AM

配列はPHPセッションに保存できます。 1。セッションを開始し、session_start()を使用します。 2。配列を作成し、$ _Sessionで保存します。 3. $ _Sessionを介して配列を取得します。 4.セッションデータを最適化してパフォーマンスを向上させます。

Garbage CollectionはPHPセッションでどのように機能しますか?Garbage CollectionはPHPセッションでどのように機能しますか?Apr 27, 2025 am 12:19 AM

PHPセッションガベージコレクションは、有効期限が切れたセッションデータをクリーンアップするために確率メカニズムを通じてトリガーされます。 1)構成ファイルにトリガー確率とセッションのライフサイクルを設定します。 2)Cronタスクを使用して、高負荷アプリケーションを最適化できます。 3)データの損失を避けるために、ごみ収集の頻度とパフォーマンスのバランスを取る必要があります。

どのようにしてPHPでセッションアクティビティをトレースできますか?どのようにしてPHPでセッションアクティビティをトレースできますか?Apr 27, 2025 am 12:10 AM

PHPでのユーザーセッションアクティビティの追跡は、セッション管理を通じて実装されます。 1)SESSION_START()を使用してセッションを開始します。 2)$ _Sessionアレイを介してデータを保存およびアクセスします。 3)セッションを終了するには、session_destroy()を呼び出します。セッショントラッキングは、ユーザーの動作分析、セキュリティ監視、パフォーマンスの最適化に使用されます。

データベースを使用してPHPセッションデータを保存するにはどうすればよいですか?データベースを使用してPHPセッションデータを保存するにはどうすればよいですか?Apr 27, 2025 am 12:02 AM

データベースを使用してPHPセッションデータを保存すると、パフォーマンスとスケーラビリティが向上します。 1)MySQLを構成してセッションデータを保存します:PHP.iniまたはPHPコードでセッションプロセッサを設定します。 2)カスタムセッションプロセッサを実装します:データベースと対話するために、開いて、閉じ、読み取り、書き込み、その他の機能を定義します。 3)最適化とベストプラクティス:インデックス、キャッシュ、データ圧縮、分散ストレージを使用して、パフォーマンスを向上させます。

PHPセッションの概念を簡単に説明してください。PHPセッションの概念を簡単に説明してください。Apr 26, 2025 am 12:09 AM

phpssionsStrackuserdataacrossmultiplepagerequestsusingauniqueidstoredinacookie.here'showtomanageetheemefectively:1)Startassession withsession_start()andstoredatain $ _ session.2)RegeneratesseSsessidafterloginwithsession_id(the topreventes_id)

PHPセッションに保存されているすべての値をどのようにループしますか?PHPセッションに保存されているすべての値をどのようにループしますか?Apr 26, 2025 am 12:06 AM

PHPでは、次の手順を通じてセッションデータを繰り返すことができます。1。session_start()を使用してセッションを開始します。 2。$ _Sessionアレイのすべてのキー価値ペアを介してforeachループを反復します。 3.複雑なデータ構造を処理する場合、is_array()またはis_object()関数を使用し、print_r()を使用して詳細情報を出力します。 4.トラバーサルを最適化する場合、ページングを使用して、一度に大量のデータの処理を避けることができます。これにより、実際のプロジェクトでPHPセッションデータをより効率的に管理および使用するのに役立ちます。

ユーザー認証にセッションを使用する方法を説明します。ユーザー認証にセッションを使用する方法を説明します。Apr 26, 2025 am 12:04 AM

このセッションは、サーバー側の状態管理メカニズムを介してユーザー認証を実現します。 1)セッションの作成と一意のIDの生成、2)IDはCookieを介して渡されます。3)サーバーストアとIDを介してセッションデータにアクセスします。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!