Php GD library
Php uses the GD library to generate thumbnails.
Code:
- <Form method = "post" action = "suo_do.php" enctype = "multipart/form-data">
- <Input type = "file" name = "pic"/>
- <Input type = "submit" value = "Upload 1"/>
- </Form>
- <? Php
- 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.jbxue.com
- // 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 );