De klasse :<?php

class ul_rip {

/*
	** DUTCH **
	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
		
	** ENLISH **
	This class is able to load an image and copy it to the website and proces it
	A thumbnail will be generated, by adding the extension you can olso update the mysql db
	
	When the class is first called 2 variables must be givin ::
		First parameter		:	dir where the image should end up ( internal dir )
		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 )
	De dir for the thumbnails will be automaticily selected, the dir where the image will be stored and in there the folder thumbs
	The dirs must be correctly chmodded
		
	Then 2 functions can be can be called :
		rip_file			:	will copy an image from an external location and proces it
		ul_file			:	will proces an internal image e.g : upload form
	These functions both have the same parameters :
		First parameter		:	location of the image
		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 )
	They will return the new filename
	
	Created by Sc0tTy on 26-09-2006
	Last modified on 03-10-2006
	www.SenS-Design.nl
*/

	// Classe starten en de settings goedzetten
	function ul_rip( $pro_dir , $pro_url = FALSE ) {
		
		 $this->pro_dir = $pro_dir;
		 $this->pro_url = $pro_url;
	}
	
	function rip_file() {
		
		// Argumenten in array zetten
		$this->arguments = func_get_args();
		
		// Kijken of de inhoud van het bestand kan worden gelezen
		@$handle = fopen ( $this->arguments[0], "r" );
		if ( !$handle ) {
			return FALSE;
		}
		fclose( $handle );
		
		// Bestand inlezen
		@$file = file_get_contents( $this->arguments[0] );
		if ( !$file ) {
			return FALSE;
		}
		
		// Naam pakken
		$this->get_name();
		
		// Tijdelijk bestand aanmaken
		$wfile = fopen( $this->file , "w" );
		
		// Kopieren
		fwrite( $wfile , $file );
		
		// Handler sluiten
		fclose( $wfile );
		
		//  Het geuploade bestand verwerken
		$this->handle_file();

		return $this->arr_names;
	}
	
	function ul_file() {
		
		// Argumenten in array zetten
		$this->arguments = func_get_args();
		
		// Naam pakken
		$this->get_name();
		
		// Plaatje naar dir zetten en hernoemen
		rename( $this->arguments[0] , $this->file );
		
		//  Het geuploade bestand verwerken
		$this->handle_file();

		return $this->arr_names;
	}
	
	// Naam pakken
	function get_name() {
		
		// Naam en extensie opslaan
		$arr_name = explode( '/' , $this->arguments[0] );
		$name = array_pop( $arr_name );
		$arr_name = explode( '.' , $name );
		$this->name = $arr_name[0];
		if ( $this->arguments[1] ) {
			$arr_name = explode( '.' , $this->arguments[1] );
			$this->name = $arr_name[0];
		}
		
		// Naam vast zetten
		$this->file = $this->pro_dir . $this->name . '.jpg';
		
		// Array voor retourneren vastzetten
		$this->arr_names = $this->name . '.jpg';
		if ( $this->pro_url ) {
			$this->arr_names = array(
				$this->name . '.jpg' ,
				$this->pro_url . $this->name . '.jpg' ,
				$this->pro_url . 'thumbs/' . $this->name . '.jpg'
			);
		}
	}
	
	function handle_file() {
		
		// Plaatje zonodig verkleinen
		$this->resize_file();
		
		// Thumbnail maken
		$this->create_thumb();
		
		// Chmodden
		chmod( $this->file , 0777 );
		chmod( $this->thumb , 0777 );
	}

