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
https://forums.phpfreaks.com/topic/151607-thumbnail-sizes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.