login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > GD library > [CLASS]Upload or Rip ( ul_rip )

[CLASS]Upload or Rip ( ul_rip )

Auteur: Sc0tTy - 02 oktober 2006 - 23:22 - Gekeurd door: Wijnand - Hits: 5597 - Aantal punten: (0 stemmen)





Deze class kan een plaatje die verwerken die is geupload of van een andere website komt.
Er word ook een thumbnail gegenereert, er kan met een apparte aanvraag ook een update worden gedaan in het database. door de extensie erbij te laden

Wanneer de classe als eerste word aangeroepen moeten er twee variabelen gegeven worden ::
Eerste parameter : de dir waar de foto uit eindelijk heen moet( interne dir )
Tweede parameter : de dir waar de foto uit eindelijk heen moet( HTTP ) ( optioneel , wanneer gegeven zal de class een array retourneren met bestand naam, url voor de foto en thumb )
De dir voor de thumbnails zal automatisch worden genomen : dir waar de foto komt en in die dir de dir thumbs
De dir's moeten correct ge chmod zijn

Er kunnen dan 2 functies worden aangeroepen ::
rip_file : zal de foto van een externe locatie kopieren en verwerken
ul_file : zal de foto van een interne locatie ( upload formulier ) verwerken
Deze functies hebben dezelfde parameters
Eerste parameter : locatie van het bestand
Tweede parameter : nieuwe naam van het bestand( OPTIONEEL )
Ze zullen het nieuwe bestand returnen


EDIT :: kleine bug gefixed

Voorbeeld om hem aan te roepen :
  1. <?php
  2. // Klasse aanmaken
  3. $screen = new ul_rip(
  4. $_SERVER['DOCUMENT_ROOT'] . '/alt.binz/screenshots/'
  5. );
  6.  
  7. // Verwerken die hap
  8. $screenshot = $screen->ul_file(
  9. $_FILES['ul']['tmp_name'] ,
  10. $_FILES['ul']['name']
  11. );
  12. ?>

