php 打包gd 库

Jun 13, 2016 pm 12:18 PM
height image name width

php 封装gd 库

使用sae 版 thinkphp 在本地和sae 上无法实现缩略图,缩略图主要使用在瀑布流上面,不然一张图片2 3 M,速度太慢,就自己封装了一个,见笑!

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Thumb</span>{</span>    <span class="hljs-comment">// 设置最大宽度,用来在编辑器中使用和显示</span>    <span class="hljs-keyword">private</span>  <span class="hljs-variable">$max_width</span>  = <span class="hljs-keyword">null</span>;    <span class="hljs-keyword">private</span>  <span class="hljs-variable">$file_name</span>  = <span class="hljs-keyword">null</span>;    <span class="hljs-keyword">private</span>  <span class="hljs-variable">$water_name</span> = <span class="hljs-keyword">null</span>;    <span class="hljs-comment">//获得文件名和图片宽度</span>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(<span class="hljs-variable">$max_widht</span>,<span class="hljs-variable">$file_name</span>,<span class="hljs-variable">$water_name</span>)</span> {</span>        <span class="hljs-variable">$this</span>->max_width  = <span class="hljs-variable">$max_widht</span>;        <span class="hljs-variable">$this</span>->file_name  = <span class="hljs-variable">$file_name</span>;        <span class="hljs-variable">$this</span>->water_name = <span class="hljs-variable">$water_name</span>;    }    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create_image</span><span class="hljs-params">()</span>{</span>        <span class="hljs-comment">// 获得ori图片信息</span>        <span class="hljs-keyword">list</span>(<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$type</span>) = getimagesize(<span class="hljs-variable">$this</span>->file_name);              <span class="hljs-comment">// 当原有图片大于 要求的最大宽度时,才需要进行压缩</span>        <span class="hljs-keyword">if</span>(<span class="hljs-variable">$width</span> > <span class="hljs-variable">$this</span>->max_width){            <span class="hljs-comment">// 获得图片压缩百分比</span>            <span class="hljs-variable">$per</span> = <span class="hljs-variable">$this</span>->max_width / <span class="hljs-variable">$width</span>;            <span class="hljs-variable">$new_width</span> = <span class="hljs-variable">$width</span> * <span class="hljs-variable">$per</span>;            <span class="hljs-variable">$new_height</span> = <span class="hljs-variable">$height</span> * <span class="hljs-variable">$per</span>;                  }<span class="hljs-keyword">else</span>{            <span class="hljs-variable">$new_height</span> =  <span class="hljs-variable">$height</span>;            <span class="hljs-variable">$new_width</span>  =  <span class="hljs-variable">$width</span>;        }        <span class="hljs-comment">//创建一个真彩色图像</span>        <span class="hljs-variable">$image_p</span> = imagecreatetruecolor(<span class="hljs-variable">$new_width</span>, <span class="hljs-variable">$new_height</span> -<span class="hljs-number">10</span>);        <span class="hljs-variable">$image</span> = <span class="hljs-variable">$this</span>->image_obj(<span class="hljs-variable">$type</span>,  <span class="hljs-variable">$this</span>->file_name);         imagecopyresampled(<span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$image</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-variable">$new_width</span>, <span class="hljs-variable">$new_height</span>, <span class="hljs-variable">$width</span>, <span class="hljs-variable">$height</span>);        <span class="hljs-variable">$this</span>->image_dump(<span class="hljs-variable">$type</span>, <span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$this</span>->file_name);        <span class="hljs-variable">$this</span>->water();    }    <span class="hljs-comment">/*     * 生成为图片添加水印     */</span>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">water</span><span class="hljs-params">()</span>{</span>        <span class="hljs-variable">$water_name</span> = <span class="hljs-variable">$this</span>->water_name;        <span class="hljs-variable">$dist_name</span> = <span class="hljs-variable">$this</span>->file_name;        <span class="hljs-keyword">list</span>(<span class="hljs-variable">$dist_width</span>,<span class="hljs-variable">$dist_height</span>,<span class="hljs-variable">$type</span>) = getimagesize(<span class="hljs-variable">$dist_name</span>);        <span class="hljs-variable">$dist_im</span>   = <span class="hljs-variable">$this</span>->image_obj(<span class="hljs-variable">$type</span>, <span class="hljs-variable">$this</span>->file_name);        <span class="hljs-variable">$water_name</span> = <span class="hljs-string">"D:/xampps/htdocs/buyingfeiblog/1/App/Modules/Admin/Tpl/Public/Images/water.png"</span>;        <span class="hljs-keyword">list</span>(<span class="hljs-variable">$w_width</span>,<span class="hljs-variable">$w_height</span>) = getimagesize(<span class="hljs-variable">$water_name</span>); <span class="hljs-comment">// 获得图片水印信息</span>        <span class="hljs-variable">$water_src</span> =  imagecreatefrompng(<span class="hljs-variable">$water_name</span>);        <span class="hljs-comment">// 设置图片水印位置 在右下角</span>        <span class="hljs-variable">$x</span> = (<span class="hljs-variable">$dist_width</span> - <span class="hljs-variable">$w_width</span>) / <span class="hljs-number">4</span> * <span class="hljs-number">3</span> ;        <span class="hljs-variable">$y</span> =(<span class="hljs-variable">$dist_height</span> - <span class="hljs-variable">$w_height</span>) /<span class="hljs-number">4</span> * <span class="hljs-number">3</span> ;        <span class="hljs-keyword">if</span>(imagecopy(<span class="hljs-variable">$dist_im</span>, <span class="hljs-variable">$water_src</span>, <span class="hljs-variable">$x</span>, <span class="hljs-variable">$y</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-variable">$w_width</span>, <span class="hljs-variable">$w_height</span>)){            imagepng(<span class="hljs-variable">$dist_im</span>,<span class="hljs-variable">$dist_name</span>);            <span class="hljs-keyword">echo</span> <span class="hljs-string">"success"</span>;        }<span class="hljs-keyword">else</span>{            <span class="hljs-keyword">echo</span> <span class="hljs-string">"error"</span>;        }    }    <span class="hljs-comment">// 生成图片类型,生成不同图片 保持图片原本类型不发生变化</span>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">image_dump</span><span class="hljs-params">(<span class="hljs-variable">$type</span>,<span class="hljs-variable">$image_p</span>,<span class="hljs-variable">$filename</span>)</span>{</span>         <span class="hljs-keyword">switch</span> (<span class="hljs-variable">$type</span>){            <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:                imagegif(<span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$filename</span>);                <span class="hljs-variable">$dis_im</span> = imagecreatefromgif(file_name);                <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:                imagejpeg(<span class="hljs-variable">$image_p</span>,  <span class="hljs-variable">$this</span>->file_name);                <span class="hljs-variable">$dis_im</span> = imagecreatefromjpeg(file_name);                 <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:                imagepng(<span class="hljs-variable">$image_p</span>,file_name);                <span class="hljs-variable">$dis_im</span> = imagecreatefrompng(file_name);                <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">default</span> :        }    }    <span class="hljs-comment">// 根据图片不同,生成不同资源对象</span>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">image_obj</span><span class="hljs-params">(<span class="hljs-variable">$type</span>,<span class="hljs-variable">$filename</span>)</span>{</span>         <span class="hljs-keyword">switch</span> (<span class="hljs-variable">$type</span>){<span class="hljs-comment">//          1 = GIF,2 = JPG,3 = PNG,</span>            <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:               <span class="hljs-variable">$image</span> =   imagecreatefromgif(<span class="hljs-variable">$filename</span>);              <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:               <span class="hljs-variable">$image</span> =   imagecreatefromjpeg(<span class="hljs-variable">$filename</span>);              <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:               <span class="hljs-variable">$image</span> =   imagecreatefrompng(<span class="hljs-variable">$filename</span>);               <span class="hljs-keyword">break</span>;            <span class="hljs-keyword">default</span> :        }        <span class="hljs-keyword">return</span> <span class="hljs-variable">$image</span>;    }}    <span class="hljs-variable">$thumb</span> = <span class="hljs-keyword">new</span>  Thumb(<span class="hljs-number">725</span>,<span class="hljs-string">"D:/xampps/htdocs/test/test.jpg"</span>);    <span class="hljs-variable">$thumb</span>->create_image();<span class="hljs-comment">//create_image</span><span class="hljs-preprocessor">?></span></span></code>
Nach dem Login kopieren

就是这么简单,
主要包括生成真彩图,
创建图形对象资源
图片进行合并,ok!

Erklärung dieser Website
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)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

