配置PHP使之能同时支持GIF和JPEG_PHP
问题:安装蓝点Linux 2.0后,进行PHP编程,发现只能处理GIF图像,不能处理JPEG图像。后来知道PHP处理图像,使用了GD库,而GD库开始时是支持GIF的,但由于GIF使用了有版权争议的LZW算法,会引起法律问题,于是从GD-1.6开始,GD库不再支持GIF,改为支持更好的,无版权争议的PNG。而我现在希望同时支持GIF,PNG和JPEG。
经过尝试,我成功地做到了这一点。下面介绍具体做法。我的配置为:蓝点Linux 2.0,Kernel-2.2.16,MySQL-3.23.10alpha,Apache-1.3.12, PHP-4.0.1pl2,gd-1.8.3,Jpeg6b。
我将按照由底向上的顺序介绍,即Jpeg->GD->PHP->Apache。
0、 当前状态查看
安装蓝点Linux 2.0后,Kernel,MySQL,Apache未做任何改变,看看当前状态如何。
创建一PHP文件,名为info.php,放于Apahce的文档目录下(/etc/httpd/html),其内容如下:
phpinfo(); ?>
文件就只有1行。用浏览器打开该文件URL(我的hostname为zhangzh):
http://zhangzh/info.php
如果Apache/PHP正常运行的话,页面中将会列出PHP版本,Apache版本,以及其他各种有用的信息。我关心的是GD库部分,看看它是否支持GIF、JPEG,结果发现支持GIF而不支持JPEG。
1、 Jpeg6b的安装和配置
Jpeg的主要文件有jpeglib.h, libjpeg.a, libjpeg.so等。首先检查系统中安装了没有,到/usr/include目录下看有无jpeglib.h,到/usr/lib目录下看有无libjpeg.a和libjpeg.so。我的系统中没有,因而要安装。
获取Jpeg源码的地址为:
ftp://ftp.uu.net/graphics/jpeg/
取得的文件为jpegsrc.v6b.tar.gz,放于/usr/src目录下。
进入/usr/src目录中,开始安装过程。
进入/usr/src:
cd /usr/src
解开压缩文件:
tar xzvf jpegsrc.v6b.tar.gz
命令完成后多了一个子目录jpeg-6b,Jpeg的源码文件就在其中。
进入该子目录:
cd jpeg-6b
该目录中的install.doc文件详细介绍了如何安装Jpeg,照章行事即可。
配置生成Makefile文件:
./configure
命令完成后该目录下多了个Makefile文件。Makefile文件是许多软件编译、安装的配置和过程控制文件,十分重要,应该学会看懂它的内容。
开始编译:
make
命令完成后该目录下多了许多文件,其中重要的是libjpeg.a和libjpeg.so。
安装:
make install
命令完成后,jpeglib.h被拷到/usr/local/include目录下,libjpeg.a和libjpeg.so被拷到/usr/local/lib目录下。
2、 GD-1.8.3的安装和配置
GD的主要文件有gd.h, libgd.a等。
获取GD源码的地址为:
http://www.boutell.com/gd/
取得的文件为gd-1.8.3.tar.gz,放于/usr/src目录下。
已知道该版本的GD不支持GIF,但想来象我一样希望GD同时支持GIF和JPEG的人不少,于是有人做了个补丁,把对GIF的支持加回去了。看起来这是个英国人吧,他的Email地址为adam@elysium.ltd.uk。
获取补丁源码的地址为:
http://www.webofsin.com/gd-1.8.3-gif.patch
取得的文件为gd-1.8.3-gif.patch,放于/usr/src目录下。
进入/usr/src:
cd /usr/src
解开压缩文件:
tar xzvf gd-1.8.3.tar.gz
命令完成后多了一个子目录gd-1.8.3,GD的源码文件就在其中。
给源码打补丁:
patch -p0
进入该子目录:
cd gd-1.8.3
缺省情况下,GD库编译时并不加入JPEG支持,得修改Makefile文件。
修改Makefile文件,使得:
CFLAGS=-O -DHAVE_XPM -DHAVE_JPEG -DHAVE_LIBTTF
LIBS=-lm -lgd -lpng -lz -ljpeg -lttf -lXpm -lX11
此后,编译并安装:
make
make install
命令完成后,gd.h被拷到/usr/local/include目录下,libgd.a被拷到/usr/local/lib目录下。
3、 PHP-4.0.1pl2的安装和配置
PHP的主要文件有libphp4.a, libphp4.so等。
获取PHP源码的地址为:
http://php.net
取得的文件为php-4.0.1pl2.tar.gz,放于/usr/src目录下。
进入/usr/src目录并解压文件:
cd /usr/src
tar xzvf php-4.0.1pl2.tar.gz
命令完成后多了一个子目录php-4.0.1pl2,PHP的源码文件就在其中。
进入该子目录:
cd php-4.0.1pl2
该目录中的INSTALL文件详细介绍了如何安装PHP,照章行事即可。
配置生成Makefile文件:
./configure '--with-apxs=/usr/sbin/apxs' '--with-mysql' '--with-config-file-path=/etc/httpd' '--enable-safe-mode' '--with-system-regex' '--disable-debug' '--with-zlib' '--enable-magic-quotes' '--enable-track-vars' '--with-jpeg-dir=/usr/local' '--with-gd=/usr/local'
注意最后一行参数'--with-jpeg-dir=/usr/local' '--with-gd=/usr/local',指明了Jpeg和GD的目录为/usr/local,这是根据步骤1、2中make install的结果而指定的。
(由于命令太长,建议写成shell文件再执行。文件my-php-conf内容如下:
#! /bin/sh
./configure '--with-apxs=/usr/sbin/apxs' '--with-mysql' '--with-config-file-path=/etc/httpd' '--enable-safe-mode' '--with-system-regex' '--disable-debug' '--with-zlib' '--enable-magic-quotes' '--enable-track-vars' '--with-jpeg-dir=/usr/local' '--with-gd=/usr/local'
用shell执行之:
sh my-php-conf
效果是一样的。)
命令完成后该目录下多了个Makefile文件。
编译并安装:
make
make install
命令完成后,libphp4.so被拷到/usr/lib/apache目录下。
4、 Apache的配置
Apache本身不必重新编译安装,但使用了新的PHP,须让Apache知道,得修改Apache的配置文件并重启Apache服务。
修改Apache配置文件/etc/httpd/conf/httpd.conf,使得文件中包含以下几行:
LoadModule php4_module modules/libphp4.so
AddModule mod_php4.c
AddType application/x-httpd-php .php3 .php
同时注意把旧的php3的相应行注释掉,否则会出现冲突而使Apache重启失败。
重启Apache服务:
/etc/rc.d/init.d/httpd restart
5、 实例测试
再次按步骤0的说明检查当前状态,我已经看到,PHP改成了新的版本号,GD库也同时支持GIF和JPEG了。
但我还是想用实例来测试一下,这个例子的功能是读取一个gif文件,生成缩图,然后保存为另一个jpg文件。文件create-thumb.php的内容如下:
function CreateThumbnail($srcFile, $dstFile, $dstW, $dstH)
{
$data = GetImageSize($srcFile,&$info);
switch ($data[2]) {
case 1:
$im = @ImageCreateFromGIF($srcFile);
break;
case 2:
$im = @ImageCreateFromJPEG($srcFile);
break;
case 3:
$im = @ImageCreateFromPNG($srcFile);
break;
}
$srcW=ImageSX($im);
$srcH=ImageSY($im);
if ($srcW ImageJPEG($im,$dstFile);
else
{
if(($srcW / $srcH) > ($dstW / $dstH))
$dstH = $dstW * $srcH / $srcW;
else
$dstW = $dstH * $srcW / $srcH;
$ni=ImageCreate($dstW,$dstH);
ImageCopyResized($ni,$im,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJPEG($ni,$dstFile);
}
}
CreateThumbnail("./test.gif", "./test-tn.jpg", 80, 80);
?>
把该文件放于Apahce的文档目录下(/etc/httpd/html),同时把测试用的图像文件test.gif也放于该目录下,然后用浏览器打开该php文件(我的hostname为zhangzh):
http://zhangzh/create-thumb.php
没出错信息。再看Apahce的文档目录(/etc/httpd/html),多了一个缩图文件test-tn.jpg。
大功告成,班师回朝。

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

AI Hentai Generator
Generate AI Hentai for free.

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



When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

If you have used Docker, you must understand daemons, containers, and their functions. A daemon is a service that runs in the background when a container is already in use in any system. Podman is a free management tool for managing and creating containers without relying on any daemon such as Docker. Therefore, it has advantages in managing containers without the need for long-term backend services. Additionally, Podman does not require root-level permissions to be used. This guide discusses in detail how to install Podman on Ubuntu24. To update the system, we first need to update the system and open the Terminal shell of Ubuntu24. During both installation and upgrade processes, we need to use the command line. a simple

While studying in high school, some students take very clear and accurate notes, taking more notes than others in the same class. For some, note-taking is a hobby, while for others, it is a necessity when they easily forget small information about anything important. Microsoft's NTFS application is particularly useful for students who wish to save important notes beyond regular lectures. In this article, we will describe the installation of Ubuntu applications on Ubuntu24. Updating the Ubuntu System Before installing the Ubuntu installer, on Ubuntu24 we need to ensure that the newly configured system has been updated. We can use the most famous "a" in Ubuntu system

Detailed steps to install Go language on Win7 computer Go (also known as Golang) is an open source programming language developed by Google. It is simple, efficient and has excellent concurrency performance. It is suitable for the development of cloud services, network applications and back-end systems. . Installing the Go language on a Win7 computer allows you to quickly get started with the language and start writing Go programs. The following will introduce in detail the steps to install the Go language on a Win7 computer, and attach specific code examples. Step 1: Download the Go language installation package and visit the Go official website

Installing Go language under Win7 system is a relatively simple operation. Just follow the following steps to successfully install it. The following will introduce in detail how to install Go language under Win7 system. Step 1: Download the Go language installation package. First, open the Go language official website (https://golang.org/) and enter the download page. On the download page, select the installation package version compatible with Win7 system to download. Click the Download button and wait for the installation package to download. Step 2: Install Go language

Essential PHP programs: Install these to run smoothly! PHP is a popular server-side scripting language that is widely used to develop web applications. To successfully run a PHP program, you first need to install some necessary software and tools on the server. In this article, we will introduce the software and tools that must be installed, along with specific code examples to help you run PHP programs smoothly. 1. PHP interpreter The core of the PHP program is the PHP interpreter, which is responsible for parsing and executing PHP code. To install the PHP interpreter, you can follow

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension ".a" have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L
