The gd Library is a specialized library for processing images in the php Tutorial. It can process most data conveniently and quickly. It provides a large number of image processing functions, the following uses the function of the gd library to generate thumbnails.
Test Code
<? Php
Include ('resizeimage. Php ');
If (! Empty ($ _ post )){
Echo ($ filename. ". jpg? Cache = ". rand (999999 ));
}
?>
<Form name = "test" action = "? Submit = true "enctype =" multipart/form-data "method =" post ">
<Input type = "file" name = "image" size = "50" value = ""> <p>
<Input type = "submit" value = "upload image">
</Form>
Resizeimage. php file
<? Php
$ Filename = "image. thumb ";
// Generate the image width
$ Resizewidth = 400;
// Generate the image height
$ Resizeheight = 400;
Function resizeimage ($ im, $ maxwidth, $ maxheight, $ name ){
$ Width = imagesx ($ im );
$ Height = imagesy ($ im );
If ($ maxwidth & $ width> $ maxwidth) | ($ maxheight & $ height> $ maxheight )){
If ($ maxwidth & $ width> $ maxwidth ){
$ Widthratio = $ maxwidth/$ width;
$ Resizewidth = true;
}
If ($ maxheight & $ height> $ maxheight ){
$ Heightratio = $ maxheight/$ height;
$ Resizeheight = true;
}
If ($ resizewidth & $ resizeheight ){
If ($ widthratio <$ heightratio ){
$ Ratio = $ widthratio;
} Else {
$ Ratio = $ heightratio;
}
} Elseif ($ resizewidth ){
$ Ratio = $ widthratio;
} Elseif ($ resizeheight ){
$ Ratio = $ heightratio;
}
$ Newwidth = $ width * $ ratio;
$ Newheight = $ height * $ ratio;
If (function_exists ("imagecopyresampled ")){
$ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
} Else {
$ Newim = imagecreate ($ newwidth, $ newheight );
Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height );
}
Imagejpeg ($ newim, $ name. ". jpg ");
Imagedestroy ($ newim );
} Else {
Imagejpeg ($ im, $ name. ". jpg ");
}
}
If ($ _ files ['image'] ['size']) {
If ($ _ files ['image'] ['type'] = "image/pjpeg "){
$ Im = imagecreatefromjpeg ($ _ files ['image'] ['tmp _ name']);
} Elseif ($ _ files ['image'] ['type'] = "image/x-png "){
$ Im = imagecreatefrompng ($ _ files ['image'] ['tmp _ name']);
} Elseif ($ _ files ['image'] ['type'] = "image/gif "){
$ Im = imagecreatefromgif ($ _ files ['image'] ['tmp _ name']);
}
If ($ im ){
If (file_exists ("your filename.jpg ")){
Unlink ("your filename.jpg ");
}
Resizeimage ($ im, $ resizewidth, $ resizeheight, $ filename );
Imagedestroy ($ im );
}
}
?>