목차
[PHP]代码 " >[PHP]代码 
php教程 PHP源码 智能的PHP缩图类

智能的PHP缩图类

May 25, 2016 pm 05:11 PM
php

[PHP]代码 

<?php
/***************************************
*作者:落梦天蝎(beluckly)
*完成时间:2006-12-18
*类名:CreatMiniature
*功能:生成多种类型的缩略图
*基本参数:$srcFile,$echoType
*方法用到的参数:
                $toFile,生成的文件
                $toW,生成的宽
                $toH,生成的高
                $bk1,背景颜色参数 以255为最高
                $bk2,背景颜色参数
                $bk3,背景颜色参数
 
*例子:
 
    include(&quot;thumb.php&quot;);
    $cm=new CreatMiniature();
    $cm-&gt;SetVar(&quot;1.jpg&quot;,&quot;file&quot;);
    $cm-&gt;Distortion(&quot;dis_bei.jpg&quot;,150,200);
    $cm-&gt;Prorate(&quot;pro_bei.jpg&quot;,150,200);
    $cm-&gt;Cut(&quot;cut_bei.jpg&quot;,150,200);
    $cm-&gt;BackFill(&quot;fill_bei.jpg&quot;,150,200);
 
***************************************/
 
class CreatMiniature
{
    //公共变量
    var $srcFile=&quot;&quot;;        //原图
    var $echoType;            //输出图片类型,link--不保存为文件;file--保存为文件
    var $im=&quot;&quot;;                //临时变量
    var $srcW=&quot;&quot;;            //原图宽
    var $srcH=&quot;&quot;;            //原图高
 
    //设置变量及初始化
    function SetVar($srcFile,$echoType)
    {
        $this-&gt;srcFile=$srcFile;
        $this-&gt;echoType=$echoType;
 
        $info = &quot;&quot;;
        $data = GetImageSize($this-&gt;srcFile,$info);
        switch ($data[2]) 
        {
         case 1:
           if(!function_exists(&quot;imagecreatefromgif&quot;)){
            echo &quot;你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!&lt;a href=&#39;javascript:go(-1);&#39;&gt;返回&lt;/a&gt;&quot;;
            exit();
           }
           $this-&gt;im = ImageCreateFromGIF($this-&gt;srcFile);
           break;
        case 2:
          if(!function_exists(&quot;imagecreatefromjpeg&quot;)){
           echo &quot;你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!&lt;a href=&#39;javascript:go(-1);&#39;&gt;返回&lt;/a&gt;&quot;;
           exit();
          }
          $this-&gt;im = ImageCreateFromJpeg($this-&gt;srcFile);    
          break;
        case 3:
          $this-&gt;im = ImageCreateFromPNG($this-&gt;srcFile);    
          break;
      }
      $this-&gt;srcW=ImageSX($this-&gt;im);
      $this-&gt;srcH=ImageSY($this-&gt;im); 
    }
    
    //生成扭曲型缩图
    function Distortion($toFile,$toW,$toH)
    {
        $cImg=$this-&gt;CreatImage($this-&gt;im,$toW,$toH,0,0,0,0,$this-&gt;srcW,$this-&gt;srcH);
        return $this-&gt;EchoImage($cImg,$toFile);
        ImageDestroy($cImg);
    }
    
    //生成按比例缩放的缩图
    function Prorate($toFile,$toW,$toH)
    {
        $toWH=$toW/$toH;
        $srcWH=$this-&gt;srcW/$this-&gt;srcH;
        if($toWH&lt; =$srcWH)
        {
            $ftoW=$toW;
            $ftoH=$ftoW*($this-&gt;srcH/$this-&gt;srcW);
        }
        else
        {
              $ftoH=$toH;
              $ftoW=$ftoH*($this-&gt;srcW/$this-&gt;srcH);
        }
        if($this-&gt;srcW&gt;$toW||$this-&gt;srcH&gt;$toH)
        {
            $cImg=$this-&gt;CreatImage($this-&gt;im,$ftoW,$ftoH,0,0,0,0,$this-&gt;srcW,$this-&gt;srcH);
            return $this-&gt;EchoImage($cImg,$toFile);
            ImageDestroy($cImg);
        }
        else
        {
            $cImg=$this-&gt;CreatImage($this-&gt;im,$this-&gt;srcW,$this-&gt;srcH,0,0,0,0,$this-&gt;srcW,$this-&gt;srcH);
            return $this-&gt;EchoImage($cImg,$toFile);
            ImageDestroy($cImg);
        }
    }
    