Code:
De klasse :
  1. <?php
  2.  
  3. class ul_rip {
  4.  
  5. /*
  6. ** DUTCH **
  7. Deze class kan een plaatje die verwerken die is geupload of van een andere website komt.
  8. Er word ook een thumbnail gegenereert, er kan met een apparte aanvraag ook een update worden gedaan in het database. door de extensie erbij te laden
  9.  
  10. Wanneer de classe als eerste word aangeroepen moeten er twee variabelen gegeven worden ::
  11. Eerste parameter : de dir waar de foto uit eindelijk heen moet( interne dir )
  12. Tweede parameter : de dir waar de foto uit eindelijk heen moet( HTTP ) ( optioneel , wanneer gegeven zal de class een array retourneren met bestand naam, url voor de foto en thumb )
  13. De dir voor de thumbnails zal automatisch worden genomen : dir waar de foto komt en in die dir de dir thumbs
  14. De dir's moeten correct ge chmod zijn
  15.  
  16. Er kunnen dan 2 functies worden aangeroepen ::
  17. rip_file : zal de foto van een externe locatie kopieren en verwerken
  18. ul_file : zal de foto van een interne locatie ( upload formulier ) verwerken
  19. Deze functies hebben dezelfde parameters
  20. Eerste parameter : locatie van het bestand
  21. Tweede parameter : nieuwe naam van het bestand, ( OPTIONEEL )
  22. Ze zullen het nieuwe bestand returnen
  23.  
  24. ** ENLISH **
  25. This class is able to load an image and copy it to the website and proces it
  26. A thumbnail will be generated, by adding the extension you can olso update the mysql db
  27.  
  28. When the class is first called 2 variables must be givin ::
  29. First parameter : dir where the image should end up ( internal dir )
  30. Second parameter : dir where the image should end up ( HTTP ) ( optional, when given the class will return an array with the image name, and urls for the image and thumb )
  31. De dir for the thumbnails will be automaticily selected, the dir where the image will be stored and in there the folder thumbs
  32. The dirs must be correctly chmodded
  33.  
  34. Then 2 functions can be can be called :
  35. rip_file : will copy an image from an external location and proces it
  36. ul_file : will proces an internal image e.g : upload form
  37. These functions both have the same parameters :
  38. First parameter : location of the image
  39. Second parameter : the new name of the image, ( extension is needed if the first parameter doesn't contain a extension else file will end up not having an extension e.g. in an upload form ) ( OPTIONAL )
  40. They will return the new filename
  41.  
  42. Created by Sc0tTy on 26-09-2006
  43. Last modified on 03-10-2006
  44. www.SenS-Design.nl
  45. */
  46.  
  47. // Classe starten en de settings goedzetten
  48. function ul_rip( $pro_dir , $pro_url = FALSE ) {
  49.  
  50. $this->pro_dir = $pro_dir;
  51. $this->pro_url = $pro_url;
  52. }
  53.  
  54. function rip_file() {
  55.  
  56. // Argumenten in array zetten
  57. $this->arguments = func_get_args();
  58.  
  59. // Kijken of de inhoud van het bestand kan worden gelezen
  60. @$handle = fopen ( $this->arguments[0], "r" );
  61. if ( !$handle ) {
  62. return FALSE;
  63. }
  64. fclose( $handle );
  65.  
  66. // Bestand inlezen
  67. @$file = file_get_contents( $this->arguments[0] );
  68. if ( !$file ) {
  69. return FALSE;
  70. }
  71.  
  72. // Naam pakken
  73. $this->get_name();
  74.  
  75. // Tijdelijk bestand aanmaken
  76. $wfile = fopen( $this->file , "w" );
  77.  
  78. // Kopieren
  79. fwrite( $wfile , $file );
  80.  
  81. // Handler sluiten
  82. fclose( $wfile );
  83.  
  84. // Het geuploade bestand verwerken
  85. $this->handle_file();
  86.  
  87. return $this->arr_names;
  88. }
  89.  
  90. function ul_file() {
  91.  
  92. // Argumenten in array zetten
  93. $this->arguments = func_get_args();
  94.  
  95. // Naam pakken
  96. $this->get_name();
  97.  
  98. // Plaatje naar dir zetten en hernoemen
  99. rename( $this->arguments[0] , $this->file );
  100.  
  101. // Het geuploade bestand verwerken
  102. $this->handle_file();
  103.  
  104. return $this->arr_names;
  105. }
  106.  
  107. // Naam pakken
  108. function get_name() {
  109.  
  110. // Naam en extensie opslaan
  111. $arr_name = explode( '/' , $this->arguments[0] );
  112. $name = array_pop( $arr_name );
  113. $arr_name = explode( '.' , $name );
  114. $this->name = $arr_name[0];
  115. if ( $this->arguments[1] ) {
  116. $arr_name = explode( '.' , $this->arguments[1] );
  117. $this->name = $arr_name[0];
  118. }
  119.  
  120. // Naam vast zetten
  121. $this->file = $this->pro_dir . $this->name . '.jpg';
  122.  
  123. // Array voor retourneren vastzetten
  124. $this->arr_names = $this->name . '.jpg';
  125. if ( $this->pro_url ) {
  126. $this->arr_names = array(
  127. $this->name . '.jpg' ,
  128. $this->pro_url . $this->name . '.jpg' ,
  129. $this->pro_url . 'thumbs/' . $this->name . '.jpg'
  130. );
  131. }
  132. }
  133.  
  134. function handle_file() {
  135.  
  136. // Plaatje zonodig verkleinen
  137. $this->resize_file();
  138.  
  139. // Thumbnail maken
  140. $this->create_thumb();
  141.  
  142. // Chmodden
  143. chmod( $this->file , 0777 );
  144. chmod( $this->thumb , 0777 );
  145. }
  146.  
  147. function resize_file() {
  148.  
  149. if ( !isset( $min_width ) ) {
  150. $min_width = 1;
  151. $up_width = 0.1;
  152. }
  153.  
  154. // Verkleinen tot dat file size minder of gelijk is aan 131072 ( 128kb )
  155. if ( filesize( $this->file ) > 131073 ) {
  156.  
  157. // Foto aanmaken
  158. $type = getimagesize( $this->file );
  159. $type = $type['mime'];
  160.  
  161. switch ( $type ) {
  162. case 'image/png' : $image = imagecreatefrompng( $this->file ); break;
  163. case 'image/bmp' : $image = imagecreatefromwbmp( $this->file ); break;
  164. case 'image/gif' : $image = imagecreatefromgif( $this->file ); break;
  165. case 'image/jpeg' : $image = imagecreatefromjpeg( $this->file ); break;
  166. case 'image/jpg' : $image = imagecreatefromjpeg( $this->file ); break;
  167. case 'image/pjpeg' : $image = imagecreatefromjpeg( $this->file ); break;
  168. }
  169. if ( !$image ) {
  170. die ( "ERROR :: An error occured when trying to read the files image construction." );
  171. }
  172.  
  173. //Hoogte n breedte
  174. $width = imagesx( $image );
  175. $height = imagesy( $image );
  176.  
  177. // Hoogte en breedte uitrekenen
  178. $nwidth = intval( $width/$min_width );
  179. $tmp = $width/$nwidth;
  180. $nheight = intval( $height/$tmp );
  181.  
  182. // Maak blanke image
  183. $newsize = imagecreatetruecolor( $nwidth , $nheight );
  184. if ( !$newsize ) {
  185. die ( "ERROR :: An error occured when trying to create a blank image." );
  186. }
  187.  
  188. // Verkleinen
  189. $result = imagecopyresampled( $newsize, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height );
  190. if ( !$result ) {
  191. die ( "ERROR :: An error occured when trying to resize and write the original image to the blank image." );
  192. }
  193.  
  194. // Opslaan
  195. $result = imagejpeg( $newsize , $this->file . '_temp' , 100 );
  196. if ( !$result ) {
  197. die ( "ERROR :: An error occured when trying to write new image to the original file." );
  198. }
  199.  
  200. //Opschonen
  201. imagedestroy( $newsize );
  202. imagedestroy( $image );
  203.  
  204. // Kijken of hij kleiner is
  205. if ( filesize( $this->file . '_temp' ) > 131073 ) {
  206. $min_width *= filesize( $this->file )/131073;
  207. $this->resize_file();
  208. } else {
  209. unlink( $this->file );
  210. rename( $this->file . '_temp' , $this->file );
  211. }
  212. }
  213. }
  214.  
  215. function create_thumb() {
  216.  
  217. // Foto aanmaken
  218. $type = getimagesize( $this->file );
  219. $type = $type['mime'];
  220.  
  221. switch ( $type ) {
  222. case 'image/png' : $image = imagecreatefrompng( $this->file ); break;
  223. case 'image/bmp' : $image = imagecreatefromwbmp( $this->file ); break;
  224. case 'image/gif' : $image = imagecreatefromgif( $this->file ); break;
  225. case 'image/jpeg' : $image = imagecreatefromjpeg( $this->file ); break;
  226. case 'image/jpg' : $image = imagecreatefromjpeg( $this->file ); break;
  227. case 'image/pjpeg' : $image = imagecreatefromjpeg( $this->file ); break;
  228. }
  229. if ( !$image ) {
  230. die ( "ERROR :: An error occured when trying to read the files image construction." );
  231. }
  232.  
  233. //Hoogte n breedte
  234. $width = imagesx( $image );
  235. $height = imagesy( $image );
  236.  
  237. // Hoogte en breedte uitrekenen
  238. $nwidth = 255;
  239. $tmp = $width/$nwidth;
  240. $nheight = $height/$tmp;
  241.  
  242. if ( $nheight > 200 ) {
  243. $tmp = $nheight/200;
  244. $nwidth /= $tmp;
  245. $nheight = 200;
  246. }
  247.  
  248. // Maak blanke image
  249. $thumb = imagecreatetruecolor( $nwidth , $nheight );
  250. if ( !$thumb ) {
  251. die ( "ERROR :: An error occured when trying to create a blank image." );
  252. }
  253.  
  254. // Verkleinen
  255. $result = imagecopyresampled( $thumb, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height );
  256. if ( !$result ) {
  257. die ( "ERROR :: An error occured when trying to resize and write the original image to the blank image." );
  258. }
  259.  
  260. // Thumbnail locatie noteren
  261. $this->thumb = $this->pro_dir . 'thumbs/' . $this->name . '.jpg';
  262.  
  263. // Wegschrijven
  264. $result = imagejpeg( $thumb , $this->thumb );
  265.  
  266. if ( !$result ) {
  267. die ( "ERROR :: An error occured when trying to write new image to the original file." );
  268. }
  269.  
  270. //Opschonen
  271. imagedestroy( $thumb );
  272. imagedestroy( $image );
  273. }
  274. }
  275.  
  276. // Update de mysql tabel
  277. class ul_rip_update extends ul_rip {
  278.  
  279. function update() {
  280. /*
  281. THIS IS AN EXAMPLE , WHEN YOU GIVE THE URL IN THE CLASS , YOU COULD USE THE ARRAY
  282.  
  283. DIT IS EEN VOORBEELD, WANNEER JE DE URL GEEFT KUN JE DE ARRAY GEBRUIKEN
  284. */
  285.  
  286. // Database updaten
  287. $result = mysql_secure_query(
  288. "
  289. UPDATE
  290. YOUR TABLE
  291. SET
  292. foto = '?' ,
  293. thumb = '?'
  294. WHERE
  295. artnr = ?
  296. LIMIT 1
  297. ",
  298. $this->pro_url . $this->name . '.jpg',
  299. $this->pro_url . 'thumb/' . $this->name . '.jpg',
  300. $this->arguments[1]
  301. );
  302.  
  303. if ( !$result ) {
  304. return FALSE;
  305. }
  306. return TRUE;
  307. }
  308. }
  309.  
  310.  
  311. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.046s