Logo MediaEventi
Automatically resize images while uploading to Image Manager in Joomla
Sabato 26 Giugno 2010 11:59


I was looking for a way to automatically resize images with the Image Manager in Joomla, while uploading, so that my client could upload a photograph from his digital camera without having to resize manually.

NOTE: this is a core hack, so you need to manually a core file. You need to remember this when you update your joomla installation with new releases, because it's can happen that a new release would overwrite the file that you edited, so you will need to make this hack again.

How to implement the function resize

To automatically resize an image (png, gif or jpg) while uploading to Image Manager, you need to edit the file "file.php" found in "administrator\components\com_media\controllers".

What you need to do is to add a function "resize" to file.php and to recall the function just after the image is uploaded.

Step 1

Open file.php and copy the function below just after the function upload(). In my case I added the function after line 128. The function to paste is the following:

 
//IMAGE RESIZE SCRIPT 
//Credit goes to "vandai" on www.akemapa.com -- code from http://www.php.net/manual/en/function.imagecreatefromjpeg.php#86605

function resize($filepath, $max_resize_width, $max_resize_height) {
	global $mainframe;
	JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );
	
	$thumb_width = $max_resize_width;
	$thumb_height = $max_resize_height;
	$img = $filepath;
	$newfilename = $filepath;
	
	$max_width=$thumb_width;
	$max_height=$thumb_height;

	//Check if GD extension is loaded
	if (!extension_loaded('gd') && !extension_loaded('gd2')) 
	{
		trigger_error("GD is not loaded", E_USER_WARNING);
		return false;
	}

	//Get Image size info
	list($width_orig, $height_orig, $image_type) = getimagesize($img);
	
	switch ($image_type) 
	{
		case 1: $im = imagecreatefromgif($img); break;
		case 2: $im = imagecreatefromjpeg($img);  break;
		case 3: $im = imagecreatefrompng($img); break;
		default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
	}
	
	/*** calculate the aspect ratio ***/
	$aspect_ratio = (float) $height_orig / $width_orig;

	if (($width_orig >= $height_orig) || ($max_resize_height == 0) || ($max_resize_height == NULL)){
		/*** calulate the thumbnail width based on the height ***/
		$thumb_height = round($thumb_width * $aspect_ratio);
		
		while($thumb_height>$max_width)
			{
				$thumb_width-=10;
				$thumb_height = round($thumb_width * $aspect_ratio);
			}
	}
	
	else {
		/*** calulate the thumbnail height based on the width ***/
		$aspect_ratio = (float) $width_orig / $height_orig;					
		$thumb_width = round($thumb_height * $aspect_ratio);
		
		while($thumb_width > $max_height)
			{
				$thumb_height-=10;
				$thumb_width = round($thumb_height * $aspect_ratio);
			}				
	}				
			
			$newImg = imagecreatetruecolor($thumb_width, $thumb_height);
			
			/* Check if this image is PNG or GIF, then set if Transparent*/  
			if(($image_type == 1) OR ($image_type==3))
			{
				imagealphablending($newImg, false);
				imagesavealpha($newImg,true);
				$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
				imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
			}
			imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
			
			//Generate the file, and rename it to $newfilename
			switch ($image_type) 
			{
				case 1: imagegif($newImg,$newfilename); break;
				case 2: imagejpeg($newImg,$newfilename);  break;
				case 3: imagepng($newImg,$newfilename); break;
				default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
			}

			// END IMAGE RESIZE	SCRIPT		
	return;
}	

Your file now should look like this:

 
		}
	} else {
		$mainframe->redirect('index.php', 'Invalid Request', 'error');
	}
}

function resize($filepath, $max_resize_width, $max_resize_height) {
...
}

/**
 * Deletes paths from the current path
 *
 * @param string $listFolder The image directory to delete a file from
 * @since 1.5
 */
function delete()

Step 2

Now the last thing to do is to add a line where you call the function. Go to line 110 and insert this function:
 
$this->resize($filepath,960,600);
The function is pretty simple:
 
$this->resize($filepath,[max width of images resized],[max height of images resized]);
So now the function should look like this:
 
		return;
	}
} else {

	$this->resize($filepath,960,600);

	if ($format == 'json') {

Demo Video


Content Requires Flash Player

Get Adobe Flash player



File demo

For an example of the final result you can download my file.php: download

More tweakings: resize by folder

This is where this tool comes very handy: you can choose the max size of any image uploaded dependingly on the folder where the image is going to be uploaded. Eg: say you have a client that has got some problems using tools programs like photoshop to resize his images that he need to upload for a gallery. Now your client's website has a main slideshow gallery and some secondary galleries, and the two has different size.

My real case was this:
  • my client had a slideshow in the front page; the size of images was max 960px X 600px
  • in another section "Galleries" he needed to create some galleries; the size here is max 800px X 600px

Ok, how to deal with this request?
The solution is simple: add a "if" statement in the file.php where you select the size into the function resize dependingly on the folder in the Image Manager.

In this example:
 
if ($folder == "home_gallery")
	$this->resize($filepath,960,600);
else
	$this->resize($filepath,800,600);

Nice, isn't it?! If you need to check easily if the name of the folder is correct, you can have a visual feedback by using the function enqueueMessage available in the upload() function.

To do this, go to near line 119 where you find the function:
 
$mainframe->enqueueMessage(JText::_('Upload complete'));
and write something like that:
 
//$mainframe->enqueueMessage(JText::_('Upload complete'));
$mainframe->enqueueMessage($folder);
Now upload an image in the folder where you want to make the resize. The warning message will return the folder path:

 

Commenti  

 
0 #1 great!Thelma 2010-10-20 17:58
nice post! thanks for the valuable information. I'd like to recommend term papers and essays. Hope you will find it useful.
 
 
0 #2 RE: Automatically resize images while uploading to Image Manager in Joomlafreelance writer job 2010-12-19 13:04
you can choose the max size of any image uploaded dependingly on the folder where the image is going to be uploaded.
 

Your are currently browsing this site with Internet Explorer 6 (IE6).

Your current web browser must be updated to version 7 of Internet Explorer (IE7) to take advantage of all of template's capabilities.

Why should I upgrade to Internet Explorer 7? Microsoft has redesigned Internet Explorer from the ground up, with better security, new capabilities, and a whole new interface. Many changes resulted from the feedback of millions of users who tested prerelease versions of the new browser. The most compelling reason to upgrade is the improved security. The Internet of today is not the Internet of five years ago. There are dangers that simply didn't exist back in 2001, when Internet Explorer 6 was released to the world. Internet Explorer 7 makes surfing the web fundamentally safer by offering greater protection against viruses, spyware, and other online risks.

Get free downloads for Internet Explorer 7, including recommended updates as they become available. To download Internet Explorer 7 in the language of your choice, please visit the Internet Explorer 7 worldwide page.