Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2010, 04:39 PM   PM User | #1
Tara Rotten
New to the CF scene

 
Join Date: Apr 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Tara Rotten is an unknown quantity at this point
PHP image resize help!

Hey,

Im using the indexhibit cms. This cms comes with some nice features such as image resizing on upload..the only problem is that it resizes the image by width and I want to switch it so it resizes the images by height.

here is the code, I have tried switching the x and y and the height (h) and width (w) but it didnt work please help!!

PHP Code:
<?php if (!defined('SITE')) exit('No direct script access allowed');


/**
* Media class
*
* Resizes and thumbnails images
*
* @version 1.0
* @author Vaska
*/
class Media
{
var 
$image;
var 
$path;
var 
$filename;
var 
$quality;
var 
$filemime;
var 
$maxsize;
var 
$thumbsize;
var 
$sizelimit;
var 
$size = array();
var 
$new_size = array();
var 
$makethumb FALSE;
var 
$final_size = array();
var 
$out_size = array();
var 
$uploads = array();
var 
$sys_thumb 100;
var 
$offset = array();
var 
$sys_size = array();
var 
$type;
var 
$input_image;
var 
$upload_max_size;
var 
$file_size;
var 
$tRed;
var 
$tBlue;
var 
$tGreen;
var 
$tFlag FALSE;

/**
* Returns allowed uploads (filetypes from config.php) array and max size
*
* @param void
* @return mixed
*/
function Media()
{
global 
$uploads;
$this->uploads $uploads;
$this->upload_max_size();
}

/**
* Returns filetype by file extension
*
* @param void
* @return string
*/
function getFileType()
{
$type explode('.'$this->filename);
$this->filemime array_pop($type);
}

/**
* Returns array of image filetypes
*
* @param void
* @return array
*/
function allowThumbs()
{
return 
$this->uploads['images'];
}

/**
* Returns server settings for max upload size
*
* @param void
* @return integer
*/
function upload_max_size()
{
$upload_max_filesize ini_get('upload_max_filesize');
$upload_max_filesize preg_replace('/M/''000000'$upload_max_filesize);

$post_max_size ini_get('post_max_size');
$post_max_size preg_replace('/M/''000000'$post_max_size);

$this->upload_max_size = ($post_max_size >= $upload_max_filesize) ? $upload_max_filesize $post_max_size;
}

/**
* Return destroys input image
*
* @param void
* @return mixed
*/
function uploader()
{
$this->getFileType();
$this->get_input();
$this->size getimagesize($this->image);

// first image
$this->upload_image($this->maxsize);
$this->file_size();

// system thumbnail
$this->sys_thumb($this->sys_thumb);

// we'll need to distinguish this for only images
if (($this->makethumb == TRUE) && (in_array($this->filemime$this->allowThumbs())))
{
$this->upload_image($this->thumbsizeTRUE);
}

imagedestroy($this->input_image);
}

/**
* Deals with the bits
* Oh. So. Messy. ;)
*
* @param integer $maxwidth
* @param boolean $thumb
* @return integer
*/
function upload_image($maxwidth$thumb=FALSE)
{
if ((
$maxwidth != 9999) || ($thumb == TRUE))
{
// get the new sizes
$this->resizing($maxwidth);

$output_image imagecreatetruecolor($this->new_size['w'], $this->new_size['h']);

// if we have transparency in the image
// it sucks that PHP auto sets background to black!!!!!!!
if ($this->tFlag == TRUE)
{
imagecolortransparent($output_imageimagecolorallocate($output_image,
$this->tRed$this->tGreen$this->tBlue));
}

// png special handling rules
if ($this->filemime == 'png')
{
// [url]http://be.php.net/manual/en/function.imagesavealpha.php[/url]
imagealphablending($output_imagefalse);
imagesavealpha($output_imagetrue);
}

// resizing
@imagecopyresampled($output_image$this->input_image0000,
$this->new_size['h'], $this->new_size['w'], $this->size[0], $this->size[1]);

// how do we flag when we are working on thumbs>
if ($thumb == TRUE)
{
$this->image $this->path 'th-' $this->filename;
}

$this->do_output($output_image$this->image);
imagedestroy($output_image);
}
else
{
// no resize - get file x, y
$this->out_size['x'] = $this->size[0];
$this->out_size['y'] = $this->size[1];

return;
}


if (
$thumb == FALSE)
{
$this->out_size['x'] = $this->new_size['w'];
$this->out_size['y'] = $this->new_size['h'];
}

return;
}

/**
* Returns file size
*
* @param void
* @return integer
*/
function file_size()
{
$size str_replace('.''', @filesize($this->image));
$this->file_size = ($size == 0) ? $size;
}


/**
* Returns input image according to type
*
* @param void
* @return variable
*/
function get_input()
{
switch(
$this->filemime)
{
case 
'gif':
$this->checkBackground();
$this->input_image imagecreatefromgif($this->image);
break;
case 
'jpg':
$this->input_image imagecreatefromjpeg($this->image);
break;
case 
'jpeg':
$this->input_image imagecreatefromjpeg($this->image);
break;
case 
'png':
$this->input_image imagecreatefrompng($this->image);
break;
}
}

/**
* Checks file to find background transparency
*
* @param void
* @return string
*/
function checkBackground()
{
// we need to determine transparency for gifs
// [url]http://be.php.net/imagecolortransparent[/url]
$fp fopen($this->image'rb');
$result fread($fp13);
$colorFlag ord(substr($result101)) >> 7;
$background ord(substr($result11));
if (
$colorFlag)
{
$tableSizeNeeded = ($background 1) * 3;
$result fread($fp$tableSizeNeeded);
$this->tRed ord(substr($result$background 31));
$this->tGreen ord(substr($result$background 11));
$this->tBlue ord(substr($result$background 21));

if (isset(
$this->tRed) && isset($this->tGreen) && isset($this->tBlue))
{
$this->tFlag TRUE;
}
}
fclose($fp);

return;
}

/**
* Returns output image according to type
*
* @param string $output_image
* @param string $image
* @return string
*/
function do_output($output_image$image)
{
switch(
$this->filemime) {
case 
'gif':
imagegif($output_image$image);
break;
case 
'jpg':
imagejpeg($output_image$image$this->quality);
break;
case 
'jpeg':
imagejpeg($output_image$image$this->quality);
break;
case 
'png':
imagepng($output_image$image);
break;
}
}

/**
* Returns array of file size
* (natural dimensions)
*
* @param integer $maxwidth
* @return array
*/
function resizing($maxwidth)
{
$width_percentage $maxwidth $this->size[0];
$height_percentage $maxwidth $this->size[1];

if ((
$this->size[0] > $maxwidth) || ($this->size[1] > $maxwidth))
{
if (
$width_percentage <= $height_percentage)
{
$this->new_size['w'] = round($width_percentage $this->size[0]);
$this->new_size['h'] = round($width_percentage $this->size[1]);
}
else
{
$this->new_size['w'] = round($height_percentage $this->size[0]);
$this->new_size['h'] = round($height_percentage $this->size[1]);
}
}
else
// square images ?
$this->new_size['w'] = $this->size[0];
$this->new_size['h'] = $this->size[1];
}
}

