Jump to content

Image resize problem


Harley1979

Recommended Posts

Got a problem with resizing an image.

 

Here is my code, the image uploads fine but just seems to bypass the part where it resizes.

 

Not a problem when creating thumbnails using the almost exact same method further on.

 

Can anyone help?

 

Thanks

 

<?php
	$caption = $_REQUEST['caption'];

	$max_size = 8388608;

	$size = $_FILES['imagefile']['size'];
	$type = $_FILES['imagefile']['type'];
	$imageFile = $_FILES['imagefile']['tmp_name'];

	if(isset($imageFile)){

	if ($type == "image/jpeg" || $type == "image/gif"){

	$src = imagecreatefromjpeg($imageFile);
	list($origWidth, $origHeight) = getimagesize($imageFile);

	$path = "../gallery/";
	$thumbPath = "../gallery/thumbs/";

	// get last image from db
	$result = mysql_query("SELECT photo FROM gallery ORDER BY id desc LIMIT 1");
	$row = mysql_fetch_row($result);

	// modify file name
	if(empty($row[0])){$photoName = 0;}
	$photoName = $row[0] + 1;
	$newName = $photoName.".jpg";


	/////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////  resize original image  ////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////

	// if our image is not the desired dimension, resample to default dimensions

	if($origWidth < $origHeight && $origWidth > 560){
		$newHeight = 540;
		$newWidth = $origWidth * ($newHeight/$origHeight);

	} elseif($origWidth > $origHeight && $origHeight > 540){
		$newWidth = 560;
		$newHeight = $origHeight * ($newWidth/$origWidth);

	} else {
		$newWidth = $origWidth;
		$newHeight = $origHeight;
	}

	$mainImage = imagecreatetruecolor($newWidth, $newHeight);
	imagecopyresampled($mainImage, $src, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);

	// save to path
	imagejpeg($mainImage, $path.$newName, 100);

	// destroy image
	imagedestroy($mainImage);

	/////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////   create thumbnails   ////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////////////////////

	// set crop size
	if($origWidth > $origHeight){
		$biggestside = $origWidth;
	} else {
		$biggestside = $origHeight;
	}

	// crop size will be half that of the largest side
	$croppercent = .80;
	$cropwidth = $biggestside*$croppercent;
	$cropheight = $biggestside*$croppercent;

	// get top left coordinate
	$cl = array("x"=>($origWidth-$cropwidth)/2, "y"=>($origHeight-$cropheight)/2);

	// create thumbnail
	$thumbsize = 75;

	$thumb = imagecreatetruecolor($thumbsize, $thumbsize);
	imagecopyresampled($thumb, $src, 0, 0, $cl['x'], $cl['y'], $thumbsize, $thumbsize, $cropwidth, $cropheight);

	// save to path
	imagejpeg($thumb, $thumbPath.$newName, 100);

	imagedestroy($src);
	imagedestroy($thumb);

		if(move_uploaded_file($imageFile, $path.$newName)){

			mysql_query("INSERT INTO gallery (photo, caption) VALUES ('$photoName', '$caption')") or die (mysql_error());
			$imageMsg = "Image successfuly uploaded!";
		}

	} else {
		$imageMsg = "Incorrect file type";}
}
?>

Link to comment
Share on other sites

What are your parameters for wanting a resize? And what's the desired dimension?

 

Also, you might want to use >= or <= in the checks between origHeight and origWidth, otherwise a square image will always remain the same. Even if it is 1000x1000.

Link to comment
Share on other sites

What do you consider too big though, having a bit of trouble seeing that from the code you've posted, from what I can get this is what your checking for:

 

An image is large if origHeight and origWidth are both over 560(with origHeight being the largest), and the other is if both origHeight and origWidth are both over 540(with origWidth being largest).

 

Although this does leave a dead zone if origHeight and origWidth are between 540 and 560 and origHeight is the largest it'll stay the same.

 

What is the dimension of the image your using for testing?

Link to comment
Share on other sites

Did a quick test of your if block(occurs to me this should of been the first thing I did...) and it seems to work like it's supposed to(although add in the >=/<= signs, since without them a square image will slip by), and I don't have enough experience(read none) working with the image functions to help see if anything went wrong there.

 

Judging by a quick read over of the imagecopyresample page, my only thought is the decimal places might be messing it up somehow, but I'm doubtful of that. And I can't seem to get the functions(image functions) working on my end to try much else....Sorry.

Link to comment
Share on other sites

Thanks for the help, I'll try your suggestions.

 

The thing is I know this script has worked for me in the past as I have used it on another site which I've refered to for this script I'm using now.

 

Strange, I know I must have missed something or just can't see something but I can't see the wood for the trees after looking at it for days now.

 

Thanks all the same

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.