View source
<?php
namespace Drupal\photos;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
class PhotosImage {
protected $fid;
public function __construct($fid) {
$this->fid = $fid;
}
public function load() {
$fid = $this->fid;
$db = \Drupal::database();
$image = $db
->query('SELECT f.fid, f.uri, f.filemime, f.created, f.filename, n.title as node_title, a.data, u.uid, u.name, p.*
FROM {file_managed} f
INNER JOIN {photos_image__field_image} i ON i.field_image_target_id = f.fid
INNER JOIN {photos_image_field_data} p ON p.revision_id = i.revision_id
INNER JOIN {node_field_data} n ON p.album_id = n.nid
INNER JOIN {photos_album} a ON a.album_id = n.nid
INNER JOIN {users_field_data} u ON f.uid = u.uid
WHERE i.field_image_target_id = :fid', [
':fid' => $fid,
])
->fetchObject();
if (!isset($image->height) && isset($image->uri)) {
$image_info = \Drupal::service('image.factory')
->get($image->uri);
if ($image_info
->isValid()) {
$image->width = $image_info
->getWidth();
$image->height = $image_info
->getHeight();
}
else {
$image->width = $image->height = NULL;
}
}
return $image;
}
public function view($style_name = NULL, array $variables = []) {
$image = $this
->load();
if (isset($variables['title'])) {
$image->title = $variables['title'];
}
if (!$style_name) {
$image_sizes = \Drupal::config('photos.settings')
->get('photos_size');
$style_name = key($image_sizes);
}
if (!$style_name) {
$style_name = 'thumbnail';
}
$scheme = \Drupal::service('stream_wrapper_manager')
->getScheme($image->uri);
$uri = $image->uri;
if ($scheme == 'private') {
$photos_image = new PhotosImage($image->fid);
$url = $photos_image
->derivative($uri, $style_name, $scheme);
}
else {
$style = ImageStyle::load($style_name);
$url = $style
->buildUrl($uri);
}
$title = isset($image->title) ? $image->title : '';
$alt = isset($image->alt) ? $image->alt : $title;
$image_render_array = [
'#theme' => 'image',
'#uri' => $url,
'#alt' => $alt,
'#title' => $title,
];
if (isset($variables['href'])) {
$image_render_array = [
'#type' => 'link',
'#title' => $image_render_array,
'#url' => Url::fromUri('base:' . $variables['href']),
];
}
return $image_render_array;
}
public function derivative($uri, $style_name, $scheme = 'private') {
$style = ImageStyle::load($style_name);
$pathInfo = pathinfo($uri);
$ext = strtolower($pathInfo['extension']);
$destination = $scheme . '://photos/tmp_images/' . $style_name . '/image_' . $this->fid . '.' . $ext;
$style
->createDerivative($uri, $destination);
return file_create_url($destination);
}
public function url($uri, $style_name = 'thumbnail') {
if ($style_name == 'original') {
$image_styles = image_style_options(FALSE);
if (isset($image_styles['photos_original'])) {
$image_url = ImageStyle::load($style_name)
->buildUrl($uri);
}
else {
$image_url = file_create_url($uri);
}
}
else {
$image_url = ImageStyle::load($style_name)
->buildUrl($uri);
}
return $image_url;
}
public function delete($filepath = NULL, $count = FALSE) {
$fid = $this->fid;
if (!$filepath) {
if ($count) {
$file = File::load($fid);
$db = \Drupal::database();
$file->album_id = $db
->select('photos_image', 'p')
->fields('p', [
'album_id',
])
->condition('fid', $fid)
->execute()
->fetchField();
$filepath = $file
->getFileUri();
}
else {
$db = \Drupal::database();
$filepath = $db
->query('SELECT uri FROM {file_managed} WHERE fid = :fid', [
':fid' => $fid,
])
->fetchField();
}
}
if ($filepath) {
if (\Drupal::config('photos.settings')
->get('photos_access_photos')) {
$file_scheme = \Drupal::service('stream_wrapper_manager')
->getScheme($filepath);
if ($file_scheme == 'private') {
$pathinfo = pathinfo($filepath);
$ext = strtolower($pathinfo['extension']);
$basename = 'image_' . $fid . '.' . $ext;
$file_uris = \Drupal::service('file_system')
->scanDirectory('private://photos/tmp_images', '~\\b' . $basename . '\\b~');
foreach ($file_uris as $uri => $data) {
\Drupal::service('file_system')
->delete($uri);
}
}
}
$db = \Drupal::database();
$db
->delete('photos_image')
->condition('fid', $fid)
->execute();
$db
->delete('photos_comment')
->condition('fid', $fid)
->execute();
if ($count) {
PhotosAlbum::setCount('node_album', $file->album_id);
PhotosAlbum::setCount('user_image', $file
->getOwnerId());
}
if (empty($file)) {
$file = File::load($fid);
}
if (empty($file->album_id)) {
$db = \Drupal::database();
$file->album_id = $db
->select('photos_image', 'p')
->fields('p', [
'album_id',
])
->condition('fid', $file
->id())
->execute()
->fetchField();
}
$file_usage = \Drupal::service('file.usage');
$file_usage
->delete($file, 'photos', 'node', $file->album_id);
$file
->delete();
Cache::invalidateTags([
'photos:image:' . $fid,
]);
return TRUE;
}
else {
return FALSE;
}
}
}