Solved

call a function twice

Posted on 2010-08-17
6
347 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Script - Am I missing anything here? 8 38
Creating a slider 12 34
uploading multiple image with php 14 19
PHP string issue 5 17
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.

758 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

20 Experts available now in Live!

Get 1:1 Help Now