/**
* Returns array of file size
* (square thumbnails)
*
* @param void
* @return array
*/
function sys_resize()
{
$this->sys_size['w'] = $this->size[0];
$this->sys_size['h'] = $this->size[1];

if (
$this->sys_size['w'] > $this->sys_size['h'])
{
$this->offset['w'] = ($this->sys_size['w'] - $this->sys_size['h'])/2;
$this->offset['h'] = 0;
$this->sys_size['w'] = $this->sys_size['h'];
}
elseif (
$this->sys_size['h'] > $this->sys_size['w'])
{
$this->offset['w'] = 0;
$this->offset['h'] = ($this->sys_size['h'] - $this->sys_size['w'])/2;
$this->sys_size['h'] = $this->sys_size['w'];
}
else
{
$this->offset['w'] = 0;
$this->offset['h'] = 0;
$this->sys_size['w'] = $this->sys_size['h'];
}
}

/**
* Returns imagedestroy of input image
*
* @param integer $maxwidth
* @return mixed
*/
function sys_thumb($maxwidth)
{
$this->sys_resize();

$output_image imagecreatetruecolor($this->sys_thumb$this->sys_thumb);

// if we have transparency in the image
// it sucks that PHP auto sets background to black!!!!!!!
if ($this->tFlag == TRUE)
{
imagecolortransparent($output_imageimagecolorallocate($output_image,
$this->tRed$this->tGreen$this->tBlue));
}

// png special handling rules
if ($this->filemime == 'png')
{
// [url]http://be.php.net/manual/en/function.imagesavealpha.php[/url]
imagealphablending($output_imagefalse);
imagesavealpha($output_imagetrue);
}

@
imagecopyresampled($output_image$this->input_image00,
$this->offset['w'], $this->offset['h'],
$this->sys_thumb$this->sys_thumb,
$this->sys_size['w'], $this->sys_size['h']);

// for sys- naming convention
$image $this->path 'sys-' $this->filename;

$this->do_output($output_image$image);
imagedestroy($output_image);

return;
}

/**
* Returns new file name based upon exiting files to prevent name collisions
*
* @param string $filename
* @return string
*/
function checkName($filename)
{
static 
$v 1;

if (
file_exists($this->path '/' $filename $this->type))
{
// remove the previous version number
$filename preg_replace('/_v[0-9]{1,3}$/i'''$filename);
$v++;
$filename $filename '_v' $v;
$filename $this->checkName($filename);
}
else
{
$v 1;
return 
$filename;
}

return 
$filename;
}

}

?>

Last edited by Fou-Lu; 04-01-2010 at 04:49 PM.. Reason: Needs some PHP tags here
Tara Rotten is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 AM.


Advertisement
Log in to turn off these ads.