php实现从上传文件创建缩略图的方法
本文实例讲述了php实现从上传文件创建缩略图的方法。分享给大家供大家参考。具体实现方法如下:
<?php if($_REQUEST['action']=="add"){ $userfile=$HTTP_POST_FILES['photo']['tmp_name']; $userfile_name=$HTTP_POST_FILES['photo']['name']; $userfile_size=$HTTP_POST_FILES['photo']['size']; $userfile_type=$HTTP_POST_FILES['photo']['type']; ///////////////////////// //GET-DECLAREDIMENSIONS// $dimension=getimagesize($userfile); $large_width=$dimension[0];//GETPHOTOWIDTH $large_height=$dimension[1];//GETPHOTOHEIGHT $small_width=120;//DECLARETHUMBWIDTH $small_height=90;//DECLARETHUMBHEIGHT ///////////////////////// //CHECKSIZE// if($userfile_size>102400){ $error=1; $msg="Thephotoisover100kb.Pleasetryagain."; } //////////////////////////////// //CHECKTYPE(IEANDOTHERS)// if($userfile_type="image/pjpeg"){ if($userfile_type!="image/jpeg"){ $error=1; $msg="ThephotomustbeJPG"; } } ////////////////////////////// //CHECKWIDTH/HEIGHT// if($large_width!=600or$large_height!=400){ $error=1; $msg="Thephotomustbe600x400pixels"; } /////////////////////////////////////////// //CREATETHUMB/UPLOADTHUMBANDPHOTO/// if($error<>1){ $image=$userfile_name;//ifyouwanttoinsertittothedatabase $pic=imagecreatefromjpeg($userfile); $small=imagecreatetruecolor($small_width,$small_height); imagecopyresampled($small,$pic,0,0,0,0,$small_width,$small_height,$large_width,$large_height); if(imagejpeg($small,"path/to/folder/to/upload/thumb".$userfile_name,100)){ $large=imagecreatetruecolor($large_width,$large_height); imagecopyresampled($large,$pic,0,0,0,0,$large_width,$large_height,$large_width,$large_height); if(imagejpeg($large,"path/to/folder/to/upload/photo".$userfile_name,100)) {} else{$msg="Aproblemhasoccured.Pleasetryagain.";$error=1;} } else{ $msg="Aproblemhasoccured.Pleasetryagain.";$error=1; } } ////////////////////////////////////////////// ///Ifeverythingwentrightaphoto(600x400)and ///athumb(120x90)wereuploadedtothegivenfolders } ?> <html><head><title>createthumb</title></head> <body> <formname="form1"enctype="multipart/form-data"action="thisfile.php?action=add"method="post"> SelectPhoto:<inputtype="file"name="photo"> <inputtype="submit"name="submit"value="CREATETHUMBANDUPLOAD"> </form> </body </html>
希望本文所述对大家的php程序设计有所帮助。