Jump to content

imagecopyresampled() argument is not a valid Image resource


surreal5335

Recommended Posts

I am trying to crop an image and I used a tutorial showing how to create a new canvas, shrink it 75 by 75, shrink the original image's copy to 1/4 of its size. Then take a cut out of it (75 by 75) out of the center.

 

function small($id) {
// original image location
$img = "photos/original/".$id.".jpeg";

// set our image canvas
$canvas_width = 75;
$canvas_height = 75;

// create a blank canvas
$canvas = imagecreatetruecolor($canvas_width, $canvas_height);

// get width and height of original image
list($img_width, $img_height) = getimagesize($img);

// $original now holds the image in the memory banks
$original = imagecreatefromjpeg($img);

// copy original onto canvas
// 0, 0: sets the x and y position at top left hand corner of $original
// (($canvas_width / 2) * 4)) the * 4 is used to balance the $image_width / 4 to keep the crop in the center
imagecopyresampled($canvas, $orignal, 0, 0, (($img_width / 2) - (($canvas_width / 2) * 4)),
											(($img_height / 2) - (($canvas_height / 2) * 4)),

											$image_width / 4,
											$image_height / 4,
											// fills last parameter of original height and width not scaled down.
											$image_width,
											$image_height
											); // line 38

if(imagejpeg($canvas, 'photos/small/'.$id.'.jpeg', 80)){

	return true;
} else {
	return false;
}
} // end small()

 

The error I am getting from this is:

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home1/royalvil/public_html/photo_voting/photo_fns.php on line 38

 

line 38 has been commented in the code.

Would this error be refering to original images path? Likes it unable to find the image?

I checked the directory path to the image to be copied and its correct.

 

What else could this error be refering to?

 

I appreciate the help

Link to comment
Share on other sites

Thanks alot for the help, got rid of the error. My last problem is... while the cropped canvas appears in the folder specified, its completely black. Seems that I am not getting the image from the folder or setting it to the canvas properly. Mind giving me an idea as to how I could remedy this issue?

 

Thanks a lot

Ben

Link to comment
Share on other sites

imagecreatefromjpeg() only returns false if it cannot create the image, it doesn't throw a fatal error. Verify the $img path actually exists and that $original isn't false after the call.

 

I think the real problem lies with your method of calculating the src_x and src_y parameters though. Try removing the "* 4" part of each. You may want to round the result too..

Link to comment
Share on other sites

Thanks for the suggestions,

 

I checked the path for $img and it came out fine.

 

My $original I have not been able to get a reponse back if its true or false.

 

here is the methods I have tried to achieve this:

 

if(!$original) {
	echo "original is false";
} else {
	echo "original is true";
}


if($original == false) {
	echo "original is false";
} else {
	echo "original is true";
}



echo imagecreatefromjpeg($img);


 

I put these inside my small() function so the local variable $original would be accessible.

 

Any suggestions?

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.