Automatically scale a large image into a small graph (and install the GD Library) in PHP)

Source: Internet
Author: User
Tags imagejpeg
I think the biggest difference between network programming and general programming is that in addition to having the characteristics of general programming, network programming also takes a lot of energy to consider the speed issue. There are a lot of pictures on the website, and now there are a lot of photo and Image Album programs for users to upload their own pictures, and then automatically scale into small pictures, I think the biggest difference between network programming and common programming is that network programming has the characteristics of common programming, it also takes a lot of effort to consider the speed issue.

There are a lot of pictures on the website. now there are a lot of pictures for users. The "photo album" program allows users to upload pictures themselves and then scale them into small pictures automatically, you can see a lot of small pictures on a page, and then click the small picture to see the big picture.
Note: when you specify the length and width of an image on a webpage, the image can only be displayed in a small size, but the size (size) remains unchanged (you can check the attributes). The speed is naturally slow. The real scale-down is to reduce the size (capacity) of a large image, that is, to regenerate a small image file. One of my programs has this function and is testing: www1.yn.cninfo.net/love
In addition, the GD library has many functions, such as program drawing and some special counters. FREETYPE fonts can also be supported. There are many functions and powerful functions in it.

First, let's talk about the differences between GIF, JPG, PNG, and BMP images: they are all image compression formats. A single GIF image can display up to 256 colors. it can save multiple images and is suitable for small animations with a large size. The JPEG format supports 24-bit (16 million) colors and is suitable for static images with a small size. PNG format, including all features of GIF and JPEG, supports 48-bit color, but the size is large and is not widely used. BMP format, with few colors and very old compression formats, is too big for the network.
In PHP, it is very easy to automatically scale a large image into a small image. install the GD library and use a small function, but it is very troublesome to install the GD Library. GD libraries earlier than version 1.6.2 support the GIF format function, but the LZW algorithm used in GIF format involves the Unisys patent right, therefore, the GIF format is not supported after GD library 1.6.2.

PHP versions 3.0.13 and later finally support the PNG graphic format. if you use PNG, you will not be afraid of copyright issues, during installation and compilation, the system checks whether the GD library supports the old GIF format or the new PNG format to determine whether to use the GIF or PNG special functions.


1. install the GD Library:

1. environment: windows2000server + PHP4.0.4 + GD + IIS

It is easy to install in WINDOWS. php and later versions are self-contained. after installation, the dynamic link library php_gd.dll is automatically included in the php4/extensions directory. You just need to remove the extension = php_gd.dll in php. ini and restart WEBSERVER (IIS or APACHE. Run PHP program, which has the following content to describe how to use:

gdGD Support enabledGD Version 1.6.2 or higherFreeType Support enabledFreeType Linkage with TTF libraryJPG Support enabledPNG Support enabledWBMP Support enabled


In addition, there is a php_gd_gif.dll on the Internet that can be downloaded and renamed to php_gd.dll to overwrite the original file and restart IIS to implement the GD library with the GIF library. Download php_gd_404.zip to the http://php.weblogs.com/php_gd and decompress the package to complete the above operations. This is for php4.0.4, and other PHP files cannot be used. (you can download php_gd_gif.zip from php4.0.0of the boss ).

Haha, after you get it done, your phpinfo () will show an extra GIF Support enabled, but it involves copyright issues. let's do it by yourself.

Installing the GD Library in LINUX is troublesome. let's talk about the installation of APACHE and PHP first: when we install PHP in Apache, you have three installation modes to choose from: static module, dynamic module (DSO), and CGI.

Static modules are commonly used, that is, APACHE and PHP are compiled together, which makes PHP execution more efficient, however, to upgrade PHP or install additional libraries such as the GD library, it is very troublesome to re-compile both of them. The dynamic module is much easier to handle.
Compile PHP and check the INSTALL file in PHP. I spent half a day successfully installing. I will introduce some experiences in adding the GD library + JPEG + PNG to the static module:

1) Download the required Library:
Download GD Library: it is recommended to use Version 1.8.4 (Stable) Stable Version, I did not pass when compiling the gd-2.0.1 (BETA.
Http://www.boutell.com/gd/

Download zlib:
Http://www.info-zip.org/pub/infozip/zlib/

Download libpng: it is recommended to use libpng-1.0.6-1-src.tar.gz
Http://www.turbolinux.com.cn/pub/mirrors/sourceware/cygwin/contrib/libpng/

Download example src.v6b.tar.gz:
Http://www.turbolinux.com.cn/pub/mirrors/gnu/ghostscript/

Follow these steps:

2) I use LINUX6.1 and 2.2.12 as the kernel. Enter as ROOT and decompress all packages, tar-zxvf filename. Then compile:

