<commit_before><?php
/**
 * Gallery Photo
 *
 *
 * @category Model
 * @package  Croogo
 * @version  1.3
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.edineicipriani.com.br
 */
class Photo extends AppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
	var $name = 'Photo';

	var $useDbConfig = 'gallery';

	var $dir = '';

	var $albumDir = '';

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	var $belongsTo = array(
		'Album' => array(
			'className' => 'Gallery.album',
			'foreignKey' => 'album_id'
		)
	);

	function __construct($id = false, $table = null, $ds = null){
		parent::__construct($id = false, $table = null, $ds = null);
		$this->setTargetDirectory();
	}

	function getTargetDirectory() {
		return $this->dir;
	}

	function setTargetDirectory($dir = 'photos', $perm = 0775) {
		if ($dir == 'photos') {
			$this->albumDir = 'img' . DS . $dir . DS;
		} else {
			$this->albumDir = 'galleries' . DS . $dir . DS;
		}
		$this->dir = WWW_ROOT . $this->albumDir;
		if(!is_dir($this->dir)) {
			mkdir($this->dir, $perm, true);
		}
	}

	function beforeDelete(){
		$photo = $this->findById($this->id);
		unlink(WWW_ROOT . $photo['Photo']['small']);
		unlink(WWW_ROOT . $photo['Photo']['large']);
		return true;
	}

	function beforeSave(){
		$this->Behaviors->trigger($this, 'setupAlbumPath', array($this->data['Photo']['album_id']));
		$this->data = $this->upload($this->data);
		return true;
	}

	function upload($data){
		$max_width = Configure::read('Gallery.max_width');
		$thumb_width = Configure::read('Gallery.max_width_thumb');
		$thumb_height = Configure::read('Gallery.max_height_thumb');
		$thumb_quality = Configure::read('Gallery.quality');
		App::import('Vendor', 'Gallery.qqFileUploader', array('file' => 'qqFileUploader.php'));
	   	$uploader = new qqFileUploader();
		$result = $uploader->handleUpload($this->dir);

		$width = $this->getWidth($this->dir.$result['file']);
		$height = $this->getHeight($this->dir.$result['file']);
		if ($width > $max_width){
			$scale = $max_width/$width;
			$this->resizeImage($this->dir.$result['file'],$width,$height,$scale);
		}else{
			$scale = 1;
			$this->resizeImage($this->dir.$result['file'],$width,$height,$scale);
		}
		if (empty($thumb_height) && !empty($thumb_width)) {
			$this->resizeImage2('resize', $result['file'], $this->dir, 'thumb_'.$result['file'], $thumb_width, FALSE, $thumb_quality);
		}elseif (empty($thumb_width) && !empty($thumb_height)) {
			$this->resizeImage2('resize', $result['file'], $this->dir, 'thumb_'.$result['file'], FALSE, $thumb_height, $thumb_quality);
		}else{
			$this->resizeImage2('resizeCrop', $result['file'], $this->dir, 'thumb_'.$result['file'], $thumb_width, $thumb_height, $thumb_quality);
		}

		$data['Photo']['small'] = $this->albumDir . 'thumb_'.$result['file'];
		$data['Photo']['large'] = $this->albumDir . $result['file'];
		return $data;
	}


	function getHeight($image) {
		$sizes = getimagesize($image);
		return $sizes[1];
	}

	function getWidth($image) {
		$sizes = getimagesize($image);
		return $sizes[0];
	}

	function resizeImage($image,$width,$height,$scale) {
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		$ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
		$source = "";
		if($ext == "png"){
			$source = imagecreatefrompng($image);
		}elseif($ext == "jpg" || $ext == "jpeg"){
			$source = imagecreatefromjpeg($image);
		}elseif($ext == "gif"){
			$source = imagecreatefromgif($image);
		}
		imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
		imagejpeg($newImage,$image,100);
		chmod($image, 0777);
	}

	function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		$ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
		$source = "";
		if($ext == "png"){
			$source = imagecreatefrompng($image);
		}elseif($ext == "jpg" || $ext == "jpeg"){
			$source = imagecreatefromjpeg($image);<commit_msg>init optimizer<commit_after><?php
/**
 * Gallery Photo
 *
 *
 * @category Model
 * @package  Croogo
 * @version  1.3
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.edineicipriani.com.br
 */
class Photo extends AppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
	var $name = 'Photo';

	var $useDbConfig = 'gallery';

	var $dir = '';

	var $albumDir = '';

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	var $belongsTo = array(
		'Album' => array(
			'className' => 'Gallery.album',
			'foreignKey' => 'album_id'
		)
	);

	function __construct($id = false, $table = null, $ds = null){
		parent::__construct($id = false, $table = null, $ds = null);
		$this->setTargetDirectory();
	}

	function getTargetDirectory() {
		return $this->dir;
	}

	function setTargetDirectory($dir = 'photos', $perm = 0775) {
		if ($dir == 'photos') {
			$this->albumDir = 'img' . DS . $dir . DS;
		} else {
			$this->albumDir = 'galleries' . DS . $dir . DS;
		}
		$this->dir = WWW_ROOT . $this->albumDir;
		$this->sourceDir = WWW_ROOT . $this->albumDir . 'source' . DS;
		if(!is_dir($this->dir)) {
			mkdir($this->dir, $perm, true);
		}
		if(!is_dir($this->sourceDir)) {
			mkdir($this->sourceDir, $perm, true);
		}
	}

	function beforeDelete(){
		$photo = $this->findById($this->id);
		unlink(WWW_ROOT . $photo['Photo']['small']);
		unlink(WWW_ROOT . $photo['Photo']['large']);
		return true;
	}

	function beforeSave(){
		$this->Behaviors->trigger($this, 'setupAlbumPath', array($this->data['Photo']['album_id']));
		$this->data = $this->upload($this->data);
		return true;
	}

	function upload($data){
		$max_width = Configure::read('Gallery.max_width');
		$thumb_width = Configure::read('Gallery.max_width_thumb');
		$thumb_height = Configure::read('Gallery.max_height_thumb');
		$thumb_quality = Configure::read('Gallery.quality');
		App::import('Vendor', 'Gallery.qqFileUploader', array('file' => 'qqFileUploader.php'));
	   	$uploader = new qqFileUploader();
		//$result = $uploader->handleUpload($this->dir);
		$result = $uploader->handleUpload($this->sourceDir);
		if (!empty($result['file'])) {
			$sourceFile = $this->sourceDir.$result['file'];
			$copyFile = $this->dir.$result['file'];
			copy($sourceFile, $copyFile);
		}

		$width = $this->getWidth($this->dir.$result['file']);
		$height = $this->getHeight($this->dir.$result['file']);
		if ($width > $max_width){
			$scale = $max_width/$width;
			$this->resizeImage($this->dir.$result['file'],$width,$height,$scale);
		}else{
			$scale = 1;
			$this->resizeImage($this->dir.$result['file'],$width,$height,$scale);
		}
		if (empty($thumb_height) && !empty($thumb_width)) {
			$this->resizeImage2('resize', $result['file'], $this->dir, 'thumb_'.$result['file'], $thumb_width, FALSE, $thumb_quality);
		}elseif (empty($thumb_width) && !empty($thumb_height)) {
			$this->resizeImage2('resize', $result['file'], $this->dir, 'thumb_'.$result['file'], FALSE, $thumb_height, $thumb_quality);
		}else{
			$this->resizeImage2('resizeCrop', $result['file'], $this->dir, 'thumb_'.$result['file'], $thumb_width, $thumb_height, $thumb_quality);
		}

		$data['Photo']['small'] = $this->albumDir . 'thumb_'.$result['file'];
		$data['Photo']['large'] = $this->albumDir . $result['file'];
		return $data;
	}


	function getHeight($image) {
		$sizes = getimagesize($image);
		return $sizes[1];
	}

	function getWidth($image) {
		$sizes = getimagesize($image);
		return $sizes[0];
	}

	function resizeImage($image,$width,$height,$scale) {
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		$ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
		$source = "";
		if($ext == "png"){
			$source = imagecreatefrompng($image);
		}elseif($ext == "jpg" || $ext == "jpeg"){
			$source = imagecreatefromjpeg($image);
		}elseif($ext == "gif"){
			$source = imagecreatefromgif($image);
		}
		imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
		imagejpeg($newImage,$image,Configure::read('Gallery.quality'));
		chmod($image, 0777);
	}

	function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
		$newImageWidth = ceil($width * $scale);
		$newImageHeight = ceil($height * $scale);
		$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
		$ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
		$source = "";
		if($ext == "png"){
			$source = imagecreatefrompng($image);
		}elseif($ext == "jpg" || $ext == "jpeg"){
			$source = imagecreatefromjpeg($image);
{'fraction_non_alphanumeric': 0.21463734821135544, 'fraction_numerical': 0.0051416694015972, 'mean_word_length': 4.982984293193717, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '572387', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3192, 'n_words': 545}