    //生成最小裁剪后的缩图
    function Cut($toFile,$toW,$toH)
    {
          $toWH=$toW/$toH;
          $srcWH=$this-&gt;srcW/$this-&gt;srcH;
          if($toWH&lt; =$srcWH)
          {
               $ctoH=$toH;
               $ctoW=$ctoH*($this-&gt;srcW/$this-&gt;srcH);
          }
          else
          {
              $ctoW=$toW;
              $ctoH=$ctoW*($this-&gt;srcH/$this-&gt;srcW);
          } 
        $allImg=$this-&gt;CreatImage($this-&gt;im,$ctoW,$ctoH,0,0,0,0,$this-&gt;srcW,$this-&gt;srcH);
        $cImg=$this-&gt;CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
        return $this-&gt;EchoImage($cImg,$toFile);
        ImageDestroy($cImg);
        ImageDestroy($allImg);
    }
 
    //生成背景填充的缩图
    function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
    {
        $toWH=$toW/$toH;
        $srcWH=$this-&gt;srcW/$this-&gt;srcH;
        if($toWH&lt; =$srcWH)
        {
            $ftoW=$toW;
            $ftoH=$ftoW*($this-&gt;srcH/$this-&gt;srcW);
        }
        else
        {
              $ftoH=$toH;
              $ftoW=$ftoH*($this-&gt;srcW/$this-&gt;srcH);
        }
        if(function_exists(&quot;imagecreatetruecolor&quot;))
        {
            @$cImg=ImageCreateTrueColor($toW,$toH);
            if(!$cImg)
            {
                $cImg=ImageCreate($toW,$toH);
            }
        }
        else
        {
            $cImg=ImageCreate($toW,$toH);
        }
        $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3);        //填充的背景颜色
        ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
        if($this-&gt;srcW&gt;$toW||$this-&gt;srcH&gt;$toH)
        {     
            $proImg=$this-&gt;CreatImage($this-&gt;im,$ftoW,$ftoH,0,0,0,0,$this-&gt;srcW,$this-&gt;srcH);
            /*
             if($ftoW&lt; $toW)
             {
                 ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);
             }
             else if($ftoH&lt;$toH)
             {
                 ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
             }
             */
            if($ftoW&lt;$toW)
            {
                 ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
            }
            else if($ftoH&lt;$toH)
            {
                 ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
            }
            else
            {
                 ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
            } 
        }
        else
        {
             ImageCopyMerge($cImg,$this-&gt;im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
        }
        return $this-&gt;EchoImage($cImg,$toFile);
        ImageDestroy($cImg);
    }
    
 
    function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
    {
        if(function_exists(&quot;imagecreatetruecolor&quot;))
        {
            @$creatImg = ImageCreateTrueColor($creatW,$creatH);
            if($creatImg) 
                ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
            else
            {
                $creatImg=ImageCreate($creatW,$creatH);
                ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
            }
         }
         else
         {
            $creatImg=ImageCreate($creatW,$creatH);
            ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
         }
         return $creatImg;
    }
    
    //输出图片,link---只输出,不保存文件。file--保存为文件
    function EchoImage($img,$to_File)
    {
        switch($this-&gt;echoType)
        {
            case &quot;link&quot;:
                if(function_exists(&#39;imagejpeg&#39;)) return ImageJpeg($img);
                else return ImagePNG($img);
                break;
            case &quot;file&quot;:
                if(function_exists(&#39;imagejpeg&#39;)) return ImageJpeg($img,$to_File);
                else return ImagePNG($img,$to_File);
                break;
        }
    }
 
}
?>
로그인 후 복사

                   

                   

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 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 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles