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 >
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 {
2010-08-06 23:24:31 +00:00
public function setup () {
$sb = new SetupBlock ( " Image Zoom " );
$sb -> add_bool_option ( " image_zoom " , " Zoom by default: " );
return $sb ;
}
2009-07-16 19:20:53 +00:00
protected function supported_ext ( $ext ) {
2007-12-06 02:26:34 +00:00
$exts = array ( " jpg " , " jpeg " , " gif " , " png " );
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
2009-07-16 19:20:53 +00:00
protected function create_image_from_data ( $filename , $metadata ) {
2007-12-06 02:26:34 +00:00
global $config ;
$image = new Image ();
$info = " " ;
if ( ! ( $info = getimagesize ( $filename ))) return null ;
$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' ];
$image -> hash = $metadata [ 'hash' ];
$image -> filename = $metadata [ 'filename' ];
$image -> ext = $metadata [ 'extension' ];
2009-01-24 19:05:07 +00:00
$image -> tag_array = Tag :: explode ( $metadata [ 'tags' ]);
2007-12-06 02:26:34 +00:00
$image -> source = $metadata [ 'source' ];
return $image ;
}
2009-07-16 19:20:53 +00:00
protected function check_contents ( $file ) {
2008-03-29 03:59:34 +00:00
$valid = Array ( IMAGETYPE_PNG , IMAGETYPE_GIF , IMAGETYPE_JPEG );
if ( ! file_exists ( $file )) return false ;
$info = getimagesize ( $file );
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
}
2009-07-16 19:20:53 +00:00
protected function create_thumb ( $hash ) {
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 );
}
protected function create_thumb_force ( $hash ) {
2010-02-02 01:04:38 +00:00
$inname = warehouse_path ( " images " , $hash );
$outname = warehouse_path ( " thumbs " , $hash );
2007-12-06 02:26:34 +00:00
global $config ;
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 ;
}
// IM thumber {{{
private function make_thumb_convert ( $inname , $outname ) {
global $config ;
$w = $config -> get_int ( " thumb_width " );
$h = $config -> get_int ( " thumb_height " );
$q = $config -> get_int ( " thumb_quality " );
$mem = $config -> get_int ( " thumb_max_memory " ) / 1024 / 1024 ; // IM takes memory in MB
2010-01-27 12:25:19 +00:00
// Windows is a special case
if ( in_array ( " OS " , $_SERVER ) && $_SERVER [ " OS " ] == 'Windows_NT' ) {
2011-01-26 17:09:45 +00:00
$convert = $config -> get_string ( " thumb_convert_path " );
2010-01-22 17:42:00 +00:00
}
2010-01-27 12:25:19 +00:00
else {
2011-01-26 17:09:45 +00:00
$convert = " convert " ;
2010-01-27 12:25:19 +00:00
}
2010-01-22 17:42:00 +00:00
2011-01-26 17:09:45 +00:00
// ffff imagemagic fails sometimes, not sure why
//$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
2011-12-24 21:28:29 +00:00
$format = '"%s" "%s[0]" -crop %ux%u +repage -flatten -strip -thumbnail %ux%u -quality %u jpg:"%s"' ;
$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 );
log_debug ( 'handle_pixel' , " Generating thumnail with command ` $cmd `, returns $ret " );
2007-12-06 02:26:34 +00:00
return true ;
}
// }}}
2008-04-29 22:52:06 +00:00
// epeg thumber {{{
private function make_thumb_epeg ( $inname , $outname ) {
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 {{{
private function make_thumb_gd ( $inname , $outname ) {
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
}
private function get_thumb ( $tmpname ) {
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 ;
2007-12-06 02:26:34 +00:00
$image = imagecreatefromstring ( $this -> read_file ( $tmpname ));
$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 ;
}
}
2007-12-06 03:09:42 +00:00
private function read_file ( $fname ) {
$fp = fopen ( $fname , " r " );
if ( ! $fp ) return false ;
$data = fread ( $fp , filesize ( $fname ));
fclose ( $fp );
return $data ;
}
2007-12-06 02:26:34 +00:00
// }}}
}
add_event_listener ( new PixelFileHandler ());
?>