Jump to content

Image imagecopyresampled problem


Harley1979

Recommended Posts

Hello,

 

This is the first time I have ever attempted uploading/resizing images in php. Upto now I've done ok I think except for one small problem I am having.

 

I have a line of code to determine if the width or height of an image is bigger than it should appear on screen, if so to resize it to a set width or height proportionatly.

 

I have succeeded in resizing an image for my thumbnails, but just can't seem to do so for my main image.

 

Can someone point out where I am going wrong?

 

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

$max_size = 2000000;

if(isset($HTTP_POST_FILES['imagefile'])){
	$imageMsg = "You must choose an image to upload";
}

if(is_uploaded_file($HTTP_POST_FILES['imagefile']['tmp_name'])){

	if($HTTP_POST_FILES['imagefile']['size'] > $max_size){
		$imageMsg = "The image is just too feckin big!";
		$err = true;
	}

	if($HTTP_POST_FILES['imagefile']['type'] == "image/jpeg" || $HTTP_POST_FILES['imagefile']['type'] == "image/gif"){

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

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

	// if our image is not the desired dimension, resample to default dimensions
	$orig = $HTTP_POST_FILES['imagefile']['tmp_name'];
	list($origWidth, $origHeight) = getimagesize($orig);

	if($origWidth < $origHeight && $origWidth > 450){
		$newHeight = 530;
		$newWidth = $origWidth * ($newHeight/$origHeight);
		echo $newWidth."<br />";
		echo $newHeight."<br />";

		$resizedImage = imagecreatetruecolor($newWidth, $newHeight);

                        //this is where i am getting the error
		imagecopyresampled($resizedImage, $orig, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
	}
	 elseif($origWidth > $origHeight && $origHeight > 530){
		$newWidth = 550;
		$newHeight = $origHeight * ($newWidth/$origWidth);
		echo $newWidth."<br />";
		echo $newHeight."<br />";

		$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
                        //this is where i am getting the error
                        imagecopyresampled($resizedImage, $orig, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
	}

		$res = imagejpeg($resizedImage, $path.$newName);

		if(!$res){
			$imageMsg = "Upload failed!";
			$err = true;
		} else {
			$imageMsg = "Upload successful";

			// create thumbnail
			$src = ($HTTP_POST_FILES['imagefile']['tmp_name']);

			list($width, $height) = getimagesize($src);

			$storeImage = imagecreatefromjpeg($src);

			// set crop size
			if($width > $height){
				$biggestside = $width;
			} else {
				$biggestside = $height;
			}

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

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

			// create thumbnail
			$thumbsize = 75;
			$thumb = imagecreatetruecolor($thumbsize, $thumbsize);
			imagecopyresampled($thumb, $storeImage, 0, 0, $cl['x'], $cl['y'], $thumbsize, $thumbsize, $cropwidth, $cropheight);

			// save to path and destroy temp files
			imagejpeg($thumb, $thumbPath.$newName);

			imagedestroy($thumb);
			imagedestroy($storeImage);
		}

	} else {
		$imageMsg = "Wrong file type!";
		$err = true;
	}
}

Link to comment
Share on other sites

For the line you have marked, the call looks okay.  Make sure the image source and destination are correct.  I have a very similar function that I got from tweaking this code:

 

http://www.netlobo.com/php_image_resize_gd2.html

 

It become part of a larger application.  I remember the source/destination were the ones that gave me problems.  Once I got that right, the entire thing worked.

 

 

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.