Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* --------------------------------------------------------------------
- Chevereto
- http://chevereto.com/
- @author Rodolfo Berrios A. <http://rodolfoberrios.com/>
- <inbox@rodolfoberrios.com>
- Copyright (C) Rodolfo Berrios A. All rights reserved.
- BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
- http://chevereto.com/license
- --------------------------------------------------------------------- */
- namespace CHV;
- use G, Exception;
- class Image {
- static $table_chv_image = [
- 'name',
- 'extension',
- 'album_id',
- 'size',
- 'width',
- 'height',
- 'date',
- 'date_gmt',
- 'nsfw',
- 'user_id',
- 'uploader_ip',
- 'storage',
- 'storage_id',
- 'md5',
- 'original_filename',
- 'original_exifdata',
- 'category_id',
- 'description'
- ];
- public static function getSingle($id, $sumview=false, $pretty=true) {
- $tables = DB::getTables();
- $query = 'SELECT * FROM '.$tables['images']."\n";
- $joins = array(
- 'LEFT JOIN '.$tables['users'].' ON '.$tables['images'].'.image_user_id = '.$tables['users'].'.user_id',
- 'LEFT JOIN '.$tables['albums'].' ON '.$tables['images'].'.image_album_id = '.$tables['albums'].'.album_id'
- );
- $query .= implode("\n", $joins) . "\n";
- $query .= 'WHERE image_id=:image_id;'."\n";
- if($sumview === true) {
- $query .= 'UPDATE '.$tables['images'].' SET image_views = image_views + 1 WHERE image_id=:image_id';
- }
- try {
- $db = DB::getInstance();
- $db->query($query);
- $db->bind(':image_id', $id);
- $image_db = $db->fetchSingle();
- if($image_db) {
- if($sumview === true and $image_db['image_views'] == 0) {
- $image_db['image_views'] = 1;
- }
- $return = array_merge($image_db, self::getSrcTargetSingle($image_db, true));
- return $pretty ? self::formatArray($return) : $return;
- } else {
- return $image_db;
- }
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function getMultiple($ids, $pretty=false) {
- if(!is_array($ids)) {
- $ids = func_get_args();
- $aux = array();
- foreach($ids as $k => $v) {
- $aux[] = $v;
- }
- $ids = $aux;
- }
- if(count($ids) == 0) {
- throw new ImageException('Null ids provided in Image::get_multiple', 100);
- }
- $tables = DB::getTables();
- $query = 'SELECT * FROM '.$tables['images']."\n";
- $joins = array(
- 'LEFT JOIN '.$tables['users'].' ON '.$tables['images'].'.image_user_id = '.$tables['users'].'.user_id',
- 'LEFT JOIN '.$tables['albums'].' ON '.$tables['images'].'.image_album_id = '.$tables['albums'].'.album_id'
- );
- $query .= implode("\n", $joins) . "\n";
- $query .= 'WHERE image_id IN ('. join(',', $ids). ')' . "\n";
- try {
- $db = DB::getInstance();
- $db->query($query);
- $images_db = $db->fetchAll();
- if($images_db) {
- foreach($images_db as $k => $v) {
- $images_db[$k] = array_merge($v, self::getSrcTargetSingle($v, true));
- }
- }
- if($pretty) {
- $return = [];
- foreach($images_db as $k => $v) {
- $return[] = self::formatArray($v);
- }
- return $return;
- }
- return $images_db;
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function getAlbumSlice($image_id, $album_id=NULL, $padding=2) {
- $tables = DB::getTables();
- if($image_id == NULL) {
- throw new ImageException("Image id can't be NULL", 100);
- }
- if($album_id == NULL) {
- try {
- $db = DB::getInstance();
- $db->query('SELECT image_album_id FROM '.$tables['images'].' WHERE image_id=:image_id');
- $db->bind(':image_id', $image_id);
- $image_album_db = $db->fetchSingle();
- $album_id = $image_album_db['image_album_id'];
- } catch(Excepton $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- if($album_id == NULL) {
- return;
- }
- }
- if(!is_numeric($padding)) {
- $padding = 2;
- }
- //$where_album = $album_id !== NULL ? "image_album_id=:image_album_id" : "image_album_id IS NULL";
- try {
- $db = DB::getInstance();
- $db->query('SELECT * FROM (
- (SELECT * FROM '.$tables['images'].' WHERE image_album_id=:image_album_id AND image_id <= :image_id ORDER BY image_id DESC LIMIT 0,'.($padding*2 + 1).')
- UNION
- (SELECT * FROM '.$tables['images'].' WHERE image_album_id=:image_album_id AND image_id > :image_id ORDER BY image_id ASC LIMIT 0,'.($padding*2).')
- ) images ORDER BY images.image_id ASC');
- $db->bind(':image_album_id', $album_id);
- $db->bind(':image_id', $image_id);
- $image_album_slice_db = $db->fetchAll();
- $album_offset = array('top' => 0, 'bottom' => 0);
- foreach($image_album_slice_db as $v) {
- if($image_id > $v['image_id']) {
- $album_offset['top']++;
- }
- if($image_id < $v['image_id']) {
- $album_offset['bottom']++;
- }
- }
- $album_chop_count = count($image_album_slice_db);
- $album_iteration_times = $album_chop_count - ($padding*2 + 1);
- if($album_chop_count > ($padding*2 + 1)) {
- if($album_offset['top'] > $padding && $album_offset['bottom'] > $padding) {
- // Cut on top
- for($i=0; $i<$album_offset['top']-$padding; $i++) {
- unset($image_album_slice_db[$i]);
- }
- // Cut on bottom
- for($i=1; $i<=$album_offset['bottom']-$padding; $i++) {
- unset($image_album_slice_db[$album_chop_count - $i]);
- }
- } else if($album_offset['top'] <= $padding) {
- // Cut bottom
- for($i=0; $i<$album_iteration_times; $i++) {
- unset($image_album_slice_db[$album_chop_count - 1 - $i]);
- }
- } else if($album_offset['bottom'] <= $padding) {
- // Cut top
- for($i=0; $i<$album_iteration_times; $i++) {
- unset($image_album_slice_db[$i]);
- }
- }
- // Some cleaning after the unsets
- $image_album_slice_db = array_values($image_album_slice_db);
- }
- $album_cursor = '';
- foreach($image_album_slice_db as $k => $v) {
- if($v['image_id'] == $image_id) {
- $album_cursor = $k;
- break;
- }
- }
- $image_album_slice['images'] = array();
- foreach($image_album_slice_db as $k => $v) {
- $image_album_slice['images'][$k] = self::formatArray($v);
- }
- if($image_album_slice['images'][$album_cursor-1]) {
- $image_album_slice['prev'] = $image_album_slice['images'][$album_cursor-1];
- }
- if($image_album_slice['images'][$album_cursor+1]) {
- $image_album_slice['next'] = $image_album_slice['images'][$album_cursor+1];
- }
- return array(
- 'db' => $image_album_slice_db,
- 'formatted' => $image_album_slice
- );
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function getCategorySlice($image_id, $cat_id=NULL, $padding=2) {
- $tables = DB::getTables();
- if($image_id == NULL) {
- throw new ImageException("Image id can't be NULL", 100);
- }
- if($cat_id == NULL) {
- try {
- $db = DB::getInstance();
- $db->query('SELECT image_category_id FROM '.$tables['images'].' WHERE image_id=:image_id');
- $db->bind(':image_id', $image_id);
- $image_album_db = $db->fetchSingle();
- $cat_id = $image_album_db['image_category_id'];
- } catch(Excepton $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- if($cat_id == NULL) {
- return;
- }
- }
- if(!is_numeric($padding)) {
- $padding = 2;
- }
- //$where_album = $album_id !== NULL ? "image_album_id=:image_album_id" : "image_album_id IS NULL";
- try {
- $db = DB::getInstance();
- $db->query('SELECT * FROM (
- (SELECT * FROM '.$tables['images'].' WHERE image_category_id=:image_category_id AND image_id <= :image_id ORDER BY image_id DESC LIMIT 0,'.($padding*2 + 1).')
- UNION
- (SELECT * FROM '.$tables['images'].' WHERE image_category_id=:image_category_id AND image_id > :image_id ORDER BY image_id ASC LIMIT 0,'.($padding*2).')
- ) images ORDER BY images.image_id ASC');
- $db->bind(':image_category_id', $cat_id);
- $db->bind(':image_id', $image_id);
- $image_album_slice_db = $db->fetchAll();
- $album_offset = array('top' => 0, 'bottom' => 0);
- foreach($image_album_slice_db as $v) {
- if($image_id > $v['image_id']) {
- $album_offset['top']++;
- }
- if($image_id < $v['image_id']) {
- $album_offset['bottom']++;
- }
- }
- $album_chop_count = count($image_album_slice_db);
- $album_iteration_times = $album_chop_count - ($padding*2 + 1);
- if($album_chop_count > ($padding*2 + 1)) {
- if($album_offset['top'] > $padding && $album_offset['bottom'] > $padding) {
- // Cut on top
- for($i=0; $i<$album_offset['top']-$padding; $i++) {
- unset($image_album_slice_db[$i]);
- }
- // Cut on bottom
- for($i=1; $i<=$album_offset['bottom']-$padding; $i++) {
- unset($image_album_slice_db[$album_chop_count - $i]);
- }
- } else if($album_offset['top'] <= $padding) {
- // Cut bottom
- for($i=0; $i<$album_iteration_times; $i++) {
- unset($image_album_slice_db[$album_chop_count - 1 - $i]);
- }
- } else if($album_offset['bottom'] <= $padding) {
- // Cut top
- for($i=0; $i<$album_iteration_times; $i++) {
- unset($image_album_slice_db[$i]);
- }
- }
- // Some cleaning after the unsets
- $image_album_slice_db = array_values($image_album_slice_db);
- }
- $album_cursor = '';
- foreach($image_album_slice_db as $k => $v) {
- if($v['image_id'] == $image_id) {
- $album_cursor = $k;
- break;
- }
- }
- $image_album_slice['images'] = array();
- foreach($image_album_slice_db as $k => $v) {
- $image_album_slice['images'][$k] = self::formatArray($v);
- }
- if($image_album_slice['images'][$album_cursor-1]) {
- $image_album_slice['prev'] = $image_album_slice['images'][$album_cursor-1];
- }
- if($image_album_slice['images'][$album_cursor+1]) {
- $image_album_slice['next'] = $image_album_slice['images'][$album_cursor+1];
- }
- return array(
- 'db' => $image_album_slice_db,
- 'formatted' => $image_album_slice
- );
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function getSrcTargetSingle($filearray, $prefix=true) {
- $prefix = $prefix ? 'image_' : '';
- $folder = CHV_PATH_IMAGES;
- switch($filearray[$prefix.'storage']) {
- case 'datefolder':
- $folder .= date('Y/m/d/', strtotime($filearray[$prefix.'date'])); // sqlite no strtotime!
- break;
- case 'old':
- $folder .= 'old/';
- break;
- case 'direct':
- // use direct $folder
- break;
- }
- $targets = array(
- $prefix.'path' => $folder.$filearray[$prefix.'name'] . '.' . $filearray[$prefix.'extension'],
- $prefix.'path_thumb' => $folder.$filearray[$prefix.'name'].'.th.'.$filearray[$prefix.'extension'],
- $prefix.'path_medium' => $folder.$filearray[$prefix.'name'].'.md.'.$filearray[$prefix.'extension'],
- $prefix.'path_large' => $folder.$filearray[$prefix.'name'].'.la.'.$filearray[$prefix.'extension']
- );
- $return = array();
- foreach($targets as $k => $v) {
- if(!file_exists($v)) continue;
- $return[$k] = $v;
- }
- return $return;
- }
- public static function getUrlViewer($id_encoded) {
- return G\get_base_url('image/'.$id_encoded);
- }
- public static function upload($source, $destination, $filename='', $options=array()) {
- $default_options = array(
- 'max_size' => G\get_bytes('2 MB'),
- 'filenaming' => 'original'
- );
- foreach($options as $k => $v) {
- $upload_options[$k] = $v;
- }
- if($filename !== '' and !$upload_options['filenaming']) {
- $upload_options['filenaming'] = 'original';
- }
- try {
- $upload = new Upload;
- $upload->setSource($source);
- $upload->setDestination($destination);
- $upload->setOptions($upload_options);
- if($filename !== '') {
- $upload->setFilename($filename);
- }
- $upload->exec();
- // Watermark
- if($options['watermark']) {
- $watermark_postion = explode(' ', get_chv_setting('watermark_position'));
- $watermark_file = CHV_PATH_CONTENT_IMAGES_SYSTEM . get_chv_setting('watermark_image');
- if(!is_readable($watermark_file)) {
- throw new Exception("Can't read watermark file", 100);
- }
- switch($upload->uploaded['fileinfo']['extension']) {
- case 'gif':
- $src = imagecreatefromgif($upload->uploaded['file']);
- break;
- case 'png':
- $src = imagecreatefrompng($upload->uploaded['file']);
- break;
- case 'jpg':
- $src = imagecreatefromjpeg($upload->uploaded['file']);
- break;
- }
- $src_width = imagesx($src);
- $src_height = imagesy($src);
- $watermark_src = imagecreatefrompng($watermark_file);
- $watermark_width = imagesx($watermark_src);
- $watermark_height = imagesy($watermark_src);
- // Calculate the position
- switch($watermark_postion[0]) {
- case 'left':
- $watermark_x = get_chv_setting('watermark_margin');
- break;
- case 'center':
- $watermark_x = $src_width/2 - $watermark_width/2;
- break;
- case 'right':
- $watermark_x = $src_width - $watermark_width - get_chv_setting('watermark_margin');
- break;
- }
- switch($watermark_postion[1]) {
- case 'top':
- $watermark_y = get_chv_setting('watermark_margin');
- break;
- case 'center':
- $watermark_y = $src_height/2 - $watermark_height/2;
- break;
- case 'bottom':
- $watermark_y = $src_height - $watermark_height - get_chv_setting('watermark_margin');
- break;
- }
- // Watermark has the same or greater size of the image ?
- // --> Center the watermark
- if($watermark_width == $src_width && $watermark_height == $src_height) {
- $watermark_x = $src_width/2 - $watermark_width/2;
- $watermark_y = $src_height/2 - $watermark_height/2;
- }
- // Watermark is too big ?
- // --> Fit the watermark on the image
- if($watermark_width > $src_width || $watermark_height > $src_height) {
- // Watermark is wider than the image
- if($watermark_width > $src_width) {
- $watermark_new_width = $src_width;
- $watermark_new_height = $src_width * $watermark_height / $watermark_width;
- if($watermark_new_height > $src_height) {
- $watermark_new_width = $src_height * $watermark_width / $watermark_height;
- $watermark_new_height = $src_height;
- }
- } else {
- $watermark_new_width = $src_height * $watermark_width / $watermark_height;
- $watermark_new_height = $src_height;
- }
- try {
- $watermark_temp = @tempnam(sys_get_temp_dir(), 'chvtemp');
- self::resize($watermark_file, $watermark_temp, ['width' => $watermark_new_width]);
- $watermark_width = $watermark_new_width;
- $watermark_height = $watermark_new_height;
- $watermark_src = imagecreatefrompng($watermark_temp);
- $watermark_x = $src_width/2 - $watermark_width/2;
- $watermark_y = $src_height/2 - $watermark_height/2;
- } catch(Exception $e) {} // Silence
- }
- // Apply and save the watermark
- G\imagecopymerge_alpha($src, $watermark_src, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height, get_chv_setting('watermark_opacity'), $upload->uploaded['fileinfo']['extension']);
- switch($upload->uploaded['fileinfo']['extension']) {
- case 'gif':
- imagegif($src, $upload->uploaded['file']);
- break;
- case 'png':
- imagepng($src, $upload->uploaded['file']);
- break;
- case 'jpg':
- imagejpeg($src, $upload->uploaded['file'], 90);
- break;
- }
- imagedestroy($src);
- @unlink($watermark_temp);
- // Remake the fileinfo array
- $upload->uploaded['fileinfo'] = get_image_fileinfo($upload->uploaded['file']);
- }
- return array(
- 'uploaded' => $upload->uploaded,
- 'source' => $upload->source
- );
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), $e->getCode());
- }
- }
- public static function uploadToWebsite($source, $user, $params=[]) {
- try {
- $storage = get_chv_setting('upload_storage_mode');
- switch($storage) {
- case "direct":
- $upload_path = CHV_PATH_IMAGES;
- break;
- case "datefolder":
- $upload_path = CHV_PATH_IMAGES . date("Y/m/d/");
- break;
- }
- $upload_options = array(
- 'max_size' => G\get_bytes(get_chv_setting('upload_max_filesize_mb') . ' MB'),
- 'filenaming' => in_array($params['privacy'], ['private', 'private_but_link']) ? 'random' : get_chv_setting('upload_filenaming'),
- 'watermark' => get_chv_setting('watermark_enable')
- );
- $image_upload = self::upload($source, $upload_path, NULL, $upload_options);
- // Try to generate the thumb
- $image_thumb_options = array(
- 'width' => get_chv_setting('upload_thumb_width'),
- 'height' => get_chv_setting('upload_thumb_height')
- );
- $image_thumb = self::resize($image_upload['uploaded']['file'], $upload_path, $image_upload['uploaded']['name'] . '.th', $image_thumb_options);
- // Try to generate the medium
- $medium_size = get_chv_setting('upload_medium_width');
- $is_animated_image = $image_upload['uploaded']['fileinfo']['extension'] == 'gif' and G\is_animated_image($image_upload['uploaded']['file']);
- if($image_upload['uploaded']['fileinfo']['width'] > $medium_size or $image_upload['uploaded']['fileinfo']['height'] > $medium_size or $is_animated_image) {
- $image_medium_options = [];
- /*
- if($image_upload['uploaded']['fileinfo']['ratio'] > 1) {
- $image_medium_options['width'] = $medium_size;
- } else {
- $image_medium_options['height'] = $medium_size;
- }
- */
- $image_medium_options['width'] = $medium_size;
- if($is_animated_image) {
- $image_medium_options['forced'] = true;
- }
- $image_large_options = array(
- 'width' => 2048,
- 'forced' => true
- );
- $image_medium = self::resize($image_upload['uploaded']['file'], $upload_path, $image_upload['uploaded']['name'] . '.md', $image_medium_options);
- }
- if($image_upload['uploaded']['fileinfo']['width'] > 2048 or $image_upload['uploaded']['fileinfo']['height'] > 2048 or $is_animated_image) {
- $image_large_options = array(
- 'width' => 2048,
- 'forced' => true
- );
- $image_large = self::resize($image_upload['uploaded']['file'], $upload_path, $image_upload['uploaded']['name'] . '.la', $image_large_options);
- }
- $image_insert_values = array(
- 'storage' => $storage,
- 'storage_id' => NULL,
- 'user_id' => $user['id'],
- 'album_id' => NULL,
- 'nsfw' => $params['nsfw'],
- 'category_id' => $params['category_id']
- );
- $uploaded_id = self::insert($image_upload, $image_insert_values);
- $user_updated_counts = [
- 'image_count' => $user['image_count'] + 1
- ];
- // Private upload? Create a private album then
- if(in_array($params['privacy'], ['private', 'private_but_link'])) {
- $upload_timestamp = $params['timestamp'];
- if(!$_SESSION['upload_'.$upload_timestamp]) {
- $inserted_album = Album::insert(_s('Private upload').' '.G\datetime('Y-m-d'), $user['id'], $params['privacy']);
- $_SESSION['upload_'.$upload_timestamp] = encodeID($inserted_album);
- }
- Album::addImage(decodeID($_SESSION['upload_'.$upload_timestamp]), $uploaded_id);
- }
- // Update user (if any)
- if($user) {
- User::update($user['id'], $user_updated_counts);
- }
- return $uploaded_id;
- } catch(Exception $e) {
- //@unlink($image_upload['uploaded']['file'], $image_medium['file'], $image_thumb['file']);
- //@unlink($image_upload['uploaded']['file'], $image_large['file'], $image_thumb['file']);
- throw new ImageException($e->getMessage(), $e->getCode());
- }
- }
- public static function resize($source, $destination, $filename='', $options=array()) {
- try {
- $resize = new Imageresize;
- $resize->setSource($source);
- $resize->setDestination($destination);
- if($filename) {
- $resize->setFilename($filename);
- }
- $resize->setOptions($options);
- if($options['width']) {
- $resize->set_width($options['width']);
- }
- if($options['height']) {
- $resize->set_height($options['height']);
- }
- if($options['width'] == $options['height']) {
- $resize->set_fixed();
- }
- if($options['forced']) {
- $resize->setOption('forced', true);
- }
- $resize->exec();
- return $resize->resized;
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), $e->getCode());
- }
- }
- public static function insert($image_upload, $values=[]) {
- try {
- $table_chv_image = self::$table_chv_image;
- foreach($table_chv_image as $k => $v) {
- $table_chv_image[$k] = 'image_' . $v;
- }
- $values = array(
- 'album_id' => $values['album_id'],
- 'date' => G\datetime(),
- 'date_gmt' => G\datetimegmt(),
- 'nsfw' => isset($values['nsfw']) ? $values['nsfw'] : 0,
- 'user_id' => $values['user_id'],
- 'uploader_ip' => G\get_client_ip(),
- 'storage' => $values['storage'],
- 'storage_id' => $values['storage_id'],
- 'md5' => md5_file($image_upload['uploaded']['file']),
- 'original_filename' => $image_upload['source']['filename'],
- 'original_exifdata' => (G\check_value($image_upload['source']['image_exif']) ? json_encode($image_upload['source']['image_exif']) : null),
- 'category_id' => $values['category_id'],
- 'description' => $values['description']
- );
- // Populate the info from fileinfo
- $values = array_merge($image_upload['uploaded']['fileinfo'], $values);
- foreach(['description', 'category_id'] as $v) {
- G\nullify_string($values[$v]);
- }
- // Now use only the values accepted by the table
- foreach($values as $k => $v) {
- if(!in_array('image_' . $k, $table_chv_image)) {
- unset($values[$k]);
- }
- }
- $insert = DB::insert('images', $values);
- if(!is_null($values['album_id']) and $insert) {
- Album::updateImageCount($values['album_id'], 1);
- }
- return $insert;
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function update($id, $values) {
- try {
- $values = G\array_filter_array($values, self::$table_chv_image, 'exclusion');
- foreach(['description', 'category_id'] as $v) {
- G\nullify_string($values[$v]);
- }
- if(isset($values['album_id'])) {
- $image_db = self::getSingle($id, false, false);
- $old_album = $image_db['image_album_id'];
- $new_album = $values['album_id'];
- $update = DB::update('images', $values, ['id' => $id]);
- if($update and $old_album !== $new_album) {
- if(!is_null($old_album)) { // Update the old album
- Album::updateImageCount($old_album, 1, '-');
- }
- if(!is_null($new_album)) { // Update the new album
- Album::updateImageCount($new_album, 1);
- }
- }
- return $update;
- } else {
- return DB::update('images', $values, ['id' => $id]);
- }
- } catch(Excepton $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function delete($id, $update_user=true) {
- try {
- if($update_user) {
- // Get the user id
- $user_id = DB::get('images', ['id' => $id])[0]['image_user_id'];
- }
- $image_db = self::getSingle($id, false, false);
- $file_deletes = array('image_path', 'image_path_thumb', 'image_path_medium', 'image_path_large');
- foreach($file_deletes as $file_delete) {
- $target = $image_db[$file_delete];
- if(file_exists($target) and !unlink($target)) {
- throw new ImageException("Can't delete file", 200);
- }
- }
- if($update_user and $user_id) {
- $user = User::getSingle($user_id, 'id');
- User::update($user_id, ['image_count' => $user['image_count'] - 1]);
- }
- // Update album count
- if($image_db['image_album_id'] > 0) {
- Album::updateImageCount($image_db['image_album_id'], 1, '-');
- }
- return DB::delete('images', ['id' => $id]);
- } catch(Exception $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function deleteMultiple($ids) {
- if(!is_array($ids)) {
- throw new ImageException('Expecting array argument, '.gettype($ids).' given in '. __METHOD__, 100);
- }
- try {
- foreach($ids as $id) {
- self::delete($id);
- }
- return true;
- } catch(Excepton $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- }
- public static function fill(&$image) {
- $image['id_encoded'] = encodeID($image['id']);
- $targets = self::getSrcTargetSingle($image, false);
- if(count($targets) > 0) {
- $image = array_merge($image, (array) get_image_fileinfo($targets['path'])); // Never do an array merge over an empty thing!
- }
- $image['file_resource'] = $targets ? [
- 'type' => 'path',
- 'image' => $targets['path'],
- 'thumb' => $targets['path_thumb'],
- 'medium'=> $targets['path_medium'],
- 'large'=> $targets['path_large']
- ] : false;
- $image['url_viewer'] = self::getUrlViewer($image['id_encoded']);
- $image['thumb'] = file_exists($targets['path_thumb']) ? get_image_fileinfo($targets['path_thumb']) : null;
- $image['medium'] = file_exists($targets['path_medium']) ? get_image_fileinfo($targets['path_medium']) : null;
- $image['large'] = file_exists($targets['path_large']) ? get_image_fileinfo($targets['path_large']) : null;
- $image['views_label'] = _n('view', 'views', $image['views']);
- $image['display_url'] = $image['medium'] == null ? $image['url'] : $image['medium']['url'];
- $image['how_long_ago'] = time_elapsed_string($image['date_gmt']);
- }
- public static function formatArray($dbrow, $safe=false) {
- try {
- $output = DB::formatRow($dbrow);
- } catch(Excepton $e) {
- throw new ImageException($e->getMessage(), 400);
- }
- if(!is_null($output['user']['id'])) {
- User::fill($output['user']);
- } else {
- unset($output['user']);
- }
- if(!is_null($output['album']['id']) or !is_null($output['user']['id'])) {
- Album::fill($output['album'], $output['user']);
- } else {
- unset($output['album']);
- }
- self::fill($output);
- if($safe === true) {
- unset($output['id'], $output['path'], $output['uploader_ip']);
- unset($output['album']['id'], $output['album']['privacy_extra']);
- unset($output['user']['id']);
- unset($output['path'], $output['path_thumb'], $output['path_medium']);
- if($output['file_resource']['type'] == 'path') {
- unset($output['file_resource']);
- }
- }
- return $output;
- }
- }
- class ImageException extends Exception {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement