<?php
/**
 * Поиск MP3 (ВКонтакте)
 *
 * Автор: byvlad
 * Версия: 2.12
 * Стоимость: 400 WMR
 */

require_once ('system/core.php');		// Ядро скрипта
if(empty($argv[1]) or !is_numeric($argv[1]))
	exit('Fuck!');

$id = (int)$argv[1];
$query = $mysqli -> query("SELECT * FROM `songs` WHERE `id` = " . $id) -> fetch_assoc();
if(!$query)
	die('Fatal error!');

/**
* Ресайз обложки
*/

class SimpleImage {

   	public $image;
   	public $image_type;

	function load($filename) {
		$image_info = getimagesize($filename);
      	$this->image_type = $image_info[2];
      	if( $this->image_type == IMAGETYPE_JPEG ) {
			$this->image = imagecreatefromjpeg($filename);
      	} elseif( $this->image_type == IMAGETYPE_GIF ) {
        	$this->image = imagecreatefromgif($filename);
      	} elseif( $this->image_type == IMAGETYPE_PNG ) {
       		$this->image = imagecreatefrompng($filename);
      	}
   	}

   	function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 93, $permissions = NULL) {
      	if( $image_type == IMAGETYPE_JPEG ) {
         	imagejpeg($this->image, $filename, $compression);
      	} elseif( $image_type == IMAGETYPE_GIF ) {
         	imagegif($this->image, $filename, $compression);
      	} elseif( $image_type == IMAGETYPE_PNG ) {
         	imagepng($this->image, $filename, $compression);
      	}
      	if($permissions != NULL) {
         	chmod($filename, $permissions);
      	}
   	}

   	function output($image_type=IMAGETYPE_JPEG) {
      	if( $image_type == IMAGETYPE_JPEG ) {
         	imagejpeg($this->image);
      	} elseif( $image_type == IMAGETYPE_GIF ) {
         	imagegif($this->image);
      	} elseif( $image_type == IMAGETYPE_PNG ) {
         	imagepng($this->image);
      	}
   	}

   	function getWidth() {
      	return imagesx($this->image);
   	}

   	function getHeight() {
      	return imagesy($this->image);
   	}

   	function resizeToHeight($height) {
      	$ratio = $height / $this->getHeight();
      	$width = $this->getWidth() * $ratio;
      	$this->resize($width,$height);
   	}

   	function resizeToWidth($width) {
      	$ratio = $width / $this->getWidth();
      	$height = $this->getheight() * $ratio;
      	$this->resize($width,$height);
   	}

   	function scale($scale) {
      	$width = $this->getWidth() * $scale/100;
      	$height = $this->getheight() * $scale/100;
      	$this->resize($width,$height);
   	}

  	function resize($width = '', $height = '') {
		if(empty($width) and empty($height)) {
  			if ($this -> getWidth() > $this -> getHeight() and $this -> getWidth() >= 300) {
				$prop = $this -> getWidth() / $this -> getHeight();
				$width = 300;
				$height = ceil($width/$prop);
			} elseif($this -> getHeight() > $this -> getWidth() and $this -> getHeight() >= 300) {
				$prop=$this -> getHeight()/$this -> getWidth();
				$height = 300;
				$width = ceil($height/$prop);
			} else {
     			$width = $this -> getWidth();
  				$height = $this -> getHeight();
			}
		}

      	$new_image = imagecreatetruecolor($width, $height);
      	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      	$this->image = $new_image;
   	}
}


$img = 'files/' . $id . '/picture.jpg';

if(file_exists($img)){
	$image = new SimpleImage;
   	$image->load($img);
   	$image->resizeToWidth(100);
   	unlink($img);
   	$image->save('files/' . $id . '/picture.jpg');
} else {
	header('Location: http://' . $_SERVER['HTTP_HOST'] . $extdir . 'error.html');
	exit;
}
?>