public
Last active

jplatform image script

  • Download Gist
index.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
<?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();

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
            $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!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.