So nutzen Sie Bing Image Creator kostenlos So nutzen Sie Bing Image Creator kostenlos Feb 27, 2024 am 11:04 AM

In diesem Artikel werden sieben Möglichkeiten vorgestellt, mit dem kostenlosen BingImageCreator eine qualitativ hochwertige Ausgabe zu erhalten. BingImageCreator (jetzt bekannt als ImageCreator für Microsoft Designer) ist einer der großartigen Online-Kunstgeneratoren für künstliche Intelligenz. Es generiert äußerst realistische visuelle Effekte basierend auf Benutzereingaben. Je spezifischer, klarer und kreativer Ihre Aufforderungen sind, desto besser werden die Ergebnisse sein. BingImageCreator hat erhebliche Fortschritte bei der Erstellung hochwertiger Bilder gemacht. Es verwendet jetzt den Dall-E3-Trainingsmodus und zeigt ein höheres Maß an Detailgenauigkeit und Realismus. Allerdings hängt die Fähigkeit, konsistent HD-Ergebnisse zu liefern, von mehreren Faktoren ab, unter anderem von der Geschwindigkeit

Was bedeutet die Breite von HTML? Was bedeutet die Breite von HTML? Jun 03, 2021 pm 02:15 PM

In HTML5 bedeutet „Breite“ die Breite des Inhaltsbereichs. Sie können innere Ränder, Ränder und äußere Ränder außerhalb des Inhaltsbereichs hinzufügen Element.

