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>
Copy after login

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

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

What does the width of html mean? What does the width of html mean? Jun 03, 2021 pm 02:15 PM

In HTML5, width means width. The width attribute defines the width of the element's content area. You can add inner margins, borders, and outer margins outside the content area. You only need to set "element {width: value}" to the element.

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images on Xiaomi mobile phones? You can delete images on Xiaomi mobile phones, but most users don’t know how to delete images. Next is the tutorial on how to delete images on Xiaomi mobile phones brought by the editor. Interested users can come and join us. Let's see! How to delete images on Xiaomi mobile phone 1. First open the [Album] function in Xiaomi mobile phone; 2. Then check the unnecessary pictures and click the [Delete] button in the lower right corner; 3. Then click [Album] at the top to enter the special area , select [Recycle Bin]; 4. Then directly click [Empty Recycle Bin] as shown in the figure below; 5. Finally, directly click [Permanent Delete] to complete.

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

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

Imagemagic installation Centos and Image installation tutorial Imagemagic installation Centos and Image installation tutorial Feb 12, 2024 pm 05:27 PM

LINUX is an open source operating system. Its flexibility and customizability make it the first choice of many developers and system administrators. In the LINUX system, image processing is a very important task, and Imagemagick and Image are Two very popular image processing tools, this article will introduce you to how to install Imagemagick and Image in Centos system, and provide detailed installation tutorials. Imagemagic installation Centos tutorial Imagemagick is a powerful image processing toolset, which can perform various image operations under the command line. The following are the steps to install Imagemagick on Centos system: 1

Detailed explanation of CSS dimension properties: height and width Detailed explanation of CSS dimension properties: height and width Oct 21, 2023 pm 12:42 PM

Detailed explanation of CSS dimension properties: height and width In front-end development, CSS is a powerful style definition language. Among them, height and width are the two most basic dimension attributes, used to define the height and width of the element. This article will analyze these two properties in detail and provide specific code examples. 1. Height attribute The height attribute is used to define the height of an element. You can use pixel, percentage or

What are the methods for expressing width value in css? What are the methods for expressing width value in css? Nov 13, 2023 pm 05:47 PM

Methods include pixel value, percentage, em unit, rem unit, vw/vh unit, auto, fit-content, min-content, max-content. Detailed introduction: 1. Pixel value (px): The pixel value is fixed, and its width remains unchanged no matter how the screen resolution changes. For example: width: 300px; 2. Percent (%): The percentage width is relative to the width of the parent element. For example: width: 50%; 3, em unit, etc.

How to add name to setup in Vue3 How to add name to setup in Vue3 May 13, 2023 am 09:40 AM

What is the use of name in Vue3? 1. Name needs to be defined when making recursive components. 2. The component can be cached with keep-aliveincludeexclude. 3. When Vue reports an error or is debugging, you can see the name of the component. Vue3 defines name1. It is automatically generated as long as the setup syntax sugar mode single file component is turned on in the script. The corresponding name option will be automatically generated based on the file name. For example, Tree.vue, then its name will be automatically generated by Tree. This has a drawback. If you want to modify the name, you need to modify the component name. If there is a place to import the component, you need to modify it together. 2. Open a script to define name

See all articles