Share an example of generating thumbnails using the GD library of php. For more information, see.
The code is as follows:
Header ("content-type: text/html; charset = gbk ");
Ini_set ("date. timezone", "Asia/chong ");
// Determine whether the file is empty
If (empty ($ _ FILES )){
Echo "uploading files too large ";
Exit;
}
// Determine whether the file is uploaded incorrectly
If ($ _ FILES ['Pic '] ['error']) {
Echo "upload files ";
Exit;
}
// Determine whether the file type is illegal to obtain the file suffix
$ Allowtype = array ("jpg", "png", "jpeg", "gif ");
$ A = explode ('.', $ _ FILES ['Pic '] ['name']);
$ Index = count ($ a)-1;
$ Ex = strtolower ($ a [$ index]);
If (! In_array ($ ex, $ allowtype )){
Echo "Illegal File upload ";
Exit;
}
$ File = date ('ymdhis '). rand (). ".". $ ex;
$ Src = $ _ FILES ['Pic '] ['tmp _ name'];
$ Des = "upload/". $ file;
$ Rs = move_uploaded_file ($ src, $ des );
// Thumbnail
// Read the uploaded image
$ Image = imagecreatefromjpeg ($ des );
$ A = getimagesize ($ des );
$ W = $ a [0];
$ H = $ a [1];
If ($ w> $ h ){
$ Width = 300;
$ Height = $ width/$ w * $ h;
} Else if ($ w <$ h ){
$ Height = 300;
$ Width = $ height/$ h * $ w;
} Else {
$ Width = 300;
$ Height = 300;
} Www.jb51.net
// Create a blank image
$ Newimage = imagecreatetruecolor ($ width, $ height );
// Copy source image content copy New Image
Imagecopyresized ($ newimage, $ image, 0, 0, 0, $ width, $ height, $ w, $ h );
$ Filename = "upload/s _". $ file;
Imagejpeg ($ newimage, $ filename );