Configure PHP to support both the GIF and jpeg_php basics

Source: Internet
Author: User
Tags file url install php imagejpeg php programming vars
Problem: After installing blue Dot Linux 2.0, PHP programming, found that only processing GIF images, can not handle JPEG images. Later know that PHP processing images, using the GD library, and the GD library to support the beginning of GIF, but because GIF use of copyright controversial LZW algorithm, will cause legal problems, so from the GD-1.6, the GD library no longer support GIF, to support better, no copyright dispute png. And now I want to support both gif,png and JPEG. After trying, I succeeded in doing it. The following is an introduction to the specific approach. My configuration is: Blue Dot Linux 2.0,kernel-2.2.16,mysql-3.23.10alpha,apache-1.3.12,php4.0.1pl2,gd-1.8.3,jpeg6b.





I will be introduced in the order of the bottom up, that is, Jpeg->gd->php->apache.

0, current Status View
After installing Blue Dot Linux 2.0, Kernel,mysql,apache did not make any changes to see what the current state was.
Create a php file named info.php, placed in the APAHCE document directory (/etc/httpd/html), which reads as follows:
? Phpinfo ();?>
The file is only 1 lines. Open the file URL in the browser (my hostname is ZHANGZH):
http://zhangzh/info.php
If the apache/php works, the PHP version, the Apache version, and all the other useful information will be listed in the page. I am concerned about the GD library section to see if it supports GIF, JPEG, and found support for GIF rather than JPEG support.

1, installation and configuration of jpeg6b
The main files of JPEG are jpeglib.h, LIBJPEG.A, libjpeg.so and so on. First check the system installed did not, to the/usr/include directory to see if there is no jpeglib.h, to the/usr/lib directory to see if there is no libjpeg.a and libjpeg.so. Not in my system, so to install.
To obtain the JPEG source address is:
ftp://ftp.uu.net/graphics/jpeg/
The documents obtained are jpegsrc.v6b.tar.gz and placed in the/USR/SRC directory.
Enter the/USR/SRC directory and start the installation process.
Enter/USR/SRC:
Cd/usr/src
To undo a compressed file:
Tar xzvf jpegsrc.v6b.tar.gz
After the command is complete, a subdirectory jpeg-6b,jpeg the source file is in it.
Enter this subdirectory:
CD jpeg-6b
The Install.doc file in this directory details how to install JPEG, so follow the rules.
To configure the build makefile file:
./configure
After the command is complete, there is a makefile file in the directory. Makefile file is a lot of software compiled, installed configuration and process control files, is very important, should learn to understand its content.
Start compilation:
Make
After the command is complete, there are many more files in the directory, which are important to libjpeg.a and libjpeg.so.
Installation:
Make install
When the command is complete, jpeglib.h is handcuffed to the/usr/local/include directory, LIBJPEG.A and libjpeg.so are handcuffed to the/usr/local/lib directory.

2, installation and configuration of GD-1.8.3
GD's main documents are gd.h, LIBGD.A and so on.
To obtain the GD source address is:
http://www.boutell.com/gd/
The documents obtained are gd-1.8.3.tar.gz and placed in the/USR/SRC directory.
Know that this version of GD does not support GIF, but want to like me as I hope GD support GIF and JPEG many people, so someone made a patch to add to the GIF support. It seems to be an Englishman, his email address is adam@elysium.ltd.uk.
To obtain the patch source address is:
Http://www.webofsin.com/gd-1.8.3-gif.patch
The documents obtained are gd-1.8.3-gif.patch and placed in the/USR/SRC directory.
Enter/USR/SRC:
Cd/usr/src
To undo a compressed file:
Tar xzvf gd-1.8.3.tar.gz
After the command is complete, a subdirectory gd-1.8.3,gd the source file is in it.
To the source code to make a patch:
After the PATCH-P0 command was completed, a GD_GIF.C file was gd-1.8.3 in the subdirectory, and the files Gd.h and makefile were modified accordingly.
Enter this subdirectory:
CD gd-1.8.3
By default, the GD library does not add JPEG support at compile time, and you have to modify the makefile file.
Modify the Makefile file so that:
Cflags=-o-dhave_xpm-dhave_jpeg-dhave_libttf
Libs=-lm-lgd-lpng-lz-ljpeg-lttf-lxpm-lx11
After that, compile and install:
Make
Make install
When the command is complete, gd.h is handcuffed to the/usr/local/include directory and LIBGD.A is handcuffed to the/usr/local/lib directory.

3, installation and configuration of PHP-4.0.1PL2
PHP's main files are libphp4.a, libphp4.so and so on.
To obtain the PHP source address is:
Http://php.net
The documents obtained are php-4.0.1pl2.tar.gz and placed in the/USR/SRC directory.
Enter the/USR/SRC directory and unzip the file:
Cd/usr/src
Tar xzvf php-4.0.1pl2.tar.gz
After the command is complete, a subdirectory php-4.0.1pl2,php the source file is in it.
Enter this subdirectory:
CD PHP-4.0.1PL2
The install file in this directory details how to install PHP, and follow the rules.
To configure the build makefile file:
./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 '
Note that the last line of parameter '--with-jpeg-dir=/usr/local '--with-gd=/usr/local ' indicates that the directory for JPEG and GD is/usr/local, according to step 1, 2 The result of the install.
(because the command is too long, it is recommended to write a shell file for execution.) The contents of document my-php-conf are as follows:
#! /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 '
To execute with the shell:
SH my-php-conf
The effect is the same. )
After the command is complete, there is a makefile file in the directory.
Compile and install:
Make
Make install
When the command is complete, libphp4.so is handcuffed to the/usr/lib/apache directory.

4, the Apache configuration
Apache itself does not have to recompile the installation, but using the new PHP, you need to let Apache know that you have to modify the Apache configuration file and restart the Apache service.
Modify the Apache configuration file/etc/httpd/conf/httpd.conf to include the following lines in the file:
LoadModule Php4_module modules/libphp4.so
Addmodule mod_php4.c
AddType application/x-httpd-php. php3. php
Also note that the corresponding line of the old PhP3 is commented out, otherwise there will be conflicts and the Apache restart failed.
Restart the Apache service:
/ETC/RC.D/INIT.D/HTTPD restart

5. Example Test
Again, check the current state as described in step 0, and I've seen PHP change to a new version number, and the GD library also supports GIF and JPEG.
But I still want to use an example to test, the function of this example is to read a GIF file, generate thumbnails, and then save as another JPG file. The contents of document create-thumb.php are as follows:
?
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 <= $dstW && $srcH <= $dstH)
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);
?>
Place the file in the Apahce document directory (/etc/httpd/html) and place the test image file Test.gif in the directory, and then open the PHP file in the browser (my hostname is ZHANGZH):
http://zhangzh/create-thumb.php
No error message. Then look at the APAHCE document directory (/etc/httpd/html), more than one thumbnail file test-tn.jpg.

Success, cavalry back to the face.


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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.