Jump to content

imagecreatetruecolor problem


Harley1979

Recommended Posts

I am using imagecreatetruecolor within a script for resizing my images which is causing me a problem.

 

For some reason instead of creating a black to my dimensions image like it should do, I am getting "resource id#6" as its value which of course is an invalid image resource.

 

Has anyone else ever experienced this before, what is it and why can I not simply create a blank image?

 

I have a hunch it is something to do with the php/gd setup which to me looks fine.

 

P.S. I am using GD2.

 

Thanks

Link to comment
Share on other sites

Here you go:

 

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

$max_size = 3000000;

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

if(isset($original)){
	$imageMsg = "You must choose an image to upload";
}

if(is_uploaded_file($original)){

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

	if($type == "image/jpeg" || $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);

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

	// begin create thumbnail				
	list($width, $height) = getimagesize($original);

	// 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, $original, 0, 0, $cl['x'], $cl['y'], $thumbsize, $thumbsize, $cropwidth, $cropheight);

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

	list($origWidth, $origHeight) = getimagesize($original);

	// if our image is not the desired dimension, resample to default dimensions		
	if($origWidth < $origHeight && $origWidth > 450){
		$newHeight = 530;
		$newWidth = $origWidth * ($newHeight/$origHeight);

		$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
		imagecopyresampled($resizedImage, $original, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);

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

		$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
		imagecopyresampled($resizedImage, $original, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
	}
		// copy image to folder
		$res = imagejpeg($resizedImage, $path.$newName);

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

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

 

Thanks

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.