Resize image function in joomla


// Here is the code for creating thumb image & re-size it.

currently you have call upload function in component like this,

if(!JFile::upload($fileTemp, $uploadPath))

now this function call replace with this line

if(!JFile::thumb($fileTemp, $uploadPath, 150, 100))

put this function after upload function at : libraries/joomla/filesystem/file.php


function thumb($file, $save, $width, $height)
	{
		@unlink($save);

		if (!$infos = @getimagesize($file)) {
			return false;
		}

		$iWidth = $infos[0];
		$iHeight = $infos[1];
		
		//$iRatioW = $width / $iWidth;
		//$iRatioH = $height / $iHeight;
		
		//$iNewH = $height;
		
		if(!empty($height))
			$iNewH = $height;
		
		if(!empty($width))
			$iNewW = $width;
	
		if(empty($height))
			$iNewH = ($iHeight/$iWidth)*$iNewW;
		
		///Condition if width blank then set autowidth... otherwise set passing width...
		if(empty($width))
			$iNewW = ($iWidth/$iHeight)*$iNewH;

		//Don't resize images which are smaller than thumbs
		if ($infos[0] < $width && $infos[1] < $height) {
			$iNewW = $infos[0];
			$iNewH = $infos[1];
		}

		if($infos[2] == 1) {

			$imgA = imagecreatefromgif($file);
			$imgB = imagecreate($iNewW,$iNewH);
			
          	if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
            	$transcolorindex = imagecolortransparent($imgA);
            		//transparent color exists
            		if($transcolorindex >= 0 ) {
             			$transcolor = imagecolorsforindex($imgA, $transcolorindex);
              			$transcolorindex = imagecolorallocate($imgB, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
              			imagefill($imgB, 0, 0, $transcolorindex);
              			imagecolortransparent($imgB, $transcolorindex);
              		//fill white
            		} else {
              			$whitecolorindex = @imagecolorallocate($imgB, 255, 255, 255);
              			imagefill($imgB, 0, 0, $whitecolorindex);
            		}
            //fill white
          	} else {
            	$whitecolorindex = imagecolorallocate($imgB, 255, 255, 255);
            	imagefill($imgB, 0, 0, $whitecolorindex);
          	}
          	imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
			imagegif($imgB, $save);        

		} elseif($infos[2] == 2) {

			$imgA = imagecreatefromjpeg($file);
			$imgB = imagecreatetruecolor($iNewW,$iNewH);
			imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
			imagejpeg($imgB, $save);
			

		} elseif($infos[2] == 3) {
			/*
			* Image is typ png
			*/
			$imgA = imagecreatefrompng($file);
			$imgB = imagecreatetruecolor($iNewW, $iNewH);
			imagealphablending($imgB, false);
			imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
			imagesavealpha($imgB, true);
			imagepng($imgB, $save);
			
		} else {
			return false;
		}
		return true;
	}

This function will helpful to you for re-sizing images in their proportions with good resolutions.

About these ads

One thought on “Resize image function in joomla

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s