2007-12-06 02:26:34 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class PixelFileHandler extends DataHandlerExtension
|
|
|
|
{
|
2019-06-14 17:34:53 +00:00
|
|
|
const SUPPORTED_EXTENSIONS = ["jpg", "jpeg", "gif", "png", "webp"];
|
|
|
|
|
2019-06-25 20:17:13 +00:00
|
|
|
|
|
|
|
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event)
|
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
if (in_array($event->ext, Media::LOSSLESS_FORMATS)) {
|
2019-06-25 20:17:13 +00:00
|
|
|
$event->lossless = true;
|
2019-09-29 13:30:55 +00:00
|
|
|
} elseif ($event->ext=="webp") {
|
2019-06-25 20:17:13 +00:00
|
|
|
$event->lossless = Media::is_lossless_webp($event->file_name);
|
|
|
|
}
|
|
|
|
|
2019-09-29 13:30:55 +00:00
|
|
|
if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) {
|
|
|
|
if ($event->lossless==null) {
|
2019-06-25 20:17:13 +00:00
|
|
|
$event->lossless = false;
|
|
|
|
}
|
|
|
|
$event->audio = false;
|
|
|
|
switch ($event->ext) {
|
|
|
|
case "gif":
|
|
|
|
$event->video = Media::is_animated_gif($event->file_name);
|
|
|
|
break;
|
|
|
|
case "webp":
|
|
|
|
$event->video = Media::is_animated_webp($event->file_name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$event->video = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-16 14:40:42 +00:00
|
|
|
$event->image = !$event->video;
|
2019-06-25 20:17:13 +00:00
|
|
|
|
|
|
|
$info = getimagesize($event->file_name);
|
|
|
|
if (!$info) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$event->width = $info[0];
|
|
|
|
$event->height = $info[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
protected function supported_ext(string $ext): bool
|
|
|
|
{
|
|
|
|
$ext = (($pos = strpos($ext, '?')) !== false) ? substr($ext, 0, $pos) : $ext;
|
2019-06-14 17:34:53 +00:00
|
|
|
return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function create_image_from_data(string $filename, array $metadata)
|
|
|
|
{
|
|
|
|
$image = new Image();
|
|
|
|
|
|
|
|
$image->filesize = $metadata['size'];
|
|
|
|
$image->hash = $metadata['hash'];
|
|
|
|
$image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename'];
|
|
|
|
$image->ext = (($pos = strpos($metadata['extension'], '?')) !== false) ? substr($metadata['extension'], 0, $pos) : $metadata['extension'];
|
|
|
|
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
|
|
|
|
$image->source = $metadata['source'];
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function check_contents(string $tmpname): bool
|
|
|
|
{
|
2019-06-09 18:22:48 +00:00
|
|
|
$valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP];
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!file_exists($tmpname)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$info = getimagesize($tmpname);
|
|
|
|
if (is_null($info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (in_array($info[2], $valid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function create_thumb(string $hash, string $type): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-18 18:45:59 +00:00
|
|
|
try {
|
|
|
|
create_image_thumb($hash, $type);
|
|
|
|
return true;
|
|
|
|
} catch (InsufficientMemoryException $e) {
|
|
|
|
$tsize = get_thumbnail_max_size_scaled();
|
|
|
|
$thumb = imagecreatetruecolor($tsize[0], min($tsize[1], 64));
|
|
|
|
$white = imagecolorallocate($thumb, 255, 255, 255);
|
|
|
|
$black = imagecolorallocate($thumb, 0, 0, 0);
|
|
|
|
imagefill($thumb, 0, 0, $white);
|
|
|
|
log_warning("handle_pixel", "Insufficient memory while creating thumbnail: ".$e->getMessage());
|
|
|
|
imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black);
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
log_error("handle_pixel", "Error while creating thumbnail: ".$e->getMessage());
|
|
|
|
return false;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$event->add_part("
|
2012-03-24 11:16:59 +00:00
|
|
|
<form>
|
2012-08-15 18:46:51 +00:00
|
|
|
<select class='shm-zoomer'>
|
2012-03-24 11:16:59 +00:00
|
|
|
<option value='full'>Full Size</option>
|
|
|
|
<option value='width'>Fit Width</option>
|
|
|
|
<option value='height'>Fit Height</option>
|
|
|
|
<option value='both'>Fit Both</option>
|
|
|
|
</select>
|
|
|
|
</form>
|
|
|
|
", 20);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|