Created
December 12, 2012 15:17
-
-
Save drmmr763/4268599 to your computer and use it in GitHub Desktop.
jplatform image script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An example JApplicationWeb application built on the Joomla Platform. | |
* | |
* To run this example, copy or soft-link this folder to your web server tree. | |
* | |
* @package Joomla.Examples | |
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
// We are a valid Joomla entry point. | |
define('_JEXEC', 1); | |
// Setup the base path related constant. | |
define('JPATH_BASE', '/'); | |
// Setup the site basepath | |
define('JPATH_SITE', '/'); | |
// Increase error reporting to that any errors are displayed. | |
// Note, you would not use these settings in production. | |
error_reporting(E_ALL); | |
ini_set('display_errors', true); | |
// Bootstrap the application. | |
require 'bootstrap.php'; | |
/** | |
* An example JApplicationWeb application class. | |
* | |
* @package Joomla.Examples | |
* @since 11.3 | |
*/ | |
class RenameImages extends JApplicationWeb | |
{ | |
/** | |
* Overrides the parent doExecute method to run the web application. | |
* | |
* This method should include your custom code that runs the application. | |
* | |
* @return void | |
* | |
* @since 11.3 | |
*/ | |
protected function doExecute() | |
{ | |
jimport('joomla.filesystem.file'); | |
jimport('joomla.image.image'); | |
jimport('joomla.filesystem.folder'); | |
// This application will just output a simple HTML document. | |
// Use the setBody method to set the output. | |
// JApplicationWeb will take care of all the headers and such for you. | |
$this->setBody(' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Image Processor</title> | |
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> | |
<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css" /> | |
<link href="bootstrap/js/bootstrap.min.js" rel="javascript" type="text/js" /> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="hero-unit"> | |
<h3>Processing Images</h3> | |
<p>Set the directory name you want to run the processor on</p> | |
'); | |
$this->appendBody(' | |
</div> | |
</div> | |
</body> | |
</html>done.' | |
); | |
// SET THIS AS THE DIRECTORY TO RUN | |
$path = 'images'; | |
if ($path == '' || !(is_dir($path))) | |
{ | |
echo "Path does not exist or is not defined. exiting"; | |
return false; | |
} | |
else | |
{ | |
echo "processing…."; | |
} | |
$images = $this->getImages($path); | |
foreach($images as $img) | |
{ | |
$this->processImage($img, $path); | |
//print_r($images[$i]); | |
} | |
//$this->renameImage($path); | |
} | |
public function getImages($path) | |
{ | |
// get jfile | |
$files = JFolder::files($path); | |
//print_r(); | |
return $files; | |
} | |
static function processImage($img, $path) | |
{ | |
$imageObj = new JImage; | |
$fullpath = $path.'/'.$img; | |
$imageObj->loadFile($fullpath); | |
$imageObj->generateThumbs(array('600x500'),$imageObj::SCALE_INSIDE); | |
$fullpath = JFile::stripExt($fullpath) . "_web" . ".jpg"; | |
$imageObj->toFile($fullpath); | |
} | |
public static function renameImage($path) | |
{ | |
$files = JFolder::files($path.'/thumbs'); | |
foreach ($files as $file) | |
{ | |
$old_name = JFile::getName($path.'/'.$file); | |
//echo 'old name' . $old_name; | |
$old_ext = JFile::getExt($path.'/'.$file); | |
//echo 'old ext' . $old_ext; | |
$new_name = str_replace('_500x375', '',$old_name); | |
//echo 'new name' . $new_name; | |
rename($path.'/thumbs/'.$file, $path.'/thumbs/'.$new_name); | |
} | |
} | |
public function listDirectoriesLinks() | |
{ | |
$list = JFolder::folders(dirname(__FILE__)); | |
return $list; | |
} | |
} | |
// Instantiate the application object, passing the class name to JApplicationWeb::getInstance | |
// and use chaining to execute the application. | |
JApplicationWeb::getInstance('RenameImages')->execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this should work:
I didn't tested it so WARNING! :D
Tip: Try always to avoid using JFile & JFolder. All is doable with native PHP functions and that's why they are going to be deprecated soon.
Hope this works!