PHP to achieve large image automatically reduced to small map (and GD library installation)
UB 2001.10.11
I think the biggest difference between network programming and ordinary programming is that network programming, in addition to the characteristics of ordinary programming, but also to spend a lot of energy to consider the problem of speed.
The site needs a lot of pictures, now there are a lot of users to store photos, pictures of the "album" Program, you can let users upload their own pictures, and then automatically shrink into a small map, you can see a lot of small pictures in a page, and then click on the small picture to see the big picture
Note: Specify the long width display picture in the webpage, only can let the picture show in small size, but the size (the capacity) is invariable (can look at the attribute), the speed is very slow naturally. The real thumbnail is the size of the larger image (capacity), which is to regenerate a small picture file. One of my programs has this feature that is being tested: Www1.yn.cninfo.net/love
In addition, installed on the GD library, there are many functions, you can use the program to paint, do some special counters and so on, but also can support FreeType fonts, and so on, there are many functions, powerful.
First of all, the difference between the Gif,jpg,png,bmp diagram: They are graphic compression format. GIF format single map can display up to 256 colors, can save multiple graphs, suitable for small animation, large capacity. JPEG format, supports 24-bit (16 million) colors, suitable for static graphs, small capacity. The PNG format, which contains all the features of the Gif,jpeg, supports 48-bit colors, but has a large capacity and is not widely used. BMP format, less color, very old compression format, for the network is too big.
In PHP to achieve large picture automatically reduced to small figure is actually very simple, installed on the GD library, with a small function on it, but the installation of the GD library is troublesome. The GD library before 1.6.2 has features that support the GIF format, but because the LZW algorithm used in the GIF format involves Unisys patents, the GIF format is not supported after the GD Library 1.6.2 Edition.
PHP after version 3.0.13, finally support the PNG format, PNG is not afraid of the issue of copyright, and in this version of PHP, the installation will check when the GD library to support the old GIF format or the new PNG format to determine whether to use GIF or PNG's private functions.
First, install the GD library:
1. Environment: Windows2000server+php4.0.4+gd+iis
It is easy to install in Windows, PHP4.0 in the above version, installed in the Php4/extensions directory with the dynamic link library: Php_gd.dll. Just put it in the php.ini, Extension=php_gd.dll the front, remove it, and then restart the webserver (IIS or Apache). Run a with A;? Phpinfo ();? > PHP program, which has the following content to show that it can be used:
Gd
GD Support Enabled
GD Version 1.6.2 or higher
FreeType Support Enabled
FreeType linkage with TTF library
JPG Support Enabled
PNG Support Enabled
WBMP Support Enabled
In addition, there is a php_gd_gif.dll on the Internet can download, renamed it php_gd.dll overwrite the original file, restart IIS, you can implement the GD library with GIF library.
To HTTP://PHP.WEBLOGS.COM/PHP_GD download Php_gd_404.zip, decompression complete the above operation.
This is for PHP4.0.4, the other version of PHP is not available, (the owner of the PHP4.0.0 can download php_gd_gif.zip).
Oh, after you finish your Phpinfo () will show more than one GIF Support enabled, but related to the issue of copyright, I see To do it.
In Linux installed GD library is more trouble, first simple about the installation of Apache and PHP: When we install PHP in the Apache environment, you have three kinds of installation mode to choose: Static module, dynamic module (DSO) and CGI.
Commonly used is the static module, that is, Apache and PHP compiled together, so that the implementation of PHP more efficient, but PHP to upgrade or to install the GD library and so on the additional library will be two to recompile, very troublesome. Dynamic module is much better, separate
Compile PHP on the line, the specific installation view of PHP install file. I've been doing this for half a day. Installation succeeded, introduced a little static module plus GD Library +jpeg+png experience:
2. Environment: Linux6.1+php4.0.6+gd1.8.4+jpeg+png+zlib+apache_1.3.20+mysql
1) Download the required libraries:
Download GD Library: Recommended version 1.8.4 (Stable) stable version, I compile gd-2.0.1 (BETA), failed.
http://www.boutell.com/gd/
The following steps need to be performed sequentially:
2) I use the LINUX6.1, the kernel is 2.2.12. To root into, extract all packets, TAR-ZXVF filename. Then compile:
3 If your Linux is not installed zlib, you must first install ZILB, enter zlib-1.1.3 execution:
./configure
Make
Make install
4 Enter libpng-1.0.6, can be installed in Linux, or DOS, under Linux need to copy his makefile file, execute:
CP Scripts/makefile.linux Makefile
Make
Make install
5) Enter jpeg-6b execution:
./configure
Make
Make install
6a) into the gd-1.8.4
Modify Makefile
Check the GD library compile time to add JPEG support, you have to modify the makefile file.
Modify the Makefile file so that:
Cflags=-o-dhave_libpng-dhave_libjpeg
Libs=-lgd-lpng-lz-lm-ljpeg
Or, if you freetype,xpm,ttf and other libraries are installed, (the installation method is very similar, to find the library yourself), change the following:
#CFLAGS =-o-dhave_libxpm-dhave_libpng-dhave_libjpeg
#-dhave_libfreetype-dhave_libttf
After that, compile and install:
Make
Make install
(If you need MySQL, and you do not install, first installed MySQL, decompression, execution:./configure; make; Make install
./configure--prefix=/home/mysql, the default installation path/usr/local/share/mysql. )
7 recompile PHP, I use PHP-4.0.6, it is best to look at the installation file, more INSTALL, and then execute:
7.1 Decompression into the apache_1.3.20, need to test some documents, implementation:
./configure--prefix=/home/apache
--activate-module=src/modules/php4/libphp4.a
7.2 Decompression into php-4.0.6, compile and install:
./configure--with-mysql--with-apache=. /apache_1.3.20
--with-gd=.. /gd-1.8.4--with-jpeg-dir=. /jpeg-6b--with-png-dir=. /libpng-1.0.6
--with-zlib--enable-track-var
(Default installation path:/usr/local/lib/php)
8 If your PHP is compiled with Apache, the static module, you will recompile Apache, I use the apache_1.3.20
Let's stop Apache:./home/apache/bin/apachectl stop.
Enter apache_1.3.20, need to change file src/configuration:
Add: Addmodule modules/php4/libphp4.a
[This is apache_1.3.x installation, if you are using apache_1.2.x, add module Php4_module mod_php4.o
Then copy Libphp4.module to SRC and modify the corresponding statement as follows: Extra_libs=-l.-lphp4-lgdbm-ldb-l/usr/local/mysql/lib-lmysqlclient
If you use dynamic module installation, if it is the initial installation, modify the statement is different, the specific view of PHP insatll file. ]
Then execute:
./configure--prefix=/home/apache (prefix= is the specified store path. Without prefix, the default is/usr/local/apache)
(You can also do it directly./configure--prefix=/home/apache
--ACTIVATE-MODULE=SRC/MODULES/PHP4/LIBPHP4.A)
Make
Make install
9) Enter php-4.0.6 execution
CP Php.ini-dist/usr/local/lib/php.ini
10 start Apache:./home/apache/bin/apachectl start
11 Run your Phpinfo () program, the default Apache root directory is/usr/local/apache/htdocs/. As shown in Windows above, congratulations on your installation success, you can support php+gd+jpeg+png. If not, try reloading.
In addition, there is a GIF patch in the gd-1.8.3, I tried not to succeed, it seems to be a very low version of Linux, if you succeed, or in LINUX6.1 above the version of what other ways to achieve php+gd+gif, please tell Me (e_mail: UBs@ynmail.com)
If you want to use GIF graphics, the following steps: 6a to 6b
Use gd-1.8.3, plus gif patch compilation, so that the GD library contains GIF libraries:
6b) Download: http://www.boutell.com/gd/http/gd-1.8.3.tar.gz
Download Patch: Http://www.webofsin.com/gd-1.8.3-gif.patch
untie, enter gd-1.8.3.
Patch on GIF: Patch-p0 <gd-1.8.3-gif.patch
File to PATCH:GD.C
Then there's one more gd-1.8.3/gd_gif.c in this directory.
Revise makefile, basically ditto.
After that, compile and install:
Other steps ditto, as long as the gd-1.8.4 replaced by gd-1.8.3.
Second, PHP in the big picture to shrink the program example: (in Windows and Linux platform debugging passed.) )
1 Upload Picture page: (fragment)
In the middle of <script language= "JavaScript" >
<!--
function Checkform (theform)
{
var fileext=theform.zhaopian_up.value.substring (Theform.zhaopian_up.value.length-4,theform.zhaopian_ Up.value.length)
Fileext=fileext.tolowercase ()
if (!) ( fileext== '. jpg ' | | fileext== '. png '))
{Alert ("Sorry, incorrect photo file must be a *.jpg or *.png file!") ");
TheForm.zhaopian_up.focus ();
return false;
}
return true;
}
-->
Add an upload form to the page:
<form enctype= "Multipart/form-data" action= "photo_upload2.php" method= "POST" onsubmit= "return CheckForm (This)" >
Note: Please upload the photo first, then fill in the form. The first photo for the cover, can only pass JPG pictures, less than 300K, less than 9 pieces.
<p class= "Big" > Upload photos:</p>
<input type= "File" Name= "zhaopian_up" size= "> <input type=" Submit "value=" Upload ">
</form>
2 Processing page program, including the judge picture size, upload copy, thumbnail, generate small map file, picture ID stored in the database, etc.: (fragment)
?
Get upload file size, 300K:
if ($zhaopian _up_size>307200) {
$wrong _massage= "Sorry, you upload the file size of more than 300K, please reduce the picture and then pass!" ";
Header ("location:include/wrong_massage.php?wrong_massage= $wrong _massage");
Exit ();
}
Automatic thumbnail $srcfile original file, large image, $photo _small object file, small map, $dstW, $dstH is a small map of the wide, high.
function Makethumb ($srcFile, $photo _small, $dstW, $dstH) {
$data = getimagesize ($srcFile,& $info);
Switch ($data [2]]) {
Case 1://Picture type, 1 is GIF
$im = @ImageCreateFromGIF ($srcFile);
Break
Case 2://Picture type, 2 is JPG
$im = @imagecreatefromjpeg ($srcFile);
Break
Case 3://Picture type, 3 is PNG chart
$im = @ImageCreateFromPNG ($srcFile);
Break
}
$srcW =imagesx ($im);
$srcH =imagesy ($im);
$ni =imagecreate ($dstW, $dstH);
Imagecopyresized ($ni, $im, 0,0,0,0, $dstW, $dstH, $srcW, $srcH);
Imagejpeg ($ni, $photo _small);
Imagejpeg ($ni); When the picture is displayed, the annotation is canceled and the picture can be displayed directly on the page.
}
$srcFile = $zhaopian _up;
To obtain the file name extension:
$type =substr (STRRCHR ($zhaopian _up_name, "."), 1);
$photo _id=time ();
$photo _path= "user_picture/$user/";
if (Is_dir ($photo _path)!=true) mkdir ($photo _path,0777);
$photo _small= $photo _path. " /". $photo _id." _s. ". $type; Small figure
$photo _s=fopen ($photo _small, "w+");
if ($type = = "png" | | | $type = = "jpg") makethumb ($srcFile, $photo _small,110,130);//110,130 for small plot width, high
?>
In the program that shows the picture, just read the picture ID from the database and use $photo_path. /". $photo _id." _s. ". $type is to show the small map, add a link, you can show the big picture $photo_path. /". $photo _id.". $type;
I have this feature in one of my programs, testing: Www1.yn.cninfo.net/love
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.