1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 12:48:24 +01:00

Issue #2869 Media-Manager: Auto-convert to jpeg option added.

This commit is contained in:
Cameron 2017-11-16 20:17:07 -08:00
parent a6ede510a9
commit c10d350d8b
4 changed files with 183 additions and 17 deletions

View File

@ -132,9 +132,11 @@ class media_admin extends e_admin_dispatcher
'main/list' => array('caption'=> LAN_IMA_M_01, 'perm' => 'A'),
// 'main/create' => array('caption'=> "Add New Media", 'perm' => 'A'), // Should be handled in Media-Import.
'main/import' => array('caption'=> LAN_IMA_M_02, 'perm' => 'A|A1'),
'divider/01' => array('divider'=>true),
'cat/list' => array('caption'=> LAN_IMA_M_03, 'perm' => 'A|A2'),
'cat/create' => array('caption'=> LAN_IMA_M_04, 'perm' => 'A|A2'), // is automatic.
// 'main/settings' => array('caption'=> LAN_PREFS, 'perm' => 'A'), // legacy
'divider/02' => array('divider'=>true),
'main/prefs' => array('caption'=> LAN_PREFS, 'perm' => 'A'),
'main/avatar' => array('caption'=> LAN_IMA_M_05, 'perm' => 'A')
);
@ -361,7 +363,7 @@ class media_form_ui extends e_admin_form_ui
asort($this->cats);*/
// require(e_HANDLER.'phpthumb/ThumbLib.inc.php'); // For resizing on import.
if(varset($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__rotate_cw' || varset($_POST['etrigger_batch']) == 'options__rotate_ccw'))
if(!empty($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__rotate_cw' || varset($_POST['etrigger_batch']) == 'options__rotate_ccw'))
{
$type = str_replace('options__','',$_POST['etrigger_batch']);
$ids = implode(",", e107::getParser()->filter($_POST['multiselect'],'int'));
@ -371,14 +373,26 @@ class media_form_ui extends e_admin_form_ui
}
if(varset($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__resize_2048' ))
if(!empty($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__resize_2048' ))
{
$type = str_replace('options__','',$_POST['etrigger_batch']);
$ids = implode(",", e107::getParser()->filter($_POST['multiselect'],'int'));
$this->resizeImages($ids,$type);
}
if(!empty($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__convert_to_jpeg' ))
{
// $type = str_replace('options__','',$_POST['etrigger_batch']);
$ids = implode(",", e107::getParser()->filter($_POST['multiselect'],'int'));
$this->convertImagesToJpeg($ids);
}
if(!empty($_POST['multiselect']) && varset($_POST['e__execute_batch']) && (varset($_POST['etrigger_batch']) == 'options__convert_all_to_jpeg' ))
{
// $type = str_replace('options__','',$_POST['etrigger_batch']);
$ids = implode(",", e107::getParser()->filter($_POST['multiselect'],'int'));
$this->convertImagesToJpeg($ids,'all');
}
}
@ -523,6 +537,55 @@ class media_form_ui extends e_admin_form_ui
}
private function convertImagesToJpeg($ids,$mode=null)
{
$sql = e107::getDb();
$tp = e107::getParser();
$mm = e107::getMedia();
$insert = empty($mode) ? "media_id IN (".$ids.") AND " : " media_size > 225000 AND ";
$data = $sql->retrieve("core_media","media_id,media_url", $insert."(media_type = 'image/png' OR media_type = 'image/gif') ", true, true);
if(empty($data))
{
return null;
}
foreach($data as $row)
{
$path = $tp->replaceConstants($row['media_url']);
if($jpegFile = $mm->convertImageToJpeg($path,true))
{
$url = $tp->createConstants($jpegFile);
$size = filesize($jpegFile);
$update = array (
'media_size' => $size,
'media_url' => $url,
'media_type' => 'image/jpeg',
'WHERE' => 'media_id = '.$row['media_id']
);
$message = basename($path).SEP.basename($url);
if($sql->update("core_media",$update))
{
e107::getMessage()->addSuccess($message);
}
else
{
e107::getMessage()->addError($message);
}
}
}
}
@ -579,16 +642,24 @@ class media_form_ui extends e_admin_form_ui
//return print_a($_GET,true);
if($value == 'batch')
{
return array(
"resize_2048" => "Reduce Oversized Images",
"rotate_cw" => "Rotate 90° cw",
"rotate_ccw" => "Rotate 90° ccw"
);
$arr = array(
"resize_2048" => "Reduce Oversized Images",
"rotate_cw" => "Rotate 90° cw",
"rotate_ccw" => "Rotate 90° ccw",
'convert_to_jpeg' => "Convert to jpeg format"
);
if(deftrue('e_DEBUG'))
{
$arr['convert_all_to_jpeg'] = "Convert All Oversized to jpeg format"; // rare situations.
}
return $arr;
}
if($_GET['action'] == 'edit')
{
return;
return null;
}
$tagid = vartrue($_GET['tagid']);
@ -808,10 +879,13 @@ class media_admin_ui extends e_admin_ui
'image_post_class' => array('title'=> IMALAN_10, 'type' => 'userclass', 'data'=>'int', 'writeParms'=>'help=IMALAN_11&classlist=public,guest,nobody,member,admin,main,classes' ),
'image_post_disabled_method' => array('title'=> IMALAN_12, 'type' => 'boolean','writeParms'=>'enabled=IMALAN_15&disabled=IMALAN_14'),
'resize_method' => array('title'=> IMALAN_3, 'type'=>'method', 'data'=>'str'),
'thumbnail_quality' => array('title'=> IMALAN_73, 'type'=>'number', 'data'=>'int', 'writeParms'=>'help=IMALAN_74'),
'im_width' => array('title'=> IMALAN_75, 'type'=>'number', 'data'=>'int', 'writeParms'=>'help=IMALAN_76'),
'im_height' => array('title'=> IMALAN_77, 'type'=>'number', 'data'=>'int', 'writeParms'=>'help=IMALAN_76'),
'thumbnail_quality' => array('title'=> IMALAN_73, 'type'=>'number', 'data'=>'int', 'writeParms'=>'', 'help'=>IMALAN_74),
// 'convert_to_jpeg' => array('title'=> IMALAN_182, 'type'=>'number', 'data'=>'int', 'writeParms'=> array('tdClassRight'=>'form-inline', 'post'=> CORE_LAN_KB), 'help'=>IMALAN_183),
'convert_to_jpeg' => array('title'=> IMALAN_182, 'type'=>'boolean', 'data'=>'int', 'writeParms'=>'', 'help'=>IMALAN_183),
'im_width' => array('title'=> IMALAN_75, 'type'=>'number', 'data'=>'int', 'writeParms'=>'', 'help'=>IMALAN_76),
'im_height' => array('title'=> IMALAN_77, 'type'=>'number', 'data'=>'int', 'writeParms'=>'', 'help'=>IMALAN_76),
'resize_dimensions' => array('title'=> IMALAN_79, 'type'=>'method', 'data'=>'str'),
'watermark_activate' => array('title'=> IMALAN_80, 'tab'=>1, 'type' => 'number', 'data' => 'str', 'help'=>IMALAN_81), // 'validate' => 'regex', 'rule' => '#^[\d]+$#i', 'help' => 'allowed characters are a-zA-Z and underscore')),

View File

@ -1592,7 +1592,78 @@ class e_media
}
/**
* Convert an image to jpeg format.
* @param string $oldFile path to png or gif file.
* @param bool $deleteOld - set to true to delete original after conversion.
* @return string path to new file.
*/
public function convertImageToJpeg($oldFile, $deleteOld=false)
{
if(substr($oldFile,-4) !== '.gif' && substr($oldFile,-4) !== '.png') // jpg or some other format already
{
return false;
}
if(strpos($oldFile,".gif") !==false)
{
$type = '.gif';
}
if(strpos($oldFile,".png") !==false)
{
$type = '.png';
}
if(empty($type))
{
return $oldFile;
}
$jpgFile = str_replace($type, ".jpg", $oldFile);
$compression = e107::getPref('thumbnail_quality', 45); // 0 = worst / smaller file, 100 = better / bigger file
if(!file_exists($jpgFile))
{
switch($type)
{
case ".gif":
$image = imagecreatefromgif($oldFile);
break;
case ".png":
$image = imagecreatefrompng($oldFile);
break;
}
if(empty($image))
{
return false;
}
if(imagejpeg($image, $jpgFile, $compression) === true)
{
if($deleteOld === true)
{
unlink($oldFile);
}
}
else
{
$jpgFile = false; // fallback to original
}
// e107::getLog()->addSuccess("Converting <b>".$oldFile."</b> to <b>".$jpgFile."</b>");
imagedestroy($image);
}
return $jpgFile;
}
}

View File

@ -202,3 +202,6 @@ define("IMALAN_179", "Avatars Folder (private)");
define('IMALAN_180', "0 byte file found in:");
define("IMALAN_181", "Please remove before proceeding.");
define("IMALAN_182", "Convert to jpeg during import");
define("IMALAN_183", "PNG and GIF files will be automatically converted to jpeg format. (icons excluded)");

View File

@ -183,8 +183,22 @@
rename("{$filePath}.part", $filePath);
}
$filePath = str_replace('//','/',$filePath); // cleanup .
$convertToJpeg = e107::getPref('convert_to_jpeg', 0);
$fileSize = filesize($filePath);
if(varset($_GET['for']) !== '_icon' && !empty($convertToJpeg))
{
if($jpegFile = e107::getMedia()->convertImageToJpeg($filePath, true))
{
$filePath = $jpegFile;
$fileName = basename($filePath);
$fileSize = filesize($jpegFile);
}
}
// rename($targetDir.$fileName,e_MEDIA."images/2012-05/",$fileName);
if($_GET['for'] != '') // leave in upload directory if no category given.
{
$uploadPath = varset($_GET['path'],null);
@ -192,13 +206,17 @@
}
$log = $_GET;
$log['filepath'] = $filePath;
$log = e107::getParser()->filter($_GET,'str');
$log['filepath'] = str_replace('../','',$filePath);
$log['filename'] = $fileName;
$log['filesize'] = $fileSize;
$log['status'] = ($result) ? 'ok' : 'failed';
$type = ($result) ? E_LOG_INFORMATIVE : E_LOG_WARNING;
e107::getLog()->add('Media Upload', print_r($log, true), $type, MEDIA_01);
e107::getLog()->add('LAN_AL_MEDIA_01', print_r($log, true), $type, 'MEDIA_01');
$array = array("jsonrpc" => "2.0", "result" => $result, "id" => "id");