Solved

call a function twice

Posted on 2010-08-17
6
350 Views
Last Modified: 2012-05-10
how can I make this a function
and call the function




I want to call the function twice
so the image resizes twice
<?php



// This is the temporary file created by PHP

//$uploadedfile = $_FILES['uploadfile']['tmp_name'];

$uploadedfile="uploaded/7dkitt2.jpg";



// Create an Image from it so we can do the resize

$src = imagecreatefromjpeg($uploadedfile);



// Capture the original size of the uploaded image

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



// For our purposes, I have resized the image to be

// 600 pixels wide, and maintain the original aspect

// ratio. This prevents the image from being "stretched"

// or "squashed". If you prefer some max width other than

// 600, simply change the $newwidth variable

$newwidth=600;

$newheight=($height/$width)*600;

$tmp=imagecreatetruecolor($newwidth,$newheight);



// this line actually does the image resizing, copying from the original

// image into the $tmp image

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);



// now write the resized image to disk. I have assumed that you want the

// resized, uploaded image file to reside in the ./images subdirectory.

//$filename = "images/". $_FILES['uploadfile']['name'];

$filename = "uploaded/newfile.jpg";

imagejpeg($tmp,$filename,100);



imagedestroy($src);

imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request

// has completed.

?>

Open in new window

0
Comment
Question by:rgb192
6 Comments
 
LVL 5

Accepted Solution

by:
ploftin earned 167 total points
ID: 33460136
Try this.
<?php

function resizeImage($uploadedfile, $newfile) {
	// This is the temporary file created by PHP
	
	// Create an Image from it so we can do the resize
	$src = imagecreatefromjpeg($uploadedfile);
	
	// Capture the original size of the uploaded image
	list($width,$height)=getimagesize($uploadedfile);
	
	// For our purposes, I have resized the image to be
	// 600 pixels wide, and maintain the original aspect
	// ratio. This prevents the image from being "stretched"
	// or "squashed". If you prefer some max width other than
	// 600, simply change the $newwidth variable
	$newwidth=600;
	$newheight=($height/$width)*600;
	$tmp=imagecreatetruecolor($newwidth,$newheight);

	// this line actually does the image resizing, copying from the original
	// image into the $tmp image
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	
	// now write the resized image to disk. I have assumed that you want the
	// resized, uploaded image file to reside in the ./images subdirectory.
	//$filename = "images/". $_FILES['uploadfile']['name'];
	imagejpeg($tmp,$newfile,100);
	
	imagedestroy($src);
	imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
	// has completed.
}

//Call the function like this:
resizeImage("uploaded/7dkitt2.jpg", "uploaded/newfile.jpg")

?>

Open in new window

0
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 167 total points
ID: 33460152
It is not exactly clear which portion of the code you want to call twice, so here is a general idea.

$param1 = somecode
somemorecode

somecode_I_want_to_repeat
$some_new_value = somemorecoee_I_want_to_repeat($param1)

code_after_initial_code.

Now you want the repeatable code to be in a function .. so it becomes

$param1 = somecode
somemorecode

// call function twice
$output = new_function($param1);
$second_output = new_function($output);

function new_function($param1) {
     somecode_I_want_to_repeat
    $some_new_value = somemorecoee_I_want_to_repeat($param1)
     return $some_new_value;
}

code_after_initial_code.
0
 
LVL 5

Expert Comment

by:ploftin
ID: 33460211
I didn't state it but if you wanted to call m function a second time, add the following to line 37.
resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg")

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 166 total points
ID: 33460582
Rather than adding two lines calling the same function like so which is messy...

resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg");
resizeImage("uploaded/newfile.jpg", "uploaded/newfile.jpg");

perhaps try this



(extending ploftin's code)
<?php



function resizeImage($uploadedfile, $newfile, $iterations) {

	for ( $counter = 1; $counter <= $iterations; $counter++) { //this is what loops through the code twice or more

        // This is the temporary file created by PHP

        

        // Create an Image from it so we can do the resize

        $src = imagecreatefromjpeg($uploadedfile);

        

        // Capture the original size of the uploaded image

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

        

        // For our purposes, I have resized the image to be

        // 600 pixels wide, and maintain the original aspect

        // ratio. This prevents the image from being "stretched"

        // or "squashed". If you prefer some max width other than

        // 600, simply change the $newwidth variable

        $newwidth=600;

        $newheight=($height/$width)*600;

        $tmp=imagecreatetruecolor($newwidth,$newheight);



        // this line actually does the image resizing, copying from the original

        // image into the $tmp image

        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

        

        // now write the resized image to disk. I have assumed that you want the

        // resized, uploaded image file to reside in the ./images subdirectory.

        //$filename = "images/". $_FILES['uploadfile']['name'];

        imagejpeg($tmp,$newfile,100);

        

        imagedestroy($src);

        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request

        // has completed.

	}

}



//Call the function like this:

resizeImage("uploaded/7dkitt2.jpg", "uploaded/newfile.jpg", 2);



?>

Open in new window

0
 
LVL 5

Expert Comment

by:ploftin
ID: 33460728
Nice addition Snarfles.
0
 

Author Closing Comment

by:rgb192
ID: 33461073
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now