	function resize_file() {

		if ( !isset( $min_width ) ) {
			$min_width = 1;
			$up_width = 0.1;
		}
	
		// Verkleinen tot dat file size minder of gelijk is aan 131072 ( 128kb )
		if ( filesize( $this->file ) > 131073 ) {
				
			// Foto aanmaken
			$type = getimagesize( $this->file );
			$type = $type['mime'];
				
			switch ( $type ) {
				case 'image/png' : $image = imagecreatefrompng( $this->file ); break;
				case 'image/bmp' : $image = imagecreatefromwbmp( $this->file ); break;
				case 'image/gif' : $image = imagecreatefromgif( $this->file ); break;
				case 'image/jpeg' : $image = imagecreatefromjpeg( $this->file ); break;
				case 'image/jpg' : $image = imagecreatefromjpeg( $this->file ); break;
				case 'image/pjpeg' : $image = imagecreatefromjpeg( $this->file ); break;
			}
			if ( !$image ) {
				die ( "ERROR :: An error occured when trying to read the files image construction." ); 
			}
			
			//Hoogte n breedte
			$width = imagesx( $image );
			$height = imagesy( $image );
			
			// Hoogte en breedte uitrekenen
			$nwidth = intval( $width/$min_width );
			$tmp = $width/$nwidth;
			$nheight = intval( $height/$tmp );
				
			// Maak blanke image
			$newsize = imagecreatetruecolor( $nwidth , $nheight );
			if ( !$newsize ) {
				die ( "ERROR :: An error occured when trying to create a blank image." ); 
			}
				
			// Verkleinen
			$result = imagecopyresampled( $newsize, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height );
			if ( !$result ) {
				die ( "ERROR :: An error occured when trying to resize and write the original image to the blank image." ); 
			}
			
			// Opslaan
			$result = imagejpeg( $newsize , $this->file . '_temp' , 100 );
			if ( !$result ) {
				die ( "ERROR :: An error occured when trying to write new image to the original file." ); 
			}
			
			//Opschonen
			imagedestroy( $newsize );
			imagedestroy( $image );
			clearstatcache();

			// Kijken of hij kleiner is
			if ( filesize( $this->file . '_temp' ) > 131073 ) {
				$min_width *= filesize( $this->file )/131073;
				$this->resize_file();
			} else {
				unlink( $this->file );
				rename( $this->file . '_temp' , $this->file );
			}
		}
	}
	
	function create_thumb() {
		
		// Foto aanmaken
		$type = getimagesize( $this->file );
		$type = $type['mime'];

		switch ( $type ) {
			case 'image/png' : $image = imagecreatefrompng( $this->file ); break;
			case 'image/bmp' : $image = imagecreatefromwbmp( $this->file ); break;
			case 'image/gif' : $image = imagecreatefromgif( $this->file ); break;
			case 'image/jpeg' : $image = imagecreatefromjpeg( $this->file ); break;
			case 'image/jpg' : $image = imagecreatefromjpeg( $this->file ); break;
			case 'image/pjpeg' : $image = imagecreatefromjpeg( $this->file ); break;
		}
		if ( !$image ) {
			die ( "ERROR :: An error occured when trying to read the files image construction." ); 
		}
			
		//Hoogte n breedte
		$width = imagesx( $image );
		$height = imagesy( $image );

		// Hoogte en breedte uitrekenen
		$nwidth = 255;
		$tmp = $width/$nwidth;
		$nheight = $height/$tmp;

		if ( $nheight > 200 ) {
			$tmp = $nheight/200;
			$nwidth /= $tmp;
			$nheight = 200;
		}

		// Maak blanke image
		$thumb = imagecreatetruecolor( $nwidth , $nheight );
		if ( !$thumb ) {
			die ( "ERROR :: An error occured when trying to create a blank image." ); 
		}
		
		// Verkleinen
		$result = imagecopyresampled( $thumb, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height );
		if ( !$result ) {
			die ( "ERROR :: An error occured when trying to resize and write the original image to the blank image." ); 
		}
		
		// Thumbnail locatie noteren
		$this->thumb = $this->pro_dir . 'thumbs/' . $this->name . '.jpg';
		
		// Wegschrijven
		$result = imagejpeg( $thumb , $this->thumb );
		
		if ( !$result ) {
			die ( "ERROR :: An error occured when trying to write new image to the original file." ); 
		}
		
		//Opschonen
		imagedestroy( $thumb );
		imagedestroy( $image );
		clearstatcache();
	}
}

// Update de mysql tabel
class ul_rip_update extends ul_rip {

	function update() {
		/*
			THIS IS AN EXAMPLE , WHEN YOU GIVE THE URL IN THE CLASS , YOU COULD USE THE ARRAY
			
			DIT IS EEN VOORBEELD, WANNEER JE DE URL GEEFT KUN JE DE ARRAY GEBRUIKEN
		*/
		
		// Database updaten
		$result = mysql_secure_query(
		"
			UPDATE
				YOUR TABLE
			SET
				foto = '?' ,
				thumb = '?'
			WHERE
				artnr = ?
			LIMIT 1
		",
			$this->pro_url . $this->name . '.jpg',
			$this->pro_url . 'thumb/' . $this->name . '.jpg',
			$this->arguments[1]
		);
		
		if ( !$result ) {
			return FALSE;
		}
		return TRUE;
	}
}


?> 