Jump to content

Thumbnail Sizes


ginger76

Recommended Posts

Hi,

 

Need a little help with a script. I have a set of scripts for showing events. The scripts create and display thumbnail images but they are all set at the same height, with different widths. I want to reverse that and have the same uniformed width with different heights. The script that handles the thumbnail sizes is as follows:

 

function thumb($file, $save, $width, $height)
{
	//GD-Lib > 2.0 only!
	@unlink($save);

	//get sizes else stop
	if (!$infos = @getimagesize($file)) {
		return false;
	}

	// keep proportions
	$iWidth = $infos[0];
	$iHeight = $infos[1];
	$iRatioW = $width / $iWidth;
	$iRatioH = $height / $iHeight;

	if ($iRatioW < $iRatioH) {
		$iNewW = $iWidth * $iRatioW;
		$iNewH = $iHeight * $iRatioW;
	} else {
		$iNewW = $iWidth * $iRatioH;
		$iNewH = $iHeight * $iRatioH;
	}

	//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) {
		/*
		* Image is typ gif
		*/
		$imgA = imagecreatefromgif($file);
		$imgB = imagecreate($iNewW,$iNewH);

       		//keep gif transparent color if possible
          	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) {
		/*
		* Image is typ jpg
		*/
		$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;
}

 

function flyercreator($image, $type= 'venue')
{
	$settings = & ELHelper::config();

	jimport('joomla.filesystem.file');

	//define the environment based on the type
	if ($type == 'event') {
		$folder		= 'events';
	} else {
		$folder 	= 'venues';
	}

	if ( $image ) {

		//Create thumbnail if enabled and it does not exist already
		if ($settings->gddisabled == 1 && !file_exists(JPATH_SITE.'/images/eventlist/'.$folder.'/small/'.$image)) {

			$filepath 	= JPATH_SITE.'/images/eventlist/'.$folder.'/'.$image;
			$save 		= JPATH_SITE.'/images/eventlist/'.$folder.'/small/'.$image;

			ELImage::thumb($filepath, $save, $settings->imagewidth, $settings->imagehight);
		}

		//set paths
		$dimage['original'] = 'images/eventlist/'.$folder.'/'.$image;
		$dimage['thumb'] 	= 'images/eventlist/'.$folder.'/small/'.$image;

		//get imagesize of the original
		$iminfo = @getimagesize('images/eventlist/'.$folder.'/'.$image);

		//if the width or height is too large this formula will resize them accordingly
		if (($iminfo[0] > $settings->imagewidth) || ($iminfo[1] > $settings->imagehight)) {

			$iRatioW = $settings->imagewidth / $iminfo[0];
			$iRatioH = $settings->imagehight / $iminfo[1];

			if ($iRatioW < $iRatioH) {
				$dimage['width'] 	= round($iminfo[0] * $iRatioW);
				$dimage['height'] 	= round($iminfo[1] * $iRatioW);
			} else {
				$dimage['width'] 	= round($iminfo[0] * $iRatioH);
				$dimage['height'] 	= round($iminfo[1] * $iRatioH);
			}

		} else {

			$dimage['width'] 	= $iminfo[0];
			$dimage['height'] 	= $iminfo[1];

		}

		if (JFile::exists(JPATH_SITE.'/images/eventlist/'.$folder.'/small/'.$image)) {

			//get imagesize of the thumbnail
			$thumbiminfo = @getimagesize('images/eventlist/'.$folder.'/small/'.$image);
			$dimage['thumbwidth'] 	= $thumbiminfo[0];
			$dimage['thumbheight'] 	= $thumbiminfo[1];

		}
		return $dimage;
	}
	return false;
}

 

The $infos[0]; and $infos[1]; are both 100px, set in an admin section. I already tried making a few changes but just made a mess of it (I'm not very good). I got the right width I wanted but the images were out of proportion. So, any help would be appreciated, thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.