RSS

Create image thumb

17 Apr

$this->thumb($_FILES[‘logofile’][‘tmp_name’], ‘images/logo.png”, $logowidth, $logoheight);

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;
}

Advertisements
 
1 Comment

Posted by on April 17, 2013 in Joomla

 

One response to “Create image thumb

  1. Jignesh Patel

    April 19, 2013 at 9:59 am

    Reblogged this on RJ Solusoft.

     

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

 
%d bloggers like this: