Jump to content

[SOLVED] Functions and variable scope issue?


fireice87

Recommended Posts

Hey
I've got a form that lets users upload 3 images, these are then stored in a temp folder. I Then resized the 3 images.
I'am attempting to re-code this re-sizing script into a function as good practice, but it doesnt seem that the file paths that are stored in the variables $path1,$path2,$path3 make it to the function, the error messages thrown suggests this. Thanks for any help should hopefully be quite simple for those familiar with wrinting functions.
cheers

To call the function id give it the variables to use(do these need to be global?)
[code]
<?php image_handler($path1, $path2, $path3, $prod_id); ?>

 

The function

<?php
function image_handler()
{
mkdir("../Images/small_product_image/$prod_id/");
mkdir("../Images/large_product_image/$prod_id/");
$c = 0;
if ($c < 2) 
    {
if($c=0) {$image = $path1;}
	else if($c=1) {$image = $path2;}
	else $image = $path3;

	$ext = substr($image, -3); //get extension 
	$x = @getimagesize($image); // get image size 
	$sw = $x[0]; // image width
	$sh = $x[1]; // image height
	$w = 150;    //new width
	$wb = 350;   //new width for large image

		$h = (100 / ($sw / $w)) * .01;
		$h = @round ($sh * $h);     //new hieght

		$hb = (100 / ($sw / $wb)) * .01;
		$hb = @round ($sh * $hb);       //new hieght

		$im = @ImageCreateFromJPEG ($image) or // Read JPEG Image
		$im = @ImageCreateFromPNG ($image) or // or PNG Image
		$im = @ImageCreateFromGIF ($image) or // or GIF Image
		$im = false; // If image is not JPEG, PNG, or GIF

		$thumb = @ImageCreateTrueColor ($w, $h); // Create the resized image destination
		$thumbb = @ImageCreateTrueColor ($wb, $hb);

		@ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Copy from image source, resize it, and paste to image destination
		@ImageCopyResampled ($thumbb, $im, 0, 0, 0, 0, $wb, $hb, $sw, $sh);


	$filename = "../Images/small_product_image/$prod_id/$c.".$ext; 
	$filenameb = "../Images/large_product_image/$prod_id/$c.".$ext;
	imagejpeg($thumb,$filename,100); 
	imagejpeg($thumbb,$filenameb,100); 

	$c++;
}

} 

?>

 

Error messages

 

Warning: mkdir() [function.mkdir]: File exists

 

Warning: mkdir() [function.mkdir]: File exists

 

Warning: Division by zero

 

Warning: Division by zero

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in

[/code]
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.