Uploads. php file Class image_upload { Private $ srcimg; // original image Private $ destimg; // target image Private $ width; // The width of the original image. Private $ height; // The height of the original image Private $ type; // the image type of the original file Private $ thumb_width; // The width of the thumbnail. Private $ thumb_height; // The height of the thumbnail. Private $ cut; // whether to cut the image to the specified height Private $ tmp; // temporary address for uploading images Private $ error; Private $ im; // create a temporary Image handle Private $ new_name; // new name of the uploaded file Function _ construct ($ srcimg, $ t_width, $ t_height, $ cut, $ tmp, $ error ){ $ This-> srcimg = $ srcimg; $ This-> thumb_width = $ t_width; $ This-> thumb_height = $ t_height; $ This-> cut = $ cut; $ This-> tmp = $ tmp; $ This-> error = $ error; $ This-> get_srcimg_type (); $ This-> get_new_upload_name (); } Function img_upload (){ // File Upload method $ This-> check_error ($ this-> error ); $ This-> in_type (); $ Dst_dir = './images '; If (! Is_dir ($ dst_dir )){ Mkdir ($ dst_dir ); Echo "% "; } If (is_uploaded_file ($ this-> tmp )){ If (move_uploaded_file ($ this-> tmp, $ this-> new_name )){ Echo "file uploaded successfully "; Return true; } Else { Echo 'file cannot be moved, upload failed '; Exit; } } Else { Echo 'file Upload may be attacked '; Exit; } } Function make_thumbnail (){ // Method for generating thumbnails $ This-> get_dest_imgpath (); $ This-> make_im (); $ This-> width = imagesx ($ this-> im ); $ This-> height = imagesy ($ this-> im ); $ Thumb_ratio = $ this-> thumb_width/$ this-> thumb_height; $ Ratio = $ this-> width/$ this-> height; If ($ this-> cut = 1) {// whether to crop If ($ ratio >=$ thumb_ratio ){ $ Img = imagecreatetruecolor ($ this-> thumb_width, $ this-> thumb_height ); Imagecopyresampled ($ img, $ this-> im, 0, 0, 0, 0, $ this-> thumb_width, $ this-> thumb_height, $ this-> height * $ thumb_ratio, $ this-> height ); Imagejpeg ($ img, $ this-> destimg ); Echo "the thumbnail is successfully generated "; } Else { $ Img = imagecreatetruecolor ($ this-> thumb_width, $ this-> thumb_height ); Imagecopyresampled ($ img, $ this-> im, 0, 0, 0, 0, $ this-> thumb_width, $ this-> thumb_height, $ this-> width, $ this-> width/$ thumb_ratio ); Imagejpeg ($ img, $ this-> destimg ); Echo "the thumbnail is successfully generated "; } } Else { If ($ ratio >=$ thumb_ratio ){ $ Img = imagecreatetruecolor ($ this-> thumb_height * $ thumb_ratio, $ this-> thumb_height ); Imagecopyresampled ($ img, $ this-> im, 0, 0, 0, 0, $ this-> thumb_height * $ thumb_ratio, $ this-> thumb_height, $ this-> width, $ this-> height ); Imagejpeg ($ img, $ this-> destimg ); Echo "the thumbnail is successfully generated "; } Else { $ Img = imagecreatetruecolor ($ this-> thumb_width, $ this-> thumb_width/$ thumb_ratio ); Imagecopyresampled ($ img, $ this-> im, 0, 0, 0, 0, $ this-> thumb_width, $ this-> thumb_width/$ thumb_ratio, $ this-> width, $ this-> height ); Imagejpeg ($ img, $ this-> destimg ); Echo "the thumbnail is successfully generated "; } } Imagedestroy ($ this-> im ); Imagedestroy ($ img ); } Private function check_error ($ error ){ // Check the file upload errors; If ($ error> 0 ){ Switch ($ error ){ Case 1: Echo "the size of the uploaded file exceeds the configured value in the PHP. ini file. "; Break; Case 2: Echo "the size of the uploaded file exceeds the size limit in the form. "; Break; Case 3: Echo "only some files are uploaded "; Break; Case 4: Echo "no file is uploaded "; Break; Case 6: Echo "php. ini does not set the temporary unknown for storing images. "; Break; Case 7: Echo "hard disk cannot be written, upload failed "; Break; Default: Echo "Unknown error "; Break; } } } Private function get_srcimg_type (){ // Determine the image type of the source file $ This-> type = substr (strrchr ($ this-> srcimg, '.'), '1 '); } Private function in_type (){ // Check whether the file meets the type $ Type_arr = array ('GIF', 'jpg ', 'PNG '); If (! In_array ($ this-> type, $ type_arr )){ Echo "only supports PNG, GIF, and JPG file formats ......, Upload the correct format again "; Exit; } } Private function get_new_upload_name (){ // Generate a new name for the uploaded file $ This-> new_name = 'Images/'. date ('ymdhis'). '.'. $ this-> type; } Private function make_im (){ // Create an image from the original file Switch ($ this-> type ){ Case 'jpg ': $ This-> im = imagecreatefromjpeg ($ this-> new_name ); Break; Case 'GIF ': $ This-> im = imagecreatefromgif ($ this-> new_name ); Break; Case 'PNG ': $ This-> im = imagecreatefrompng ($ this-> new_name ); Break; } } Private function get_dest_imgpath (){ // Obtain the storage path of the thumbnail. $ Len1 = strlen ($ this-> new_name ); $ Len2 = strlen (strrchr ($ this-> new_name ,'.')); $ Len3 = $ len1-$ len2; $ This-> destimg = substr ($ this-> new_name, 0, $ len3). '_ small.'. $ this-> type; } } Print_r ($ _ FILES ); $ File = $ _ FILES ['image']; Echo $ file ['name']; $ Uploads = new image_upload ($ file ['name'], 120,160, 1, $ file ['tmp _ name'], $ file ['error']); If ($ uploads-> img_upload ()){ $ Uploads-> make_thumbnail (); } ?>
|