3) If your LINUX does not install zlib, you must first install zilb, enter the zlib-1.1.3 to execute:
./Configure
Make
Make install

4) enter the libpng-1.0.6, can be installed in LINUX, or DOS, LINUX needs to copy his makefile File, execute:
Cp scripts/makefile. linux makefile
Make
Make install

5) enter the jpeg-6b to execute:
./Configure
Make
Make install

6a) entering gd-1.8.4
Modify Makefile
Check whether JPEG support is added to the GD library during compilation. you must 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 have installed FREETYPE, XPM, TTF, and other libraries (the installation method is similar, find the library by yourself), change the following:
# CFLAGS =-O-DHAVE_LIBXPM-DHAVE_LIBPNG-DHAVE_LIBJPEG
#-DHAVE_LIBFREETYPE-DHAVE_LIBTTF

# LIBS =-lgd-lpng-lz-ljpeg-lfreetype-lm-lttf
Remove #

After that, compile and install:
Make
Make install

(If you need MYSQL, but you haven't installed it, install MYSQL first, decompress it, and execute:./configure; make install
./Configure -- prefix =/home/mysql, default installation path/usr/local/share/mysql .)


7) re-compile PHP, I use a PHP-4.0.6, it is best to first look at the installation file, more INSTALL, and then execute:

7.1) decompress the package to apache_1.3.20. you need to take some files first and execute:
./Configure -- prefix =/home/apache
-- Activate-module = src/modules/php4/libphp4.a

7.2) decompress into the 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 and the static module is required, re-compile APACHE. I use apache_1.3.20.
Stop APACHE:./home/apache/bin/apachectl stop
Go to apache_1.3.20 and change the file src/Configuration:
Add: AddModule modules/php4/libphp4.a
[This is the installation of APACHE_1.3.x. if you are using APACHE_1.2.x, add Module php4_module mod_php4.o.
Copy libphp4.module to src and modify the statement to EXTRA_LIBS =-L.-lphp4-lgdbm-ldb-L/usr/local/mysql/lib-lmysqlclient.
If you use a dynamic module for installation, if it is the first installation, the modification statement is different. for details, refer to the php insatll file.]

Then execute:
./Configure -- prefix =/home/apache (prefix = specifies the storage path. Without prefix, the default value is/usr/local/apache)
(You can also directly execute./configure -- prefix =/home/apache
-- Activate-module = src/modules/php4/libphp4.a)
Make
Make install

9) go to php-4.0.6 to execute
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 successful installation and support for PHP + GD + JPEG + PNG. If not, try again.

In addition, there is a GIF patch in the gd-1.8.3, I tried not successful, it seems to be a very low version of LINUX, if you succeed, or in LINUX6.1 or a later version of any other way to achieve PHP + GD + GIF, please tell me (E_mail: UBs@ynmail.com)

To use a GIF image, perform the following operations: replace 6a with 6b
Use a gd-1.8.3 with GIF patch to make the GD library contain the GIF library:

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
Unlock and go to gd-1.8.3
Patch with GIF: patch-p0 File to patch: gd. c
Then there is a gd-1.8.3/gd_gif.c in the directory

Modify Makefile.
After that, compile and install:
The other steps are the same as above, as long as you replace the gd-1.8.4 with the gd-1.8.3


2. example of a program with a thumbnail in PHP: (both WINDOWS and LINUX have been debugged .)

1) upload an image page: (fragment)

InAdd the following program in the middle (this program is very useful and can be used at the front end and the user end to determine the type of file uploaded by the user and control the user's file uploaded)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.