Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
jplatform image script
* 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.
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()
// 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.
<!DOCTYPE html>
<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" />
<div class="container">
<div class="hero-unit">
<h3>Processing Images</h3>
<p>Set the directory name you want to run the processor on</p>
$path = 'images';
if ($path == '' || !(is_dir($path)))
echo "Path does not exist or is not defined. exiting";
return false;
echo "processing….";
$images = $this->getImages($path);
foreach($images as $img)
$this->processImage($img, $path);
public function getImages($path)
// get jfile
$files = JFolder::files($path);
return $files;
static function processImage($img, $path)
$imageObj = new JImage;
$fullpath = $path.'/'.$img;
$fullpath = JFile::stripExt($fullpath) . "_web" . ".jpg";
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.

drmmr763 commented Dec 12, 2012

I'm not sure what's going here on line #129 using the generateThumbs method. I get the file named correctly and saved to the directory, but the dimensions do not change. Source image is 1000x750. Ideas?

Something like this should work:

    $thumbs = $imageObj->generateThumbs(array('600x500'),$imageObj::SCALE_INSIDE);
    if ($thumbs)
        foreach ($thumbs as $thumb)
            // Destination folder
            $thumbsFolder = dirname($imageObj->getPath());

            // Get parents filename and extension
            $parentName      = pathinfo($imageObj->getPath(), PATHINFO_FILENAME);
            $parentExtension = pathinfo($imageObj->getPath(), PATHINFO_EXTENSION);
            $thumbFileName   = $parentName . '_web' . '.' . $parentExtension;

            // Generate thumb filename
            $thumbFileName = $thumbsFolder . '/' . $thumbFileName;

            // Save thumb

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment