Jump to content

using gd to create thumbnails


wrathican

Recommended Posts

hey peeps

 

im trying to create a function (from one that i have already made(and works!)) that creats a thumbmail image from from an image on my server.

 

the function i made earlier is below:

<?php
//$result is the location of the image
//$picWidth is the width i want the image to be
//$imgDir is the directory that image is to be saved in


function createThumb($result, $picWidth, $imgDir) {
$poo = array();
if ($result == false) {
	//file could not be uploaded
	$poo[0] = false;
	$poo[1] = 1;
	return $poo;
}else{
	//file was uploaded. create thumbnail
	//pathinfo
	$file = pathinfo($result);
	//find the .ext
	$ext = $file['extension'];
	//strip .ext to lower
	$ext = strtolower($ext);
	//get image sizes
	$sizes = getimagesize($result);
	//ratio
	$ratio = $sizes[0]/$sizes[1];
	//create new height
	$newHeight = round($picWidth/$ratio);
	//create true colour image		
	$srcImg = imagecreatetruecolor($picWidth, $newHeight);
	if($ext == 'jpg' || $ext == 'jpeg') {
		$tmpImg = imagecreatefromjpeg($result);
	}elseif($ext == 'png') {
		$tmpImg = imagecreatefrompng($result);
	}elseif($ext == 'gif') {
		$tmpImg = imagecreatefromgif($result);
	}else{
		$poo[0] = false;
		$poo[1] = 2;
		return $poo;
	}
	//create destination image filename
	//get filename
	$name = $file['basename'];
	//dest filename
	$dstFile = $imgDir . $name;
	imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $picWidth, $newHeight, $sizes[0], $sizes[1]);
	//save image
	if($ext == 'jpg' || $ext == 'jpeg') {
		imagejpeg($srcImg, $dstFile, 100);
	}elseif($ext == 'png') {
		imagepng($scrImg, $dstFile, 100);
	}elseif($ext == 'gif') {
		imagegif($srcImg, $dstFile, 100);
	}else{
		$poo[0] = false;
		$poo[1] = 3;
		return $poo;;
	}

	$killSrc = imagedestroy($srcImg);
	$killTmp = imagedestroy($tmpImg);
	if($killSrc != false && $killTmp != false){
		$killOld = unlink($result);
		if($killOld != false){
			return $name;
		}else{
			$poo[0] = false;
			$poo[1] = 4;
			return $poo;
		}
	}else{
		$poo[0] = false;
		$poo[1] = 5;
		return $poo;
	}
}
}
?>

 

the problem im having is the difference between landscape and protrait images.

 

if i made $picWidth = 150 then a lanscape image would be 150px wide and around 100px high.

if i used the function on a protrait image then it would be 150px wide and around 200px high.

 

how can i change the function to recognise the difference between landscape and protrait  and make the maximum width/height to whatever i set $picWidth at?

 

Thanks

 

also. i think my function is quite sloppy, suggestions to improve it are welcome.

Link to comment
Share on other sites

This code is what I use. You should be able to adapt it to suite your needs. Simply use the getimagesize function to find out which is greater (width or height) and action the script accordingly.

 

if($width>$height){
$newwidth=$_GET[width]; //150
$prop = $width/$newwidth; // 300/150 = 2
$newheight= $height/$prop; // 2*300
}else{
$newheight=$_GET[height];
$prop = $height/$newheight;
$newwidth= $width/$prop;
}

Link to comment
Share on other sites

Resizing proportionally is just simple maths:

 

To resize proportionally X1/Y1 = X2/Y2

 

Therefore, if you know X2 (e.g. the width is greater than height, so width = 800), then Y2 = Y1/X1 * X2 - by rearranging the above formula

 

Conversely, if you know Y2, then X2 = X1/Y1 * Y2

 

Edit: See here for an FAQ/code snippet for creating thumbnails.

Link to comment
Share on other sites

I wrote this function and posted on another forum quite a while ago.  May help you out....

 

    // Function to create GD thumbnail from original image 
    // by Jonny Thunder (www.badacid.net) 
    // Please leave this comment in tact if you use this function 
    //   
    function createthumbnail ($original_image, $target_image, $thumbsizemax, $issquare) { 
        $imagesize = @GetImageSize($original_image); 
        if ($issquare) { 
            if ($imagesize[0] > $imagesize[1]) { 
                $smallestdimension = $imagesize[1]; 
                $widthoffset       = ceil(($imagesize[0] - $imagesize[1]) / 2); 
                $heightoffset      = 0; 
            } else { 
                $smallestdimension = $imagesize[0]; 
                $widthoffset       = 0; 
                $heightoffset      = ceil(($imagesize[1] - $imagesize[0]) / 2); 
            } 
            $source_image = ImageCreateFromJPEG($original_image); 
            $temp_image1 = ImageCreateTrueColor($smallestdimension, $smallestdimension); 
            ImageCopyResampled($temp_image1, $source_image, 0, 0, $widthoffset, $heightoffset, $smallestdimension, $smallestdimension, $smallestdimension, $smallestdimension); 
            ImageJPEG($temp_image1, $target_image, 85);       
            $temp_image2 = ImageCreateTrueColor($thumbsizemax, $thumbsizemax); 
            ImageCopyResampled($temp_image2, $temp_image1, 0, 0, 0, 0, $thumbsizemax, $thumbsizemax, $smallestdimension, $smallestdimension); 
            ImageJPEG($temp_image2, $target_image, 85); 
            ImageDestroy($temp_image1); 
            ImageDestroy($temp_image2); 
        } else { 
            if ($imagesize[0] > $thumbsizemax) { 
                if ($imagesize[0] > $imagesize[1]) { 
                    $reducepercent = ( $thumbsizemax / $imagesize[0] ) * 100; 
                    $xsize = $thumbsizemax; 
                    $ysize = ceil(( $imagesize[1] / 100) * $reducepercent ); 
                } else { 
                    $reducepercent = ( $thumbsizemax / $imagesize[1] ) * 100; 
                    $xsize = ceil(( $imagesize[0] / 100) * $reducepercent ); 
                    $ysize = $thumbsizemax; 
                } 
            } else { 
                $xsize = $imagesize[0]; 
                $ysize = $imagesize[1]; 
            } 
            $source_image = ImageCreateFromJPEG($original_image); 
            $temp_image1 = ImageCreateTrueColor($xsize, $ysize); 
            ImageCopyResampled($temp_image1, $source_image, 0, 0, 0, 0, $xsize, $ysize, $imagesize[0], $imagesize[1]); 
            ImageJPEG($temp_image1, $target_image, 85);       
            ImageDestroy($temp_image1); 
        } 
        if (!file_exists($target_image)) { return false; } else { return true; } 
    }  

 

 

You have two options...

 

You can get the function to generate a square thumbnail by using this (for example)....

 

$thumbsize = 200;
$return = createthumbnail ("original image", "target thumbnail", $thumbsize, true);  

 

 

or you can create a proportional one (which will make the longest edge the size given)

 

$thumbsize = 200;
$return = createthumbnail ("original image", "target thumbnail", $thumbsize, false);  

 

 

Hope this helps!

 

EDIT:  This function is only for JPG - though it could be easily modified, and will give you pointers on how I did resizing.

 

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.