suchen
Heimphp教程PHP源码php生成任意缩放图片

php生成任意缩放图片

May 25, 2016 pm 05:03 PM
php

1. [PHP]代码   

<?php
/**
 * @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式
 * @example
 * $file = &#39;test.png&#39;;
 * $th =new Thumbnail();
 * $th->GenerateThumbnail($file, 400, 500);
 *
 */
class Thumbnail{
     /**
     * @var string $from 源图片
     */
     private $from;
     /**
     * @var string $name 缩略图的文件名
     */
     private $name = &#39;&#39;;
     /**
     * @var 原图宽
     */
     private $rWidth;
     /**
     * @var 原图高
     */
     private $rHeight;
     /**
     * @var 缩略图宽
     */
     private $tWidth;
     /**
     * @var 缩略图高
     */
     private $tHeight;
     /**
     * @var 实际缩放到的宽度
     */
     private $width;
     /**
     * @var 实际缩放到的高度
     */
     private $height;

     public function __construct(){
         try{
             if(!function_exists(&#39;gd_info&#39;)){
                 throw new Exception(&#39;Must GD extension is enabled&#39;);
             }
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $e->getMessage();
             echo $msg;
             exit;
         }
     }
     /**
     * @var $from 原图像
     * @var $width 生成的缩略图的宽
     * @var $height 生成缩略图的高
     * @var $name 生成的缩略图的文件名,不带后缀
     * @return string 生成的缩略图
     */
     public function GenerateThumbnail($from, $width, $height, $name=&#39;&#39;){
         try{
             if(!file_exists($from)){
                 throw new Exception(&#39;File does not exist&#39;);
             }
             if($width <= 0){
                 throw new Exception(&#39;The width is invalid&#39;);
             }
             if($height <= 0){
                 throw new Exception(&#39;The height is invalid&#39;);
             }
             $this->from = $from;
             $this->tWidth = $width;
             $this->tHeight = $height;
             if(!empty($name)){
                 $this->name = $name;
             }
             else{
                 $this->name = date(&#39;Ymd&#39;) . mt_rand(0, 9999);
             }
             $this->createThumbnail();
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $e->getMessage();
             echo $msg;
             exit;
         }
     }

     public function getThumbnail(){
         return $this->name;
     }

     /**
     * 生成缩略图文件
     */
     private function createThumbnail(){
         try{
             //读取原始图像信息
             $sourceInfo = getimagesize($this->from);
             $this->rWidth = $sourceInfo[0];
             $this->rHeight = $sourceInfo[1];
             //创建缩略图图像资源句柄
             $new_pic = imagecreatetruecolor($this->tWidth, $this->tHeight);
             //原图绘制到缩略图的x、y坐标
             $x = 0;
             $y = 0;
             //创建原始图像资源句柄
             $source_pic = &#39;&#39;;
             switch ($sourceInfo[2]){
                 case 1: $source_pic = imagecreatefromgif($this->from); //gif
                         break;
                 case 2: $source_pic = imagecreatefromjpeg($this->from); //jpg
                         break;
                 case 3: $source_pic = imagecreatefrompng($this->from); //png
                         break;
                 default: throw new Exception(&#39;Does not support this type of image&#39;);
             }
             //计算缩放后图像实际大小
             //原图宽高均比缩略图大
             if($this->rWidth > $this->tWidth && $this->rHeight > $this->tHeight){
                 $midw = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
                 $midh = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
                 //那个缩小的比例大以那个为准
                 if($midw > $midh){
                     $this->width = $this->tWidth;
                     $this->height = $this->rHeight - floor($this->rHeight * $midw);
                     $y = ($this->tHeight - $this->height) / 2;
                 }
                 else{
                     $this->width = $this->rWidth - floor($this->rWidth * $midh);
                     $this->height = $this->tHeight;
                     $x = ($this->tWidth - $this->width) / 2;
                 }
             }
             //原图宽高均比缩略图小
             elseif($this->rWidth < $this->tWidth && $this->rHeight < $this->tHeight){
                 $midw = ($this->tWidth - $this->rWidth) / $this->rWidth; //宽放大的比例
                 $midh = ($this->tHeight - $this->rHeight) / $this->rHeight; //高放大的比例
                 //那个放大的比例小以那个为准
                 if($midw < $midh){
                     $this->width = $this->tWidth;
                     $this->height = $this->rHeight + floor($this->rHeight * $midw);
                     $y = ($this->tHeight - $this->height) / 2;
                 }
                 else{
                     $this->width = $this->rWidth + floor($this->rWidth * $midh);
                     $this->height = $this->tHeight;
                     $x = ($this->tWidth - $this->width) / 2;
                 }
             }
             //原图宽小于缩略图宽,原图高大于缩略图高
             elseif($this->rWidth < $this->tWidth && $this->rHeight > $this->tHeight){
                 $mid = ($this->rHeight - $this->tHeight) / $this->rHeight; //高缩小的比例
                 $this->width = $this->rWidth - floor($this->rWidth * $mid);
                 $this->height = $this->rHeight - floor($this->rHeight * $mid);
                 $x = ($this->tWidth - $this->width) / 2;
                 $y = ($this->tHeight - $this->height) / 2;
             }
             //原图宽大于缩略图宽,原图高小于缩略图高
             elseif($this->rWidth > $this->tWidth && $this->rHeight < $this->tHeight){
                 $mid = ($this->rWidth - $this->tWidth) / $this->rWidth; //宽缩小的比例
                 $this->width = $this->rWidth - floor($this->rWidth * $mid);
                 $this->height = $this->rHeight - floor($this->rHeight * $mid);
                 $x = ($this->tWidth - $this->width) / 2;
                 $y = ($this->tHeight - $this->height) / 2;
             }
             else{
                 throw new Exception(&#39;Resize error&#39;);
             }

             //给缩略图添加白色背景
             $bg = imagecolorallocate($new_pic, 255, 255, 255);
             imagefill($new_pic, 0, 0, $bg);
             //缩小原始图片到新建图片
             imagecopyresampled($new_pic, $source_pic, $x, $y, 0, 0, $this->width, $this->height, $this->rWidth, $this->rHeight);
             //输出缩略图到文件
             imagepng($new_pic, $this->name.&#39;.png&#39;);
             imagedestroy($new_pic);
             imagedestroy($source_pic);
         }
         catch(Exception $e){
             $msg = &#39;class &#39; . __CLASS__ . &#39; Error:&#39; . $e->getMessage();
             echo $msg;
             exit;
         }
     }
}

                   

                   

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

SublimeText3 Linux neue Version

SublimeText3 Linux neue Version

SublimeText3 Linux neueste Version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) ist eine PHP/MySQL-Webanwendung, die sehr anfällig ist. Seine Hauptziele bestehen darin, Sicherheitsexperten dabei zu helfen, ihre Fähigkeiten und Tools in einem rechtlichen Umfeld zu testen, Webentwicklern dabei zu helfen, den Prozess der Sicherung von Webanwendungen besser zu verstehen, und Lehrern/Schülern dabei zu helfen, in einer Unterrichtsumgebung Webanwendungen zu lehren/lernen Sicherheit. Das Ziel von DVWA besteht darin, einige der häufigsten Web-Schwachstellen über eine einfache und unkomplizierte Benutzeroberfläche mit unterschiedlichen Schwierigkeitsgraden zu üben. Bitte beachten Sie, dass diese Software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung

SecLists

SecLists

SecLists ist der ultimative Begleiter für Sicherheitstester. Dabei handelt es sich um eine Sammlung verschiedener Arten von Listen, die häufig bei Sicherheitsbewertungen verwendet werden, an einem Ort. SecLists trägt dazu bei, Sicherheitstests effizienter und produktiver zu gestalten, indem es bequem alle Listen bereitstellt, die ein Sicherheitstester benötigen könnte. Zu den Listentypen gehören Benutzernamen, Passwörter, URLs, Fuzzing-Payloads, Muster für vertrauliche Daten, Web-Shells und mehr. Der Tester kann dieses Repository einfach auf einen neuen Testcomputer übertragen und hat dann Zugriff auf alle Arten von Listen, die er benötigt.

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen