Skip to content

Instantly share code, notes, and snippets.

@drmmr763
Created December 12, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmmr763/4268599 to your computer and use it in GitHub Desktop.
Save drmmr763/4268599 to your computer and use it in GitHub Desktop.
jplatform image script
<?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();
@drmmr763
Copy link
Author

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?

@phproberto
Copy link

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
            $thumb->toFile($thumbFileName);
        }
    }

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