php生成略缩图程序代码与详细介绍
文章详细的介绍了关于php生成略缩图程序过程与实现方法 ,有需要了解的同学可参考一下下。
12.3 自动微缩图的生成
由于图片的数据量比较大,传递过程相对较长,所以在用户浏览图片时,常常希望先
显示一个图片的缩略图。
虽然在HTML中可以通过指定图片的宽度和高度来随意缩放图片,但是这种方法不会
减少图片的像素数目。图形文件的尺寸没有改变,当然也不会加快图片下载的速度了。当
然也可以手动通过图形软件生成图片的缩略图,但对于大量的图片展示来说,这个工作量
将十分巨大。为此微缩图的自动生成程序就被设计出来了。
PHP中提供的imagecopyresized函数就可以用来生成真正的缩赂图片。该函数的标推
语法如下:
语法:int imagecopyresized(int dst_im,int src_im,int dstX,int dstY,
int srcX,int srcY,int dstW,int dstH,int srcW,int srcH);
返回值:整数
函数种类:图形处理
内容说明:本函数可复制新图,并重新调整图片的大小尺寸。参数都是目的在前,来
源在后。参数dst im及src_im为图片的handle。参数dstX、dstY、srcX、srcY分别为目的
及来源的坐标。参数dstW、dstH、srcW、srcH分别为来源及目的的宽及高,欲调整的新图
的尺寸就在这儿配置。
下面举个例子来说明这个函数的用法,对应的程序thumb.php如程序清单12—5所示。
程序清单12—5 thumb.php
代码如下 | 复制代码 |
// 本函数从源文件取出图像,设定成指定大小,并输出到目的文件 // 源文件格式:gif,jpg,png // 目的文件格式:gif // $srcFile:源文件 // $dstFile: 目标文件 // $dstW:目标图片宽度 // $dstH:目标文件高度 function makethumb($srcFile,$dstFile,$dstW,$dstH) { $data = GetImageSize($srcFile,&$info); switch ($data[2]) { case 1: $imgsrc = @ImageCreateFromGIF($srcFile); break; case 2: $imgsrc = @ImageCreateFromJPEG($srcFile); break; case 3: $imgsrc = @ImageCreateFromPNG($srcFile); break; } $srcW = ImageSX($imgsrc); $srcH = ImageSY($imgsrc); $ni = ImageCreate($dstW,$dstH); ImageCopyResized($ni,$imgsrc,0,0,0,0,$dstW,$dstH,$srcW,$srcH); Imagegif($ni,$dstFile); // 如果需要输出到浏览器,那么将上一句改为 ImageJpeg($ni); // 如果需要其他格式的图片,改动最后一句就可以了 } ?> |
在这个例子中,首先通过getimagesize()函数获得源图片的情况,再用 imagecreatefromgif()、
imagecreatefromjpeg()或imagecreatefrompng()创建一个源位图$imgsrc,然后用
imagecreate()函数创建一个目标位图,其长、宽各是源位图的一半。然后调用imagecopyresized()
函数,将源位图缩小后拷贝到目标位图中,最后再用imagegif()函数生成缩略图。
这里所用到的图形处理函数就是由安装的GD库提供的,现对其分别进行说明。首先
介绍getimagesize()函数,其标准语法如下。
语法:array getimagesize(string filename,array [imageinfo]);
返回值:数组
函数种类:图形处理
内容说明:本函数可用来取得GIF、JPEG及PNG 3种WWW上图片的高与宽,不
需要安装GD library就可以便用本函数。返回的数组有4个元素,返回数组的第一个元素(索
引值0)是图片的高度,单位是像素(pixel);第二个元素(索引值1)是图片的宽度;第三个元
素(索引值2)是图片的文件格式,其值1为GIF格式、2为JPEG/JPG格式、3为PNG格式;
第四个元素(索引值3)为图片的高与宽字符串,height=xxx width=yyy。
通过getimagesize()函数的应用,能轻易获取图片的各种信息。下面给大家举一个获取
图片宽度、高度、格式、文件大小的信息的例子,来进一步领会getimagesize()函数的使用
技巧。程序imginfo如程序清单12—6所示。
程序清单12-6 imginfo.php
代码如下 | 复制代码 |
function getImageInfo($img) //$img为图像文件绝对路径 $new_img_info = array (
|
在程序12-5中要创建一个缩略图,需要先创建一个用来绘图的空白的画布。
ImageCreate函数可以做到这一点。它将返回一个图像的标识符,并且需要告诉函数用像素
计算的画布有多大(x(宽度)与y(高度))。在程序12-5中用到的图形创建函数imagecreate()
的标准语法如下:
语法:int imagecreate(int x_size,int y_size);
返回值:整数
函数种类:图形处理
内容说明:本函数用来建立一张全空的图形。参数x_size、y_size为图形的尺寸,单位
为像素(pixel)。
如果要从已经存在的图片中取出图片文件代码,可以用imagecreatefromgif()、
imagecreatefromjpeg()或imagecreatefrompng(),例如函数imagecreatefromgif()就是从一个GIF
格式的图片文件中取出对应的图片源代码,其标准语法如下:
语法:int imagecreatefromgif(string filename);
返回值:整数
函数种类:图形处理
内容说明:本函数用来取出一张GIF格式图形,通常作为背景或者基本的画布样本使
用。参数filename可以是本地端的文件,也可以是网络的URL地址。返回值为GIF的文件
代码,可供其他函数使用。
在将源位图缩小后拷贝到目标位图中时,用到了imagecopyresized()函数,此函数可以
复制新图并调整大小,其标准语法如下:
语法:int imagecopyresized(int dst_im,int src_im,int dstX,int dstY,int srcX,int srcY,
int dstW,int dstH,int srcW,int srcH);
返回值:整数
函数种类:图形处理
内容说明:本函数可复制新图,并重新调整图片的大小尺寸。参数那是目的在前,来
源在后。参数ddst_im及src_im为图片的handle。参数dstX、dstY、srcX、srcY分别为目的
及来源的坐标。参数dstW、dstH、srcW、srcH分别为来源及目的的宽及高,若欲调整新图
的尺寸就在这里配置。
最后在输出图像时用到的imagegif()函数的标准语法如下:
语法:int imagegif(int im,string [filename]);
返问值:整数
函数种类:图形处理
内容说明:本函数用来建立一张GIF格式图形。参数im为使用ImageCreate()所建立
的图片代码,参数filename可省略,若无本参数filename,则会将图片直接送到浏览器端,
记得在送出图片之前要先送出使用Content-type:image/gif的标头字符串(header)到浏览器
端,以顺利传输图片。若要使用透明背景的GIF图,也就是GIF89a的格式,需要先使用
ImageColorTransparent()配置透明背景。本函数产生的GIF图,由于有版权的问题,因此
在商业上的使用还要多加考虑。
代码如下 | 复制代码 |
$pic_name=date("dMYHis"); // 生成图片的宽度 // 生成图片的高度 function ResizeImage($im,$maxwidth,$maxheight,$name){ if($_FILES['image']['size']){
|

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

Methods to open img files include using virtual optical drive software, using compression software, and using special tools. Detailed introduction: 1. Use virtual optical drive software to open, download and install a virtual optical drive software, right-click the img file, select "Open with" or "Associated Program", select the installed virtual optical drive software in the pop-up dialog box, virtual The optical drive software will automatically load the img file and use it as a disc image in the virtual optical drive. Double-click the disc icon in the virtual optical drive to open the img file and access its contents, etc.

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
