Created
November 19, 2012 22:15
-
-
Save adrinavarro/4114395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* File manipulation class */ | |
class ProjectFiles { | |
function __construct($_parent) { | |
$this->_parent = $_parent; | |
} | |
function dir_contents($dir) { | |
$handler = opendir($dir); | |
while($file = readdir($handler)) { | |
if($file != '.' && $file != '..') | |
$return[] = $file; | |
} | |
return $return; | |
} | |
function file_path($file, $version) { | |
return PATH.'files/s'.$file['server'].'/'. $file['hash'] . '_' . $version . '.' . $file['ext']; | |
} | |
function create_default_version($file) { | |
$this->create_file_version($this->file_path($file, 750), $file['hash'], 280); | |
} | |
function rotate_file($pid, $aid, $count) { | |
$count = $count*90; | |
if($count >= 360) { | |
$count = $count - (floor($count/360)*360); | |
} | |
$info = $this->_parent->queries->get_photo_info($pid); | |
$path = $this->file_path($info, 750); | |
$alb = $this->file_path($info, 280); | |
$imagick = new Imagick(); | |
$imagick->readImage($path); | |
$imagick->stripImage(); | |
$imagick->rotateImage('white', $count); | |
$imagick->writeImage($path); | |
$imagick->clear(); | |
$imagick->destroy(); | |
$this->create_file_version($path, $info['hash'], '180', true); | |
$this->create_file_version($path, $info['hash'], '750', true); | |
if(file_exists($alb)) $this->create_file_version($path, $info['hash'], '280', true); | |
return $this->_parent->queries->update_pic_ts($pid, time()); | |
} | |
function remove_file($file, $is_avatar = false) { | |
$avatar_sizes = array(20, 40, 130); | |
$file_sizes = array(280, 180, 750); | |
$flag = true; | |
if($is_avatar) { | |
$sarray = $avatar_sizes; | |
list($tmp['hash'], $tmp['server'], $tmp['ext']) = explode("@", $file); | |
$file = $tmp; | |
} else { | |
$sarray = $file_sizes; | |
} | |
foreach($sarray as $size) { | |
if(file_exists(PATH.'files/s'.$file['server'].'/'. $file['hash'] . '_' . $size . '.' . $file['ext'])) { | |
$dels = unlink(PATH.'files/s'.$file['server'].'/'. $file['hash'] . '_' . $size . '.' . $file['ext']); | |
if(!$dels and $flag) $flag = false; | |
} | |
} | |
return $flag; | |
} | |
function create_file_version($path, $hash, $version, $force = false) { | |
$writepath = PATH.'files/s'.SERVER.'/'. $hash . '_' . $version . '.jpg'; | |
if(file_exists($writepath) and !$force) return true; | |
$crop = new Imagick($path); | |
list($w, $h, $type) = getimagesize($path, $imginfo); | |
$crop->setBackgroundColor("white"); | |
$crop->flattenImages(); | |
$crop->stripImage(); | |
$crop->setImageFormat('jpeg'); | |
$crop->setCompressionQuality(90); | |
switch($version) { | |
case '280': | |
$crop->cropThumbnailImage(280, 160); | |
break; | |
case '180': | |
$crop->cropThumbnailImage(180, 110); | |
break; | |
case '750': | |
if($w > 810 or $h > 760) $crop->scaleImage(810, 760, true); | |
break; | |
} | |
$crop->writeImage($writepath); | |
$crop->clear(); | |
$crop->destroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment