2007-12-06 02:26:34 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-01-05 10:32:39 +00:00
|
|
|
* Name: Handle Pixel
|
2007-12-06 02:26:34 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-10 04:04:37 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2019-06-09 18:22:48 +00:00
|
|
|
* Description: Handle JPEG, PNG, GIF, WEBP, etc files
|
2007-12-06 02:26:34 +00:00
|
|
|
*/
|
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
$info = getimagesize($filename);
|
|
|
|
if (!$info) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image->width = $info[0];
|
|
|
|
$image->height = $info[1];
|
|
|
|
|
|
|
|
$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
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2019-06-15 16:18:52 +00:00
|
|
|
$inname = warehouse_path(Image::IMAGE_DIR, $hash);
|
|
|
|
$outname = warehouse_path(Image::THUMBNAIL_DIR, $hash);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$ok = false;
|
|
|
|
|
|
|
|
switch ($config->get_string("thumb_engine")) {
|
|
|
|
default:
|
|
|
|
case 'gd':
|
|
|
|
$ok = $this->make_thumb_gd($inname, $outname);
|
|
|
|
break;
|
|
|
|
case 'convert':
|
2019-06-09 18:22:48 +00:00
|
|
|
$ok = create_thumbnail_convert($hash);
|
2019-05-28 16:59:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GD thumber {{{
|
|
|
|
private function make_thumb_gd(string $inname, string $outname): bool
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
try {
|
|
|
|
$info = getimagesize($inname);
|
2019-06-14 14:34:37 +00:00
|
|
|
$tsize = get_thumbnail_size($info[0], $info[1], true);
|
2019-06-14 12:47:50 +00:00
|
|
|
$image = image_resize_gd(
|
|
|
|
$inname,
|
|
|
|
$info,
|
|
|
|
$tsize[0],
|
|
|
|
$tsize[1],
|
|
|
|
$outname,
|
|
|
|
$config->get_string('thumb_type'),
|
|
|
|
$config->get_int('thumb_quality')
|
|
|
|
);
|
|
|
|
} catch (InsufficientMemoryException $e) {
|
|
|
|
$tsize = get_thumbnail_max_size_scaled();
|
|
|
|
$thumb = imagecreatetruecolor($tsize[0], min($tsize[1], 64));
|
2019-05-28 16:59:38 +00:00
|
|
|
$white = imagecolorallocate($thumb, 255, 255, 255);
|
|
|
|
$black = imagecolorallocate($thumb, 0, 0, 0);
|
|
|
|
imagefill($thumb, 0, 0, $white);
|
2019-06-14 12:47:50 +00:00
|
|
|
log_warning("handle_pixel", "Insufficient memory while creating thumbnail: ".$e->getMessage());
|
2019-05-28 16:59:38 +00:00
|
|
|
imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black);
|
2019-06-09 18:22:48 +00:00
|
|
|
return true;
|
2019-06-14 12:47:50 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
log_error("handle_pixel", "Error while creating thumbnail: ".$e->getMessage());
|
2019-06-09 18:22:48 +00:00
|
|
|
return false;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-06-09 18:22:48 +00:00
|
|
|
|
|
|
|
return true;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
// }}}
|
2007-12-06 02:26:34 +00:00
|
|
|
}
|