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 /
2010-01-05 10:32:39 +00:00
* Description : Handle JPEG , PNG , GIF , etc files
2007-12-06 02:26:34 +00:00
*/
2009-07-16 19:20:53 +00:00
class PixelFileHandler extends DataHandlerExtension {
2017-09-19 17:55:43 +00:00
protected function supported_ext ( string $ext ) : bool {
2007-12-06 02:26:34 +00:00
$exts = array ( " jpg " , " jpeg " , " gif " , " png " );
2013-07-02 02:31:06 +00:00
$ext = (( $pos = strpos ( $ext , '?' )) !== false ) ? substr ( $ext , 0 , $pos ) : $ext ;
2009-06-05 19:53:00 +00:00
return in_array ( strtolower ( $ext ), $exts );
2007-12-06 02:26:34 +00:00
}
2009-01-04 19:18:37 +00:00
2017-09-19 17:55:43 +00:00
protected function create_image_from_data ( string $filename , array $metadata ) {
2007-12-06 02:26:34 +00:00
$image = new Image ();
2014-03-29 11:44:34 +00:00
$info = getimagesize ( $filename );
if ( ! $info ) return null ;
2007-12-06 02:26:34 +00:00
$image -> width = $info [ 0 ];
$image -> height = $info [ 1 ];
2009-01-04 19:18:37 +00:00
2007-12-06 02:26:34 +00:00
$image -> filesize = $metadata [ 'size' ];
2016-07-30 22:02:14 +00:00
$image -> hash = $metadata [ 'hash' ];
2013-07-02 02:31:06 +00:00
$image -> filename = (( $pos = strpos ( $metadata [ 'filename' ], '?' )) !== false ) ? substr ( $metadata [ 'filename' ], 0 , $pos ) : $metadata [ 'filename' ];
2016-07-30 22:02:14 +00:00
$image -> ext = (( $pos = strpos ( $metadata [ 'extension' ], '?' )) !== false ) ? substr ( $metadata [ 'extension' ], 0 , $pos ) : $metadata [ 'extension' ];
2017-05-12 07:57:50 +00:00
$image -> tag_array = is_array ( $metadata [ 'tags' ]) ? $metadata [ 'tags' ] : Tag :: explode ( $metadata [ 'tags' ]);
2016-07-30 22:02:14 +00:00
$image -> source = $metadata [ 'source' ];
2007-12-06 02:26:34 +00:00
return $image ;
}
2017-09-19 17:55:43 +00:00
protected function check_contents ( string $tmpname ) : bool {
2008-03-29 03:59:34 +00:00
$valid = Array ( IMAGETYPE_PNG , IMAGETYPE_GIF , IMAGETYPE_JPEG );
2017-09-19 17:55:43 +00:00
if ( ! file_exists ( $tmpname )) return false ;
$info = getimagesize ( $tmpname );
2008-03-29 03:59:34 +00:00
if ( is_null ( $info )) return false ;
2009-06-05 19:53:00 +00:00
if ( in_array ( $info [ 2 ], $valid )) return true ;
2008-03-29 03:59:34 +00:00
return false ;
2007-12-06 02:26:34 +00:00
}
2017-09-19 17:55:43 +00:00
protected function create_thumb ( string $hash ) : bool {
2012-01-12 01:54:27 +00:00
$outname = warehouse_path ( " thumbs " , $hash );
if ( file_exists ( $outname )) {
return true ;
}
return $this -> create_thumb_force ( $hash );
}
2017-09-19 17:55:43 +00:00
protected function create_thumb_force ( string $hash ) : bool {
2014-04-06 19:47:01 +00:00
global $config ;
2014-03-30 12:26:48 +00:00
2010-02-02 01:04:38 +00:00
$inname = warehouse_path ( " images " , $hash );
$outname = warehouse_path ( " thumbs " , $hash );
2009-01-04 19:18:37 +00:00
2007-12-06 02:26:34 +00:00
$ok = false ;
2009-01-04 19:18:37 +00:00
2007-12-06 02:26:34 +00:00
switch ( $config -> get_string ( " thumb_engine " )) {
default :
case 'gd' :
$ok = $this -> make_thumb_gd ( $inname , $outname );
break ;
case 'convert' :
$ok = $this -> make_thumb_convert ( $inname , $outname );
break ;
}
return $ok ;
}
2014-04-27 19:33:57 +00:00
2012-03-24 11:16:59 +00:00
public function onImageAdminBlockBuilding ( ImageAdminBlockBuildingEvent $event ) {
$event -> add_part ( "
< 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);
}
2007-12-06 02:26:34 +00:00
// IM thumber {{{
2017-09-19 17:55:43 +00:00
private function make_thumb_convert ( string $inname , string $outname ) : bool {
2007-12-06 02:26:34 +00:00
global $config ;
$w = $config -> get_int ( " thumb_width " );
$h = $config -> get_int ( " thumb_height " );
$q = $config -> get_int ( " thumb_quality " );
2012-08-22 01:51:20 +00:00
$convert = $config -> get_string ( " thumb_convert_path " );
2010-01-22 17:42:00 +00:00
2019-04-16 19:41:13 +00:00
// ffff imagemagick fails sometimes, not sure why
2011-01-26 17:09:45 +00:00
//$format = "'%s' '%s[0]' -format '%%[fx:w] %%[fx:h]' info:";
//$cmd = sprintf($format, $convert, $inname);
//$size = shell_exec($cmd);
//$size = explode(" ", trim($size));
$size = getimagesize ( $inname );
if ( $size [ 0 ] > $size [ 1 ] * 5 ) $size [ 0 ] = $size [ 1 ] * 5 ;
if ( $size [ 1 ] > $size [ 0 ] * 5 ) $size [ 1 ] = $size [ 0 ] * 5 ;
// running the call with cmd.exe requires quoting for our paths
2012-03-06 20:30:41 +00:00
$format = '"%s" "%s[0]" -extent %ux%u -flatten -strip -thumbnail %ux%u -quality %u jpg:"%s"' ;
2011-12-24 21:28:29 +00:00
$cmd = sprintf ( $format , $convert , $inname , $size [ 0 ], $size [ 1 ], $w , $h , $q , $outname );
2011-08-08 12:01:35 +00:00
$cmd = str_replace ( " \" convert \" " , " convert " , $cmd ); // quotes are only needed if the path to convert contains a space; some other times, quotes break things, see github bug #27
2010-01-22 17:42:00 +00:00
exec ( $cmd , $output , $ret );
2019-04-16 19:41:13 +00:00
log_debug ( 'handle_pixel' , " Generating thumbnail with command ` $cmd `, returns $ret " );
2007-12-06 02:26:34 +00:00
2012-06-23 21:52:47 +00:00
if ( $config -> get_bool ( " thumb_optim " , false )) {
exec ( " jpegoptim $outname " , $output , $ret );
}
2007-12-06 02:26:34 +00:00
return true ;
}
// }}}
2008-04-29 22:52:06 +00:00
// epeg thumber {{{
2017-09-19 17:55:43 +00:00
private function make_thumb_epeg ( string $inname , string $outname ) : bool {
2008-04-29 22:52:06 +00:00
global $config ;
$w = $config -> get_int ( " thumb_width " );
exec ( " epeg $inname -c 'Created by EPEG' --max $w $outname " );
return true ;
}
// }}}
2007-12-06 02:26:34 +00:00
// GD thumber {{{
2017-09-19 17:55:43 +00:00
private function make_thumb_gd ( string $inname , string $outname ) : bool {
2007-12-06 02:26:34 +00:00
global $config ;
$thumb = $this -> get_thumb ( $inname );
2008-01-26 11:40:50 +00:00
$ok = imagejpeg ( $thumb , $outname , $config -> get_int ( 'thumb_quality' ));
imagedestroy ( $thumb );
return $ok ;
2007-12-06 02:26:34 +00:00
}
2017-09-19 17:55:43 +00:00
private function get_thumb ( string $tmpname ) {
2007-12-06 02:26:34 +00:00
global $config ;
$info = getimagesize ( $tmpname );
$width = $info [ 0 ];
$height = $info [ 1 ];
$memory_use = ( filesize ( $tmpname ) * 2 ) + ( $width * $height * 4 ) + ( 4 * 1024 * 1024 );
$memory_limit = get_memory_limit ();
2009-01-04 19:18:37 +00:00
2007-12-06 02:26:34 +00:00
if ( $memory_use > $memory_limit ) {
$w = $config -> get_int ( 'thumb_width' );
$h = $config -> get_int ( 'thumb_height' );
$thumb = imagecreatetruecolor ( $w , min ( $h , 64 ));
$white = imagecolorallocate ( $thumb , 255 , 255 , 255 );
$black = imagecolorallocate ( $thumb , 0 , 0 , 0 );
imagefill ( $thumb , 0 , 0 , $white );
imagestring ( $thumb , 5 , 10 , 24 , " Image Too Large :( " , $black );
return $thumb ;
}
else {
2011-01-26 17:09:45 +00:00
if ( $width > $height * 5 ) $width = $height * 5 ;
if ( $height > $width * 5 ) $height = $width * 5 ;
2014-03-30 12:26:48 +00:00
$image = imagecreatefromstring ( file_get_contents ( $tmpname ));
2007-12-06 02:26:34 +00:00
$tsize = get_thumbnail_size ( $width , $height );
$thumb = imagecreatetruecolor ( $tsize [ 0 ], $tsize [ 1 ]);
imagecopyresampled (
$thumb , $image , 0 , 0 , 0 , 0 ,
$tsize [ 0 ], $tsize [ 1 ], $width , $height
);
return $thumb ;
}
}
// }}}
}