Solved

call a function twice

Posted on 2010-08-17
6
355 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 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