Jump to content

image resize script issue


jarvis

Recommended Posts

Hi all,

 

I've got the following script which nicely creates my thumbnail at 120px high or wide. Rather than copy the original size image as well, I'd like the original resized to 800x600

 

I cannot see the best way to do this, so any help is much appreciated!

 

Here's my code.

function saveThumb($gal,$fn) {
define("MAX_XY", 120); // maximum width or height of thumbnail image
$fpath = "categories/$gal/$fn";

$info = GetImageSize("$fpath");

// do we need to resize image?
if ($info[0] > MAX_XY || $info[1] > MAX_XY) {
	// is image landscape?
	if ($info[0] >= $info[1]) {
		$width = MAX_XY;
		$height = $info[1]*MAX_XY/$info[0];
	// or portrait?
	} else {
		$height = MAX_XY;
		$width = $info[0]*MAX_XY/$info[1];
	}
} else {
	// use original dimensions
	$width = $info[0];
	$height = $info[1];
}

// create new thumbnail image
$im = ImageCreateTrueColor($width, $height);

$im_big = ""; // default is no image
switch ($info[2]) {
	case 1 :
		if (ImageTypes() & IMG_GIF) // test for GIF support 
			$im_big = ImageCreateFromGIF("$fpath");
		break;
	case 2 :
		if (ImageTypes() & IMG_JPG) // test for JPEG support 
			$im_big = ImageCreateFromJPEG("$fpath");
		break;
	case 3 :
		if (ImageTypes() & IMG_PNG) // test for PNG support  
			$im_big = ImageCreateFromPNG("$fpath");
		break;
	case 4 :
		if (ImageTypes() & IMG_BMP) // test for BMP support  
			$im_big = ImageCreateFromBMP("$fpath");
		break;			
}

if ($im_big) {
	/* resize original image into thumbnail - see PHP Manual for details on
	 * the arguements ImageCopyResized takes
	*/
	ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
} else {
	// couldn't load original image, so generate 'no thumbnail' image instead
	$width = 120;
	$height = 120;
      	$im = ImageCreate($width, $height); // create a blank image
      	$bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black
      	$tc  = ImageColorAllocate($im, 255, 255, 255); // text color = white
      	ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle
      	ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text
		ImageString($im, 3, 17, 48, "available", $tc);  
  	}

/* save our image in thumb directory - if the second argument is left out,
 * the image gets sent to the browser, as in thumbnail.php
*/
ImageJPEG($im, "categories/".$gal."/t_".$fn);

// clean up our working images
ImageDestroy($im_big);
ImageDestroy($im);
  }

 

Thanks in advanced!

Link to comment
Share on other sites

Oh...yeh you're right it doesn't check for image types...but do you want the thumb to be a specific type?

 

With this script you can just use it to create the thumbnail with whatever dimensions you want...just for $img use the originall image, and for $path enter the path you want to store the thumbnail in and it will create/save it for you.

 

But other then that here are a few more that I've found for ya:

 

http://www.mightystuff.net/php_thumbnail_script

 

http://phpthumb.sourceforge.net/

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.