So löschen Sie Bilder von Xiaomi-Telefonen So löschen Sie Bilder von Xiaomi-Telefonen Mar 02, 2024 pm 05:34 PM

Wie lösche ich Bilder auf Xiaomi-Handys? Sie können Bilder auf Xiaomi-Handys löschen, aber die meisten Benutzer wissen nicht, wie man Bilder auf Xiaomi-Handys löscht können Sie bei uns vorbeischauen. Mal sehen! So löschen Sie Bilder auf dem Xiaomi-Mobiltelefon 1. Öffnen Sie zunächst die Funktion [Album] im Xiaomi-Mobiltelefon. 2. Überprüfen Sie dann die nicht benötigten Bilder und klicken Sie auf die Schaltfläche [Löschen] in der unteren rechten Ecke Um den Spezialbereich aufzurufen, wählen Sie oben [Papierkorb] aus. 4. Klicken Sie dann direkt auf [Papierkorb leeren], wie in der Abbildung unten gezeigt. 5. Klicken Sie abschließend direkt auf [Permanent löschen].

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Imagemagic-Installation Centos und Image-Installations-Tutorial Imagemagic-Installation Centos und Image-Installations-Tutorial Feb 12, 2024 pm 05:27 PM

LINUX ist ein Open-Source-Betriebssystem und daher die erste Wahl für viele Entwickler und Systemadministratoren. Im LINUX-System ist die Bildverarbeitung eine sehr wichtige Aufgabe, und Imagemagick und Image sind zwei sehr beliebte Bildverarbeitungstools Der Artikel führt Sie in die Installation von Imagemagick und Image im Centos-System ein und bietet detaillierte Installationsanleitungen. Imagemagic-Installation Centos-Tutorial Imagemagick ist ein leistungsstarkes Bildverarbeitungs-Toolset, das verschiedene Bildoperationen unter der Befehlszeile ausführen kann. Im Folgenden sind die Schritte zur Installation von Imagemagick auf dem Centos-System aufgeführt: 1

Detaillierte Erläuterung der CSS-Dimensionseigenschaften: Höhe und Breite Detaillierte Erläuterung der CSS-Dimensionseigenschaften: Höhe und Breite Oct 21, 2023 pm 12:42 PM

Detaillierte Erläuterung der CSS-Dimensionseigenschaften: Höhe und Breite In der Frontend-Entwicklung ist CSS eine leistungsstarke Stildefinitionssprache. Unter diesen sind Höhe und Breite die beiden grundlegendsten Dimensionsattribute, mit denen die Höhe und Breite des Elements definiert werden. In diesem Artikel werden diese beiden Eigenschaften im Detail analysiert und spezifische Codebeispiele bereitgestellt. 1. Höhenattribut Das Höhenattribut wird verwendet, um die Höhe eines Elements zu definieren. Sie können Pixel, Prozent oder verwenden

Welche Methoden gibt es, um den Breitenwert in CSS auszudrücken? Welche Methoden gibt es, um den Breitenwert in CSS auszudrücken? Nov 13, 2023 pm 05:47 PM

Zu den Methoden gehören Pixelwert, Prozentsatz, EM-Einheit, REM-Einheit, VW/VH-Einheit, Auto, Fit-Content, Min-Content, Max-Content. Ausführliche Einführung: 1. Pixelwert (px): Der Pixelwert ist fest und seine Breite bleibt unverändert, unabhängig davon, wie sich die Bildschirmauflösung ändert. Beispiel: Breite: 300 Pixel; 2. Prozent (%): Die prozentuale Breite ist relativ zur Breite des übergeordneten Elements. Zum Beispiel: Breite: 50 %, em-Einheit usw.

So fügen Sie dem Setup in Vue3 einen Namen hinzu So fügen Sie dem Setup in Vue3 einen Namen hinzu May 13, 2023 am 09:40 AM

Wozu dient der Name in Vue3? 1. Der Name muss beim Erstellen rekursiver Komponenten definiert werden. 2. Die Komponente kann mit keep-aliveincludeexclude zwischengespeichert werden. 3. Wenn Vue einen Fehler meldet oder debuggt, wird der Name der Komponente angezeigt Wird automatisch generiert, solange der Setup-Syntax-Zuckermodus im Skript aktiviert ist. Die entsprechende Namensoption wird automatisch basierend auf dem Dateinamen generiert. Beispielsweise wird Tree.vue automatisch von Tree generiert . Dies hat einen Nachteil. Wenn Sie den Namen ändern möchten, müssen Sie den Komponentennamen ändern. 2. Öffnen Sie ein Skript, um den Namen zu definieren

See all articles